vi is one of the most powerful editors in the world, and with this power comes complexity. I've been using vi for about 8 years now, and I still don't know all of the features that come with it. I personally use 'vim' which is 'Vi IMproved' which has quite a few more features and functionality than startnard vi. This page will teach you the basics of how to get around the editor.

There are two modes: command and input. While in input mode, everything you type goes into the document that you are creating. While in command mode, your keystrokes are interpreted by vi as commands that it should execute on the document that you are creating. You will always start in command mode when you edit a file. There are a few ways to get into insert mode. Here are the ones that I know of off the top of my head:

To get out of insert mode (and return to command mode) hit [ESC]. In vim you can use your arrow keys while inserting or commanding to move the cursor around.

To start editing a file, there are two ways to go about it:

Navigation:
You can use the arrow keys to move one character/line at a time in either insert or command mode. The following keystrokes move the cursor around while in command mode:
CTRL-F Forward a page
CTRL-B Back a page
w Move to the start of the next word.
b Move to the start of the previous word.
W Move to the start of the next word following a whitespace.
B Move to the start of the previous word preceeding a whitespace.
0 Move to the start of the line.
$ Move to the end of the current line.
:0 -OR- gg Move to the first line of the file.
G Move to the last line of the file.
:# (e.g.: :12) Move the #th line of the file. (e.g. Move to the 12th line of the file.)
H Top of the screen.
M Middle of the screen.
L Bottom of the screen.

Command Mode Commands:
There are quite a few commands (too many to list here) that are executed by just pressing the appropriate key (like 'w' for go forward a word.) There are some commands that require a ':', or '/', or '?' to execute properly. Where they are needed, I will list them. If you do not see a ':' before a command, do not type ':'. There is a difference between 'w', ':w', '/w', and '?w'.

Saving and Quitting:
:w Save document
:w [filename] Save document to [filename]
:w! Save over a read-only file.
:q Quit
:q! Quit and discard changes.

Searching:
To search for text, you must be in command mode. You can search by doing '/text'. By doing '/text' and hitting enter, you will find the next occurance of 'text' in the document. Searching for 'text' would find the world 'text' and the word 'textile' as well. To search for the next occurance of your previous search, you just need to hit 'n' (no colon or slash) for it to go to the next one. If you want to find the previous occurance, then hit 'N'. To search backwards in a file you can do '?text' (not '/?text'.) Since you are now searching backwards, the 'n' and 'N' keys are swapped. 'n' will now go backwards through the document, and 'N' will go forwards through the document.

Undo:
To undo the last change, go into command mode and hit 'u'. There is a limit to how many changes you can undo, but I don't think that I've ever hit that limit. If you accidentally undo something that you didn't want to undo, you can enter ':redo' and your changes will be put back into place. If you've really messed up a document, and you want to start over where you last saved, you can do ':e!' and the file will be reloaded.

Changing Existing Text:
You can enter 'r' followed by another character to replace the character that the cursor is currently sitting on. If I had the word 'Kameron', and I wanted to change it to 'Cameron', I would put the cursor on the 'K', and then (in command mode) hit 'rC' to 'r'eplace the 'K' with the 'C'. You can also just go into insert mode, backspace over the 'K' and type a 'C'. Do whichever feels the mode natural to you. To enter insert mode so that everything you type OVERWRITES what is in the document, you can do 'R' and start typing.

Deleting Text:
x Delete character under cursor.
X Delete character to the left of the cursor.
D Delete to the end of the line.
dd Delete entire line.
d[motion] Delete from cursor to the end of the motion. Examples:
  • dG (Delete from cursor to end of file.)
  • d/text (Delete from cursor to next instance of 'text'.)
  • dw (Delete from cursor to the start of the next word.)
:X,Yd Delete from line X to line Y. (e.g.: ':10,20d' deletes lines 10 through 20.)

Copy and Pasting:
The act of 'copying' in vi is called 'yanking'. To 'yank' text, use one of the following commands:
yy Yank entire current line.
y$ Yank to end of line.
y[motion] Yank from cursor to the end of the motion. Examples:
  • yG (Yank from cursor to end of file.)
  • y/text (Yank from cursor to next instance of 'text'.)
  • yw (Yank from cursor to the start of the next word.)
To paste text that has been yanked, you can do:

Repeating Commands:
You can do commands multiple times by preceeding the command with a number. Let's say you want to delete the next 4 lines of a document. You can do 'dd' four times, or you can type '4dd'. Here are some examples of how to use repeating commands:
10x Delete 10 characters.
5yy Yank the next five lines.
10p Paste what was yanked 10 times.
40i=[ESC] Insert 40 equals signs.
3/text Find the third occurance of the word 'text'.

Aborting Commands:
Say you want to delete four lines from a document, and you accidentally type '44d' and you realize your mistake before hitting that last 'd'. You can hit [ESC] to clear the command buffer and start over. Once a command has already executed, the only way back is to do 'u' for undo.

Splitting and Joining Lines:
i[ENTER][ESC] Splits a line at the cursor.
J Joins current and next line together.
10J Joins next 10 lines together.

Search and Replace:
:s/foo/bar/ Replaces 'foo' with 'bar' on current line.
:%s/foo/bar/ Replaces 'foo' with 'bar' on EVERY line in the document.

Visual Mode:
Visual mode allows you to highlight text so that it can be yanked or deleted.
v Start visual mode in character mode.
V Start visual mode in line mode.
y Yank highlighted text.
d Delete highlighted text.
[ESC] Cancel visual mode with no changes.

Last Modified: 2006-04-19

Copyright © 2006-2008 - John Evans