VI quick reference

This page originally came from: http://www.lip.pt/computing/services/systems/unix/editors/vi.htm

To use a vi command, move the cursor to the desired text and press the appropriate command key. To use an ex command, first type a colon :this automatically moves the cursor to last screen line (command line). Then, type the appropriate command sequence and press the RETURN key.

The vi and ex commands listed in this reference are organized by task. All ex commands are preceded by a colon :Yellow background areas indicate commands that let you enter text and that you end by pressing the ESC key. Brackets "[]" indicate optional information, while green text indicates user-specified information.

Commands whose description end in a + sign may be preceded by a number to indicate characters, words, or lines. For example de commads 3dw deletes the next three words.

Commands whose description end in a * sign may be preceded by a line number or line range (comma-separated) to indicate a line other than the current line or to affect multiple lines. For example the command 10,15m80 moves lines 10-15 after line 80.

Beginning a editor session

Edit a file vi [file]
Recover a edit session vi -r[file]

Controlling your screen display

Eliminate @ lines <CTRL/R>
Repaint current screen <CTRL/L>

Moving the cursor

Beginning of current line 0 or^
Beginning of first screen line H
Beginning of last screen line L
Beginning of middle screen line M
Down one line j,<RETURN>,+
End of current line $
Left one character h,<CTRL/H>
Left to beginning of word b orB
Right one character i, <SPACE>
Right to end of word e orE
Right to beginning of word w orW
Up one line k or-
Beginning of next sentence )
Beginning of previous sentence (
Display cursor position and file information <CTRL/G>

Paging through text

Back one screen <CTRL/B>
Down half screen <CTRL/D>
Forward one screen <CTRL/F>
Forward to end of file G
Move cursor to the specified line * line no. G
Up half screen <CTRL/U>

Searching through text

Backward for pattern ?pattern
Forward for pattern /pattern
Repeat pattern search n
Reverse direction of search N

Creating text

Append text after cursor a
Append text after end of line A
Insert text before cursor i
Insert text at beginning of line I
Open new line after current line o
Open new line before current line O
Take next character literally and display <CTRL/V>

Modifying text

Change current word cw orcW
Change current line (cursor to end) C
Delete character (cursor forward)* x
Delete character (before cursor)* X
Delete word* dw or dW
Delete line dd
Delete text to end of line D
Duplicate text (use yank and put)
Join current line with next line* J
Move text (use delete and put)
Put buffer text after/below cursor p
Put buffer text before/above cursor P
Repeat last modification command .
Replace current character* r
Replace text to end of line* R
Substitute text for character* s
Undo your previous command u
Transpose characters xp
Yank (copy) word to buffer* yw
Yank (copy) current line to buffer* Y
Change case one character ~
Yank (copy) NUMBER lines to buffer numberY

Making corrections during text insertions

Overwrite last character <DELETE>
Overwrite last word <CTRL/W>

Other options

Repeat last command .
Change to ex editor Q

Ending your edit session

Quit (no changes made) :q
Quit and save changes ZZ or:wq
Quit and discard changes :q!

Using alternate excommands

Change back to vi :vi
Copy specified lines+ :co ort
Display number of each line :set nu
Move lines after specified line+ :m
Read file in after specified line* :r file
Review current editor options :set
Review editor options :set all
Set new editor option :set option
Write changes to original file + :w
Write changes to specified file + :w file
Write (overwrites) to existing file + :w! file
Replace a text string in the current line :s/text/newtext/g
Replace a text string in the whole file :1,$s/text/newtext/g