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.

Essentials

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

References:

Tony Chen’s vi-manual
Felix Gers’s manual
vi reference on pages.de
RefCard on digilife.be (pdf)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: