Essential and Handy Commands for the Text Editor vi (vim)

If for a key combination, something like nx, the n is missing one is assumed. E.g. to replace only a single character right under the cursor position in normal mode hit s followed by the replacement (can be more characters but only the first will be replaced). For most commands the optional number right before the command is somewhat a repeater to instruct how often the following should be executed.


insert at cursor i
insert at start of line I
.. end of line A
leave insert mode <ESC>
replace next (n) char(s) incl. cursor ns
repeat last (n) command(s) n.
insert n new lines after this no
insert n new lines before this nO
delete next n lines with current ndd
delete until end of line D
paste from cursor onwards nP
past after cursor np

Search and Replace

  1. Change to normal (navigation) mode with <ESC>
  2. Search (Wraped around at end of file):
    Search STRING forward : / STRING.
    Search STRING backward: ? STRING.
  3. Repeat search: n
    Repeat search in opposite direction: N (SHIFT-n)
  4. Replace: Same as with sed, Replace OLD with NEW:
    First occurrence on current line: :s/OLD/NEW
    Globally (all) on current line: :s/OLD/NEW/g
    Between two lines #,#: :#,#s/OLD/NEW/g
    Every occurrence in file: :%s/OLD/NEW/g


Tony Chen’s vi-manual
Felix Gers’s manual
vi reference on
RefCard on (pdf)