Simplified vi

This is a small subset of vi key strokes and commands, which cover most of the sorts of things you might want to do when editing a text file. They were selected from the large array of vi key strokes and commands as they are fairly easy to remember, and combine consistantly together. Some of the combinations have defined vi shortcuts, but to keep consitancy with the subset, these generally aren't shown.

Modes

Vi uses modes to control what keypresses will do. It defaults to command mode, and the other modes branch off from that mode i.e. the keypresses below are done when in command mode to move to one of the other modes pressing the Esc key in any of the other modes, takes you back to command mode:

Esc - command (normal) mode

: - ex mode

/ - search in file forward from cursor

? - search in file backward from cursor

i - enter insert mode (before cursor - insert)

a - enter insert mode (after cursor - append)

R - replace mode

Command Mode

Moving about

g - left (also left arrow key)

h - right (also right arrow key)

j - down (also down arrow key)

k - up (also up arrow key)

Motion commands

l - current letter

w - to begining of next word

b - to begining of current, or previous, word

^ - to begining of the current line

$ - to end of the current line

% - to a matching opening or closing parenthesis, square bracket or a curly brace: ([{}])

Commands

x - delete character

J - join lines

p - paste after current position

P - paste before current position

u - undo

Ctrl-R - redo

Operators

d - delete (cut)

y - copy (yank)

c - change (also enters insert mode, so you need to press Esc to get back into command mode once you've finished the change)

gu - make lowercase

gU - make uppercase

The above operators act on lines by repeating the operator e.g. "dd" deletes the current line, "yy" copies (yanks) the current line, "cc" changes the current line, "gUgU" makes the current line all uppercase.

Actions

You combine operators with motion commands to do actions i.e. <operator><motion command>:

dw - delete to begining of next word

d$ - delete to end of the current line

cw - change up to begining of next word

yl - copy current letter

y$ - copy everything to end of the current line

gUw - make current word uppercase

You can also precede an action with a motion command, to put the action at the right point to do certain actions e.g. to delete the current word if the cursor is in the middle of the word then:

bdw - go back to the begining of the word, and delete current word

You can also precede an action with a number to say how many it should do i.e. <number><operator><motion command>:

10yy - copy the next 10 lines

3dw - delete the next 3 words

ex Mode

These are usually represented by adding the colon ":" (which shifts to ex mode) before the command, as once the command is executed, vi returns to command mode:

:w - save (write)

:q - quit

:q! - quite without saving

:wq - save and quite

Search and replace

:s/xxx/yyy/ - Substitute (replace) xxx with yyy at the first occurrence in current line

:s/xxx/yyy/g - Substitute xxx with every yyy occurrence (global) in current line

:{range}s/xxx/yyy/ - Do substutution for all lines in range

:34,78s/xxx/yyy/ - Do substitution for lines 34 to 78

:10,15s/xxx/yyy/g - Do substitution, all occurances (global), for lines 10 to 15

:1,$s/xxx/yyy/ - Do substitution for lines 1 to end of file

:1,10y - Copy (yank) lines 1 to 10 of the file

:16,24d - Delete (cut) lines 16 to 14 of the file

:34,.y - Copy from line 34 to current line (inclusive)

xxx and yyy can be complex regular expressions (regex), not just simple text.

Insert other contents into current file

Use the :r (read) command to insert a file, or the output from a system command, into the current file.

:r /path/to/file - Insert the file below the cursor

:r !cmd - Insert the output from cmd below the cursor e.g. ":r !ls" will insert the directory listing

Document navigation

:g/xxx/ - Shows an index of all the lines that match the xxx regex e.g. ":g/^#/" will match all lines that start with a #

:g/xxx/# - Shows an index, with lines numbers, of all the lines that match xxx regex, just type ":{line number}" to jump to the line number

While in normal mode the % key can be used for the following :

Spell checking

:help spell - spell help

:set spell - set spell checking on

:set nospell - turn spell checking off

While in normal mode when spell checking is enabled:

]s - next misspelled word

[s - previous misspelled word

z= - list of possible corrections

zg - add current word to personal dictionary

zug - remove current word from personal dictionary

Search in file

In normal mode you can use * and # to search for a word under the cursor. * searches forward for the word, while # searches backwards.

/pattern - search forwards, from current position, for first occurence of "pattern" (case sensitive)

?pattern - search backwards, from current position, for first occurence of "pattern" (case sensitive)

While searching "n" will search forward for the next occurence, and "N" will search backwards.