vi CHEAT SHEET General
Edit
Vi begins in command mode, where the cursor movement and text deletion and pasting occur. Insertion mode begins upon entering an insertion or change command. [ESC] returns to command mode.
Prefix commands below with single lowercase letter to use specific buffer instead of general clipboard.
Most commands execute as soon as you type them, "colon" commands execute when you press the return key.
c<Move> C cc S s
Prefix most commands with a number to repeat it.
Quit :q :q! :w :w :wq ZZ
quit (fails if anything has changed) quit and throw away changes write (save) the file save as write (save) and quit write (save) and quit
C-d C-u C-f C-b z.
scroll down half screen scroll up half screen page forward (down) page backward (up) scroll window, cursor in the middle
Scroll
Move Arrow and navigation keys can be used as well. h j k l
left down up right
or or or or
left arrow down arrow up arrow right arrow
w W e E b B
next word next word - blank delimited end of word end of word - blank delimited previous word previous word - blank delimited
0 ^ $
(zero) start of line first non-blank character of line end of line
G 1G
go to last line go to 1st line
H M L
top of screen middle of screen bottom of screen
%
associated () [] {}
Windows :e :sp :bn :bp :bd C-w s C-w v C-w w C-w<Move> C-w q
Edit a file in a new buffer Open a file in a new buffer and split window go to next buffer in same window go to previous buffer in same window delete a buffer (close a file) Split windows Split windows vertically switch between windows switch to window in direction of Move Quit a window
Insert
Change “c”+<Move> commands delete and enter insert mode. delete text specified by Move, enter insert mode change to end of line (same as c$) change the whole line change the whole line (same as cc) delete character at cursor and subsitute text
Delete “d”+<Move> commands delete text specified by Move. d<Move> x X D dd :d J
delete text specified by Move delete character at cursor (Del) delete character before cursor (Backspace) delete to end of line delete the whole line delete the whole line join line below to this (change LF to space)
Yank “y”+<Move> commands copy text specified by Move y<Move> yy :y
yank text specified by Move yank the whole line yank the whole line
Put commands paste the clipboard. p P
put (paste) the clipboard AFTER cursor put (paste) the clipboard AT cursor
Visual Start a visual selection, then type a command. v<Move> V<Move> C-v<Move> o O Esc
start visual mode start visual mode, whole lines only start visual block mode move to opposite end of marked area move to other corner of block horizontally exit visual mode
Search General form [/?]pattern[/?] /pattern ?pattern?-2 //e / n N
search forward for pattern search backward for pattern, go to 2 nd line above repeat last search pattern, go to end of match repeat last search pattern with last offset repeat search in same direction repeat search in opposite direction
Replace General form :[range]s/pattern/string/[flags] :%s/old/new/ replace first in each line of file :%s/old/new/gc replace all with confirmations :.,$s/foo/bar/gi replace all downwards, ignore case :.-10,.+10s/foo/bar/g replace all within 10 lines distance & repeat last replace command without flags :&& repeat last replace command with flags
Regexp * \? \+
zero or more zero or one one or more
u .
undo redo
~ g~<Move>
toggle case toggle case of characters specified by Move
> < :[range]>[n] :[range]<[n]
indent unindent indent n lines in range unindent n lines in range
Other
vi will remain in insert- or replace mode until pressing ESC. i I a A o O
start insert mode at cursor insert at the beginning of the line append after the cursor append at the end of the line open (append) blank line below (no LF needed) open blank line above current line
r R
replace single character, stay in command mode start replacing many characters at cursor