Old Content   This page is now considerably out-of-date.
Using vi

Vi is a text editor often found on Unix systems. I don't particularly like it, and generally avoid using it, but it has the benefit of being almost always available.

The difference between vi and most other editors is it has two modes: command mode and insert mode. You start off in command mode, and usually press i to get into insert mode, where you can actually type. To get back to command mode, press escape. In some versions of vi it tells you at the bottom if you're in insert mode, otherwise you have to remember.

There's not much to say about insert mode - you just type. Here are the most common keystrokes I use in command mode:

Moving about

wforward a word
bback a word
$end of line
0beginning of line
Gend of file
<num>Ggo to line num
0Gbeginning of file
h,j,k,lalternative cursor keys (left, down, up, right)

Cut & paste

ddcut the current line
yycopy the current line
dwdelete a word
Dcut from cursor until end of line
ppaste after cursor
Ppaste before cursor

Getting into insert mode

iinsert text before cursor
aappend text after cursor
Aappend text at end of line
cwchange a word
Cchange from cursor to end of line
ssubstitute character at cursor

Others

.repeats last operation. very good with 'dd'
<num><cmd>do cmd repeated num times
r<chr>replace a character without going into insert mode
ZZsave and quit
:!<shell>runs a shell command

Points to note

  • If it all goes totally mad, you've probably got caps-lock on by mistake.
  • You can get out of the help with :q
  • Sometimes you paste into vi (in a x-term or putty window) and it misses part of the text. That's when you've done it in command mode - it starts doing your text as commands (which can wreak havoc) and drops into insert mode when it his an i, finally typing some of what you pasted.
© 1998 - 2012 Paul Johnston, distributed under the BSD License   Updated:13 Jul 2009