vi Editor Commands


Quick links...

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 move left (backspace)
jmove down
kmove up
lmove right (spacebar)
[return]move to the beginning of the next line
$last column on the current line
0move cursor to the first column on the current line
^move cursor to first nonblank column on the current line
wmove to the beginning of the next word or punctuation mark
Wmove past the next space
bmove to the beginning of the previous word or punctuation mark
Bmove to the beginning of the previous word, ignores punctuation
eend of next word or punctuation mark
Eend of next word, ignoring punctuation
Hmove cursor to the top of the screen
Mmove cursor to the middle of the screen
Lmove cursor to the bottom of the screen

Screen Movement

Gmove to the last line in the file
xGmove to line x
z+move current line to top of screen
zmove current line to the middle of screen
z-move current line to the bottom of screen
^Fmove forward one screen
^Bmove backward one line
^Dmove forward one half screen
^Umove backward one half screen
^Rredraw screen ( does not work with VT100 type terminals )
^Lredraw screen ( does not work with Televideo terminals )

Inserting

rreplace character under cursor with next character typed
Rkeep replacing character until [esc] is hit
iinsert before cursor
aappend after cursor
Aappend at end of line
Oopen line above cursor and enter append mode

Deleting

xdelete character under cursor
dddelete line under cursor
dwdelete word under cursor
dbdelete 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
Pbring back before cursor
pbring back after cursor

Find Commands

?finds a word going backwards
/finds a word going forwards
ffinds a character on the line under the cursor going forward
Ffinds a character on the line under the cursor going backwards
tfind a character on the current line going forward and stop one character before it
Tfind a character on the current line going backward and stop one character before it
;repeat last f, F, t, T

Miscellaneous Commands

.repeat last command
uundoes last command issued
Uundoes all commands on one line
xpdeletes first character and inserts aftersecond (swap)
Jjoin current line with the next line
^Gdisplay current line number
%if at one parenthesis, will jump to its mate
mxmark current line with character x
'xfind 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

SUBSTITUTION

:#,#s/old/new/g
#line number range
oldpattern to replace
newpattern to insert
goptional key for global substitution
(multiple occurences of old on the same line will not be replace without this)

READING FILES

copies (reads) filename after cursor in file currently editing
:rfilename

WRITE FILE

:wsaves the current file without quitting

MOVING

:#move to line #
:$move to last line of file

SHELL ESCAPE

:!'cmd'executes 'cmd' as a shell command.