a quick vi survival guide

This is for when you get stuck with an abomination of an editor called vi. No sophisticated vi-foo here, this is just the basics that get you covered for simple editing of files.

modes

The Vi editor has two modes: Command mode and Insert mode:

  • When you open a file with Vi, you are in Command mode. In Command mode, you can use keyboard keys to navigate, delete, copy, paste, and do some other stuff – except entering text.
  • To enter Insert mode from Command mode, hit . In Insert mode, you can enter text, use the Enter key to go to a new line, and use the arrow keys to navigate text. To enter Command mode from Insert mode, hit .

Some docs and tutorials refer to a third mode, the last line mode, which is for file operations like save and exit. This mode can be entered from Command mode with , or from Insert mode with , followed by . This mode can be seen as a sub-mode of the Command mode.

Got stuck in vi? Hit Esc to make sure in Command mode.

Now, in Command mode, to quit without saving, type , followed by return.

command mode

command mode is entered with the key

i input mode (insert before cursor)
a input mode (insert after cursor)
x delete char under cursor
<num>x delete <num> chars from under cursor
dd delete actual line
<num>dd delete delete <num> of lines
u undo last command
J append next line to actual line
. repeat last command
: go into last line mode

command mode, file operations aka last line mode

:q! quit without save
:w [<file>] save as [<file>]
:x save and quit
:e <file> edit <file>
:u undo last command

navigation

Key Function
, h move one character to the left
, l move one character to the right
, k move one line up
, j move one line down
move one page up
move one page down

additional navigation keys in command mode

Key Function
h move one character to the left
l move one character to the right
k move one line up
j move one line down

search in Command mode

hit , then enter word to search for.

replace in Command mode

replace in line x to line y text1 with text2, without confirmation:

replace in line x to line y text1 with text2, with confirmation (note the at the end instead of ):

x