Unix vi Editor Commands
***** UNIX vi EDITOR COMMANDS ***** Modified 18 Dec 91
COLON (ex) COMMANDS
:e file edit a different file
:n :n! next file (i.e. vi fn1 fn2) / next file without writing this one
:r file read (merge) a file
:set ai set auto indent
:set nu set line numbers on (show)
:set nonu set line numbers off (hide)
:set sw=n set shift width to n (affects shift right >> and shift left <<)
:set wm=n set wrap margin to n (0 is no wrap, 10 wraps after 70 i.e. 80-10)
:!command shell: execute a normal shell command (see spell check)
:q :q! :wq quit / quit without writing (saving) / write and quit
:w :w fn write (save) / write to a file name
:s/old// delete the 1st occurrence of "old"
:%s/old//g delete every occurrence of "old" in the file
:s/^/new insert "new" at beginning of current line
:s/$/new insert "new" at end of current line
:s/old/new replace the 1st "old" with "new" in the current line
:s/old/new/g replace every "old" with "new" in the current line
:%s/old/new replace the 1st "old" on every line in the file with "new"
:%s/old/new/g replace every "old" with "new" on every line in the file
:.,.+5s/old/new/g replace "old" with "new" on this line + the next 5 lines
:g/old/s//new/cp replace 1st "old" in every line of file, confirm changes
:g/old/s//new/gcp replace every "old" in every line of file, confirm changes
:!spell doc +dict spell check the file "doc", use "dict" as extra dictionary
CURSOR MOVEMENT (A ^ alone is a caret. A ^ by a letter means hold control.)
0 ^ beg. of line/of text of line | backspace left k up space right
+ CR beg. of next line | h left j down l right
- beg. of previous line | ^B ^U back (up) full/half page
$ G end of line/end of file | ^F ^D forward (down) full/half page
#G goto specific line # (e.g. 15G) | H M L jump hi/middle/low
) ( sentence: forward/backward one sentence
w b e words: (w,b) forward/backward one word, (e) end of word
W B E words: (W,B) forward/backward one word, (E) end of word (ignores punct.)
SCREEN COMMANDS
^L clear screen and redraw text ^G show file name, status, and line #
EDITING COMMANDS (Many can use repetition factors. 5dw deletes 5 words, etc.)
Ctrl-W backspace over a word (used in insert mode)
cc S change: clear current line, begin insert mode (. repeat last change)
Ctrl-V control characters allowed as input (used in insert mode)
yy copy: yank a line (makes a copy for pasting and does not delete)
"ayy copy: yank 5 lines to the named buffer 'a'
x X delete this/previous character
dw dd D delete word/line/end of line
fx Fx find next/previous 'x'
tx Tx find next/previous 'x' (stops 1 short)
; , find: repeat previous inline search in same/opposite direction
i a insert before/after current character
I A insert at the front/back of the current line
O o insert: open a blank line before/after the current line
J join: join the next line to the end of the current line
P p paste: put deleted text before/after current position
"aP "ap paste: put text from named buffer 'a' before/after current position
Q vi quit to "ex" (line editor) from "vi"/reenter "vi" from "ex"
r R replace (overwrite) one/several character(s)
/ ? search: forward/backward word search
n N search: repeat search in same/opposite direction (next)
<< >> shift left/right (Use :set sw=n to set shift width to n.)
u U undo last change/undo all changes made on the current line
Resources
vi Cheat
Sheet (pdf), K Computing, note in .pdf file says "Copy freely."
vi Editor Cheat
Sheet, University of the Virgin Island, Center for Administrative Computing
|