vi editior Command set - 1
General Startup To use vi: vi filename To exit vi and save changes: ZZ or :wq
To exit vi without saving changes: :q! To enter vi command mode: [esc]
Counts A number preceding any vi command tells vi to repeat that command that many times.
Cursor Movement h j k l move left (backspace) move down move up move right (spacebar) move to the beginning of the next line
[return] $ 0 ^ w W b B e E H M L
last column on the current line move cursor to the first column on the current line move cursor to first nonblank column on the current line move to the beginning of the next word or punctuation mark move past the next space move to the beginning of the previous word or punctuation mark move to the beginning of the previous word, ignores punctuation end of next word or punctuation mark end of next word, ignoring punctuation move cursor to the top of the screen move cursor to the middle of the screen move cursor to the bottom of the screen
Screen Movement G xG move to the last line in the file move to line x
z+ z z^F ^B ^D ^U ^R ^L
move current line to top of screen move current line to the middle of screen move current line to the bottom of screen move forward one screen move backward one line move forward one half screen move backward one half screen redraw screen ( does not work with VT100 type terminals ) redraw screen ( does not work with Televideo terminals )
Inserting r R i a A O replace character under cursor with next character typed keep replacing character until [esc] is hit insert before cursor append after cursor append at end of line open line above cursor and enter append mode
Deleting x dd dw db delete character under cursor delete line under cursor delete word under cursor delete word before cursor
Copying Code yy (yank)'copies' line which may then be put by the p(put) command. Precede with a count for multiple lines.
Put Command brings back previous deletion or yank of lines, words, or characters P p Find Commands ? finds a word going backwards bring back before cursor bring back after cursor
/ f F t T ;
finds a word going forwards finds a character on the line under the cursor going forward finds a character on the line under the cursor going backwards find a character on the current line going forward and stop one character before it find a character on the current line going backward and stop one character before it repeat last f, F, t, T
Miscellaneous Commands . u U xp J ^G % mx 'x repeat last command undoes last command issued undoes all commands on one line deletes first character and inserts after second (swap) join current line with the next line display current line number if at one parenthesis, will jump to its mate mark current line with character x find line marked with character x
NOTE: Marks are internal and not written to the file.
Line Editor Mode Any commands form the line editor ex can be issued upon entering line mode. To enter: type ':' To exit: press[return] or [esc]
ex Commands For a complete list consult the UNIX Programmer's Manual
READING FILES copies (reads) filename after cursor in file currently editing :r filename
WRITE FILE :w saves the current file without quitting
MOVING :# :$ move to line # move to last line of file
SHELL ESCAPE executes 'cmd' as a shell command. :!'cmd'
Command Set 2
Notes:
In Unix it matters whether you type upper or lowercase letters. In many cases typing a number before a command causes that action to be repeated that number of times. Don't press the Enter/Return key unless instructed to do so.
Command to enter vi edit mode
vi filename (press Enter/Return key) Opens the named file if it exists, or creates new file and opens it.
Commands to move the cursor
k j l h (lowercase)
These keys move the cursor one line above, one line below, one space right, or one space left respectively. w (lowercase) Moves cursor to next word. b (lowercase) Moves cursor to last word. arrow keys Move cursor one space in direction of arrow. 0 (zero) Moves cursor to the beginning of current line. $ (dollar sign)
Moves cursor to the end of the current line - (hyphen) Moves cursor to the beginning of the previous line. + (plus sign) Moves cursor to the beginning of the next line. G (uppercase) Moves cursor to the last line in the file. 1G (number one and uppercase G) Moves cursor to the first line in the file.
Commands to move to another screen
Note: Hold down the Ctrl (control) key while pressing the letter indicated. Ctrl-b (lowercase) Moves back to the previous screen. Ctrl-f (lowercase) Moves forward to the next screen. Ctrl-u (lowercase) Moves up (back) one half screen. Ctrl-d (lowercase) Moves down (forward) one half screen.
Commands to put you in text insert mode
Note: After inserting text, press Esc (escape) key to return to command mode. a (lowercase) Use to insert (append) text to the right of cursor. A (uppercase) Use to insert (append) text at the end of the current line. i (letter i (eye) lowercase) Use to insert text to the left of cursor. I (letter I (eye) uppercase)
Use to insert text at the beginning of the current line. o (letter o lowercase) Use to open a new line for inserting text below cursor line. O (letter O uppercase) Use to open a new line for inserting text above cursor line.
Commands to change text
Note: After making change, press Esc (escape) key to return to command mode. cw (lowercase) Changes word under and to right of cursor to new text you enter. cc (lowercase) Changes current line to new text you enter. s (lowercase) Substitutes character under cursor with new text you enter. S (uppercase) Substitutes current line with new text you enter. C (uppercase) Changes text from cursor to end of line to text you enter. r (lowercase) Replaces character under cursor with new character. (tilde) Changes case of character under cursor.
Commands to delete text
x (lowercase) Removes the character under the cursor. dw (lowercase) Deletes word under and to right of cursor. dd (lowercase) Deletes the cursor line.
D (uppercase) Deletes from cursor to end of line.
Command to join two lines
J (uppercase) Joins next line to the end of the cursor line.
Commands to search for a word or string of words
/keyword or /string (press Enter/Return key) Searches forward through text. Locates first instance of keyword or string. ?keyword or ?string (press Enter/Return key) Searches backward through text. Locates first instance of keyword or string. n (lowercase) Searches in direction of original /keyword, ?keyword, /string, or ?string command. Locates next instance of keyword or string. N (uppercase) Searches in opposite direction of original /keyword, ?keyword, /string, or ?string command. Locates next instance of keyword or string.
Commands to copy lines and put them in another location
yw (lowercase) Yanks (copies) word under and to right of cursor. Y (uppercase) Yanks (copies) line the cursor is on. p (lowercase) Puts last word or line yanked or deleted after cursor. P (uppercase) Puts last word or line yanked or deleted before cursor.
Command to recover from last change
u (lowercase) Undoes last change.
Commands to save changes or leave vi editing session
:w (colon and lowercase w) Saves changes, but does not leave vi editing session. :wq (colon and lowercase wq) Saves changes, and leaves vi editing session. ZZ (uppercase) Saves changes, and leaves vi editing session. :q! (colon, lowercase q, and exclamation) Does not save changes made. Just leaves vi editing session.
Command Set 3
Enter the vi editor
The percent sign (%) is the prompt in UNIX.
% vi filename <return>
enters vi and loads file into the buffer (memory). If this is a new file, enter the name chosen.
When you first enter vi for a new file, you will see a row of tildes (~) along the left side of your screen with the cursor blinking above the first tilde on the screen. Refer to the "Insert" section to enter text in your file. Refer to the other sections to edit text. For all the following vi commands commands to work properly, you must first press the escape key <Esc> (<esc> on a Macintosh) to ensure you are in the command mode. If you are already in the command mode, your terminal will beep when you press the escape key.
Insert (press the <Esc> (<esc>) to end text insertion
:r filename
reads existing file and inserts it into the current document
i
inserts text before the cursor
I
inserts text at the beginning of the line
a
appends text after the cursor
A
appends text at the end of the line
o
opens a line below the current line and inserts
O
opens a line above the current line and inserts
J
joins the current line with the following line
Cursor motion
h
moves the cursor left one character position
l
moves the cursor right one character position
k
moves the cursor up one line
j
moves the cursor down one line
0
(zero) moves the cursor to the beginning of the current line
$
moves the cursor to the end of the current line
b
backs up a word
e
moves to the end of the current word
w
moves to the beginning of the next word
H
moves the cursor "home" to the top of the screen
1G
moves the cursor to the first line of the file (2G will move the cursor to the second line of the file, 3G to the third, etc.)
:1
moves the cursor to the first line of the file (:2 will move the cursor to the second line of the file, :3 to the third line, etc.)
L
moves the cursor to the beginning of the last line on the screen that is not blank
G
moves the cursor to the last line of the file <Ctrl-l> (<control-l> on a Macintosh) redraws the screen <Ctrl-b> (<control-b>) moves the screen backward one page <Ctrl-f> (<control-f>) moves the screen forward one page
Change, substitute and replace (a number can be used for a repeat factor, e.g., 3cc)
:%s/orig/mew/g
:%s/orig/mew/g
globally replaces original string with new string
.
(period) repeats most recent command that made a change
cw
changes (overwrites) a word to the text typed in (up to <Esc> <esc>)
C
changes rest of line to text typed in (up to <Esc> <esc> )
cc
deletes entire line and allows text to be typed in (up to <Esc> <esc> )
r
replaces a single character only
R
replaces characters continuously with characters typed in (up to <Esc> <esc>)
s
replaces one character by substituting several
Delete Deleted text is put in a buffer and can be retrieved with the p or P command (see the "Yank and put back" section of this document).
x
deletes a character (a number can be used for a repeat factor, e.g. 3x or 3dd)
dw
deletes a word
dd
deletes a line
d0
(zero) deletes back to the beginning of the line
d$
deletes to the end of the line
dG
deletes to the end of the file
dH
deletes to the beginning of the file
p
puts previously deleted text after the cursor
P
puts previously deleted text before the cursor "put" command can be repeated)
Undo
u
undoes the last action taken (only if the cursor hasn't been moved with the h, l, k or j cursor motion keys)
U
restores current line even after several changes
Yank and put back (like block and copyoriginal text remains)
yw
yanks a word and puts it in the buffer
yy or Y
yanks entire line and puts it in the buffer
y0
(zero) yanks from cursor to beginning of line and puts it in the buffer
y$
yanks from cursor to end of line and puts it in the buffer
p
puts yanked or deleted text after the cursor
P
puts yanked or deleted text before the cursor (the "put" command can be repeated)
Exit the vi editor (be sure to press the <Esc> (<esc>) key to exit)
:w <Enter> or <return>
writes to the buffer, saving changes in the file, but does not exit
:wq <Enter> or <return>
writes and quits, saving changes in the file
:q <Enter> or <return>
quits without saving changes (vi may object with an error message)
:q! <Enter> or <return>
quits, no changes saved (overrides vi's objection)