Old Content This page is now considerably out-of-date.
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:
|w||forward a word|
|b||back a word|
|$||end of line|
|0||beginning of line|
|G||end of file|
|<num>G||go to line num|
|0G||beginning of file|
|h,j,k,l||alternative cursor keys (left, down, up, right)|
Cut & paste
|dd||cut the current line|
|yy||copy the current line|
|dw||delete a word|
|D||cut from cursor until end of line|
|p||paste after cursor|
|P||paste before cursor|
Getting into insert mode
|i||insert text before cursor|
|a||append text after cursor|
|A||append text at end of line|
|cw||change a word|
|C||change from cursor to end of line|
|s||substitute character at cursor|
|.||repeats last operation. very good with 'dd'|
|<num><cmd>||do cmd repeated num times|
|r<chr>||replace a character without going into insert mode|
|ZZ||save 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.