vi commands

vi editor comes by default with UNIX operating system. The vi editor has two modes:

  • Command mode: action to be taken on the file for different commands
  • Insert mode: entered text is inserted into the file

Every character typed in the command mode does something to the text of the file being edited.

Every character typed in the insert mode is added to the text in the file

Start vi editor:

vi filenameedit filename starting at line 1
vi -r filenamerecover filename that was being edited when system crashed

Exit vi editor:

:x<Return> quit vi, writing out modified file
to file named in original invocation
:wq<Return> quit vi, writing out modified file
to file named in original invocation
:q<Return> quit (or exit) vi
:q!<Return> quit vi even though latest changes
have not been saved for this vi call

Cursor moving:

j or <Return>

  [or down-arrow]
move cursor down one line
k [or up-arrow] move cursor up one line
h or <Backspace>

  [or left-arrow]
move cursor left one character
l or <Space>

  [or right-arrow]
move cursor right one character
0 (zero) move cursor to start of current line
(the one with the cursor)
$move cursor to end of current line
wmove cursor to beginning of next word
bmove cursor back to beginning of preceding word
:0<Return> or 1G move cursor to first line in file
:n<Return> or nG move cursor to line n
:$<Return> or G move cursor to last line in file

vi cheatsheet

Reference: