A Reference for the VI Editor

Please consider helping us maintain this site by making a donation:

Visit our new forum!


Click here for Tips and Tricks!

Cursor  Lines
h move left [RETURN] First non-blank character of next line
l move right 0, $ First, last position of current line
j move down ^ First non-blank character of current line
k move up +, - First non-blank character of next, previous line
w, W/b, B move forward/backward by word n| Column n of current line
e move to end of word H Top line of screen
E move to end of word (includes punctuation) M Middle line of screen
), ( beginning of next, previous sentence L Last line of screen
}, { beginning of next, previous paragraph nH n (number) of lines after top line
]], [[ beginning of next, previous section (normally defined by an opening '{') nL n (number) of lines before last line
Scrolling

Searches

[CTRL-F], [CTRL-B] Scroll forward, backward one screen /pattern Search forward for pattern
[CTRL-D], [CTRL-U] Scroll down, up one-half screen ?pattern Search backward for pattern
[CTRL-E], [CTRL-Y] Scroll down, up one line n, N Repeat last search in same, opposite direction
z [RETURN] Scroll so that line with cursor is at top of screen /, ? Repeat previous search forward, backward
z . Scroll so that line with cursor is at middle of screen fx Search forward for character x in current line
z - Scroll so that line with cursor is at bottom of screen Fx Search backward for character x in current line
[CTRL-L] Redraw screen (without scrolling) tx Search forward to character before x in current line
Tx Search backward to character after x in current line
Line number

; Repeat previous current-line search

[CTRL-G]

Display current line number , Repeat previous current-line search in opposite direction
nG Move to line number n :1,$s/x/y/g Globally replace x with y **
G Move to last line in file
:n Move to line n in file Marking position

mx Mark current position as x
Insert

`x Move cursor to mark x
i, a Insert text before, after cursor `` Return to previous mark or context
I, A Insert text before beginning, after end of line 'x Move to beginning of line containing mark x
o, O Open new line for text below, above cursor ' ' Return to beginning of line containing previous mark
Delete/Move

Change

x Delete character under cursor r Replace character
X Delete character before cursor cw Change word
dw Delete word cc Change current line
dd Delete current line cmotion Change text between the cursor and the target of motion
dmotion Delete text between cursor and the target of motion C Change to end of line
D Delete to the end of line R Type over (overwrite) characters
p, P Put deleted text after, before cursor s Substitute: delete character and insert new text
"np Put text from delete buffer number n after cursor (for last nine deletions) S Substitute: delete current line and insert new text
Exit commands

Yank

ZZ Write (save) the file if modified, and quit file yw Yank (copy) word
:x Write (save) the file if modified, and quit file yy Yank current line
:wq Write (save) the unconditionally, and quit file "ayy Yank current line into named buffer a (a-z).  Uppercase names append text
:w Write (save) file ymotion Yank text between the cursor and the target of motion
:w! Write (save) file, overriding protection p, P Put yanked text after ,before cursor
:30, 60w newfile Write from line 30 through line 60 and append to file "aP Put text from buffer a before cursor (a-z)
:30, 60w>>file Write from line 30 through line 60 and append to file
:w %.new Write current buffer named file as file.new ex edit commands

:q Quit file :d Delete lines
:q! Quit file, overriding protection :m Move lines
Q Quit vi and invoke ex :co or :t Copy lines
:e file2 Edit file2 without leaving vi :.,$d Delete from current line to end of file
:r newfile Read contents of newfile into current file :30, 60m0 Move lines 30 through 60 to top of file
:n Edit next file :.,/pattern/co$ Copy from current line through line containing pattern to end of file
:e! Return to version of current file at time of last write (save)
:e # Edit alternate file Other commands

:vi Invoke vi editor from ex . Repeat last edit command
: Invoke one ex command from vi editor u, U Undo last edit; restore current line
% Current filename (substitutes into ex command line) J Join two lines
# Alternate filename (substitutes into ex command line)
^] Look up the location of the identifier under the cursor in the tags file, and move to that location.  If tag stacking is enabled, the current location is automatically pushed onto the tag stack
^T Return to the previous location in the tag stack, i.e. pop off one element

** :1,$s/x/y/g
Globally replace x with y

The colon makes you enter command mode
The 1 says start with line 1
The , says and continue to
The $ says all the way to the end of the document
The s says search
The first set of brackets are what you are searching for
The second set is what you want to replace it with
The g says do it globally