Vi(m) Quick Reference

Vi(m) command-line options

Vi(m) modes

Vi(m) commands

Move the cursor
Key strokesMeaningNotes
Arrow keysMove the cursor by one character/line
hMove the cursor to the left character
jMove the cursor to the character below it
kMove the cursor to the character above it
lMove the cursor to the right character
bMove the cursor to the begining of the previous word
wMove the cursor to the begining of the next word
$ or EndMove the cursor to the end of the line
^Move the cursor to the first non-blank character of the line
0 or HomeMove the cursor to the beginning of the line
Ctrl + dShow the next half screen'd' is short for 'down'.
Ctrl + uShow the previous half screen'u' is short for 'up'.
Ctrl + f or Page DownShow the next screen'f' is short for 'foward'.
Ctrl + b or Page UpShow the previous screen'b' is short for 'back'.
number GMove the cursor to the line with line number numberIf you don't specify number, the cursor is moved to the end of the file
Insert characters (switch to insert mode)
iInsert characters before the cursor
IMove the cursor to the beginning of the line and insert characters before the cursor
aAppend characters after the current location of the cursor
AMove the cursor to the end of the line and append characters
oCreate a blank line after the current line to insert characters
OCreate a blank line before the current line to insert characters
Delete characters
xDelete the character at the location of the cursor
d wDelete the characters from the cursor to the end of the word
d bDelete the characters from the beginning of the word to the cursor
d dDelete the current line
(book)Marks
m markSet a (book)mark mark at the cursorThere are quite a few (book)marks to use (a - z, 0 - 9, ', ", [, and ]). The current location to the (book)mark can be used a text unit in delete, copy, etc.
` markMove the cursor to (book)mark markYou use backtick symbol
' markMove the cursor to the begining of the line with (book)mark markYou use single quote symbol
:marks [ mark ]Show all the (book)marks or a specific (book)mark mark
Replace and change characters
r xReplace the current character with xStay in command mode
ROverwrite charactersSwitch to insert mode, but overwrite old contents
c wDelete the characters the cursor to the end of the word and switch to insert mode
c bDelete the characters from the beginning of the word to the cursor and switch to insert mode
c cDelete the current line and switch to insert mode
Undo and redo
uUndo
Ctrl + rRedo
.Repeat last delete or change operationThe last delete or change operation can be performed in a different location
Save and exit
:w [ filename ]Save the file
:w >> filenameAppend the contents to the end of the file
:qExit vi(m) program
:q!Force vi(m) program to exit (discard any changes)
:wq or :ZZSave the contents and exit vi(m) program
Work with multiple files
:vi filenameOpen another file in vi to edit
:view filenameOpen another file in vi in read-only mode
:nextSwitch to the next opened file in viSuppose you pass multiple filenames to vi in the command line.
:previous or :NextSwitch to the previous opened file in viSuppose you pass multiple filenames to vi in the command line.
:first or :rewindSwitch to the first opened file in vi
:argsShow all the opened filesSuppose you pass multiple filenames to vi in the command line. The current file's name is enclosed in square bracket
Other editint commands
JJoin the current line with the next one
~Change the character's case
Find and replace
f letterSearch to the right in the same line for letter'f' is short for 'forward'
F letterSearch to the left in the same line for letter
t letterSearch to the right in the same line for the character before letter't' is short for 'till'
T letterSearch to the left in the same line for the character after letter
/patternSearch from the current location to the end of the file for patternAfter typing '/', you can use Up and Down arrow keys to select a recent search
?patternSearch from the current location to the beginning of the file for pattern
nRepeat the search in the same directionThe direction is determined by the previos search operation. If you use '/', the same direction is towards the end of the file; if you use '?', the same direction is towards the begnining of the file.
NRepeat the search in the reverse direction
:set hlsearchTurn on highlighting matched strings
:set nohlsearchTurn off search highlighting
:nohlsearchClear the current highlighting
:1,$s/search-string/replace-string[/g]Substitute search-string with replace-string throughout the file'g' means global replacement in each line
Copy and paste
y yCopy the current line to the anonymous clipboard'y' is short for 'yank'. BTW, any delete operation moves the deleted characters to the anonymous clipboard.
p or PPaste the characters from the anonymous clipboard after/before the current location
" letter y wCopy the characters from the current location to the end of the word to a named clipboardThe name of the clipboard is letter. You have 26 named clipboards (a-z) to use. The key w can be replaced with other keys, as explained below.
" LETTER y wAppend the characters from the current location to the end of the word to a named clipboardThe name of the clipboard is letter. Here, you still have 26 named clipboards (a-z) to use, but you need to use an uppercase letter (A-Z). The key w can be replaced with other keys, as explained below.
" letter p or " letter PPaste the characters from the named clipboard (a-z) after/before the current location
Work with multiple windows
[ lines ] :split [ +command ] [ filename ]Open a new windowSpecify a filename to open a new file in the new window. You can also specify the size of the new window as the number of lines, and run some initial command
Ctrl-w wSwitch to a different window
Ctrl-w +Increase the current window by 1 line
Ctrl-w -Decrease the current window by 1 line
Ctrl-w =Make windows the same size
[ lines ] Ctrl-w _Maximize the current window without line numbers. Otherwise, set the window size to the line number
Other commands
:help [ subject ]Show vi(m) help screenPress :q or :ZZ to exit help screen
!commandRun a UNIX/Linux command in shell forked from vi(m)
!text-unit commandRun a UNIX/Linux filter command on the selected text and replace them with the output of the commandEg., type !10Gsort to sort the lines from first line to line 10. !! runs the current line through a filter command.
:set showmodeDisplay the mode of vi
:set nu or :set numberDisplay the line number
:set nonu or :set nonumberHide the line number

General format of vi editing command

Text unit keyMeaning
bTowards the begining of the word
wTowards the end of the word
(Towards the begining of the sentence
)Towards the end of the setence
{Towards the begining of the paragraph
}Towards the end of the paragraph
letterThe same letter means the current line, like d d

Using macros in vi(m)

  1. In command mode, press q letter to start recording. letter is the name of the macro, which could be a - z, so you have 26 macros to use;
  2. Type any vi(m) commands;
  3. Press q to stop recording;
  4. press @ letter to run the macro.

References

  1. Linda Lamb and Arnold Robbins, Learning the vi Editor, 6th edition
  2. Steve Oualline, Vim Book