NEXT UP Introduction To vi
Next: Insert Mode

Edit Mode

The edit mode of vi is used mainly for moving the cursor around the file you are editing to find some position of interest. Once there, you can then cut and paste blocks of text, delete text and insert new text. You may then go back and move the cursor to the next position of interest, and so on.

Because vi doesn't rely on your keyboard having special keys (such as cursor control keys), in edit mode, many of the alphabetic keys and combinations of them, have particular functions. So, for example, when you have finished all your editing, and you want to save your editing changes and quit from the editor back to a shell prompt, you issue the ZZ command, which means just pressing the Z key twice in succession. Note that the editor is case sensitive so you must be sure to use ZZ here and not zz.

Positioning the Cursor

The main cursor movement keys in edit mode are provided by the up, down, left and right arrow keys, if your keyboard supports them. If your keyboard does not properly support the arrow keys then the same effect can be achieved with the following keys:

kup;
jdown;
hleft
lright.

Remember, again, that vi was written to run on a system which could support almost any kind of terminal. And some terminal types, especially older ones, just don't have arrow keys or any other special keys for that matter, other than the standard typewriter keyboard layout.

As well as these four commands, which only move the cursor by one line or one character at a time, vi also has commands to move the cursor by greater distances:

Ctrl-fmove forward one page in the file (page down);
Ctrl-bmove backward one page in the file (page up).

In fact, these commands move by just under a page to give some degree of continuity as you read through a file.

Once you have the page of interest on the screen you can quickly move the cursor round it with the commands:

Hmove the cursor to the home (top) line on the screen;
Mmove the cursor to the middle line on the screen;
Lmove the cursor to the last line on the screen.

Again, note the case of these letters. In the case of the H and L commands, they may be preceded by a number, which will move the cursor that many lines into the screen. So, the command 2H will position the cursor on the second line of the screen, while the command 3L will move the cursor to the third line from the bottom of the screen.

When you get to the right line in the file, positioning the cursor at the correct point on that line is easy:

wmove right to the start of the next word;
emove right to the end of the next word;
bmove left to the start of the previous word;
0(zero) move cursor left to the start of the line;
^move to the first non-whitespace character on the line;
$move cursor right to the end of the line.

When the w, e or b commands reach the end (or start) of a line they will wrap around onto the next (or previous) line.

String Searching

If you want to find an occurrence of a particular known word or phrase then you can have vi search for it directly for you rather than moving the cursor around the file and performing the search manually. To do this you type the forward slash (/) character followed by the string of characters to search for, followed by pressing Enter or Return. The editor will search forwards towards the end of the file looking for the specified string. If the string is encountered the editor will stop the search and position the cursor at the start of the string it has found. If this is not the occurrence of the string you were looking for then using the n command will search for the next occurrence of the string. It is also possible to search backwards through the file, towards the start, using the ? character instead of the /. Whichever direction you search in, when you reach the limit of the file, the search will wrap around to the other end of the file and continue on from there. In summary:

/stringsearch forward for given string;
?stringsearch backward for given string;
nsearch forward or backward for next occurrence of string.

Replace and Delete

With the cursor now located at the required position in the file, there are many options available. The main edit mode options allow you to replace the character the cursor is sitting on with another character or to delete one or more characters from the current cursor position:

rcreplace character at current cursor position with c;
xdelete character at current cursor position;
dwdelete the word to the right of the cursor
dbdelete the word before the current cursor;
dddelete the current cursor line and close the gap.

Putting a number before any of the commands in this block extends their function as follows:

nrcreplace n characters from cursor position with c
nxdelete n characters from cursor position;
ndwdelete n words to the right of the cursor;
ndbdelete the n words before the current cursor
ndddelete n lines and close the gap.

Other delete commands which can often be of use (but not with preceding numbers) are:

d$delete characters from current cursor to end of line;
d0delete characters from current cursor to start of line;
Jdelete CR at end of line to join this line to the next.

Cut and Paste

Whenever you delete characters, words or lines from your text, they are not just thrown away, but copied into a memory buffer. This can be very useful because there is a pair of commands which will allow you to paste the contents of this buffer back into your text, either into the same place (if you removed the text in error) or into some new location providing a standard cut and paste facility. The pair of commands is:

p(lower case) paste cut buffer after current cursor;
P(upper case) paste cut buffer before current cursor.

If the cut buffer contains either characters or words then the paste operations will occur before or after the current cursor position on the same line. If the cut buffer contains whole lines of text then the paste operations take place before or after the line on which the cursor currently sits.

There are several command pairs in vi (like p and P), where a lower and upper case pair provide similar functionality. In these cases, the lower case command is the one which operates after the current position, while the upper case version operates before the current position.

Sometimes you will want to make a copy of a block of text in a new location, rather than moving the block with cut and paste. All you really need for this is to be able to copy a block of text into the cut buffer without also deleting it from the file:

yycopy (yank) the current line into the cut buffer;
nyycopy (yank) n lines into the cut buffer.

Undo and Repeat

The two final edit mode commands to look at here are just for convenience. The first is for use if you make a mistake and you need to undo the ill effects of some command. The second is for the case where you have executed some command and you have now moved the cursor to the next position of interest and wish to repeat the same command again:

uundo the effect of the last command;
.repeat the last command to change the text.

NEXT UP previous
Next: Insert Mode