Like emacs-mode, vi-mode essentially creates a one-line editing window into the history file. Vi-mode is popular because vi is the most standard UNIX editor. But the function for which vi was designed, writing C programs, has different editing requirements from those of command interpreters. As a result, although it is possible to do complex things in vi with relatively few keystrokes, the relatively simple things you need to do in the Korn shell sometimes take too many keystrokes.
Like vi , vi-mode has two modes of its own: input and control mode. The former is for typing commands (as in normal Korn shell use); the latter is for moving around the command line and the history file. When you are in input mode, you can type commands in and hit RETURN to run them. In addition, you have minimal editing capabilities via control characters, which are summarized in Table 2.6 .
Note that at least some of these-depending on which version of UNIX you have-are the same as the editing commands provided by UNIX through its terminal interface.  Vi-mode will use your "erase" character as the "delete previous character" key; usually it is set to DEL or [CTRL-H] (BACKSPACE). [CTRL-V] will cause the next character you type to appear in the command line as is; i.e., if it is an editing command (or an otherwise special character like [CTRL-D] ), it will be stripped of its special meaning.
Under normal circumstances, you just stay in input mode. But if you want to go back and make changes to your command line, or if you want to recall previous commands, you need to go into control mode. To do this, hit ESC .
A full range of vi editing commands are available to you in control mode. The simplest of these move you around the command line and are summarized in Table 2.7 . Vi-mode contains two "word" concepts. The simplest is any sequence of non-blank characters; we'll call this a non-blank word . The other is any sequence of only alphanumeric characters (letters and digits) or any sequence of only non-alphanumeric characters; we'll just call this a word . 
All of these commands except the last three can be preceded by a number that acts as a repeat count. The last two will be familiar to users of UNIX utilities (such as grep ) that use regular expressions, as well as to vi users.
Time for a few examples. Let's say you type in this line and, before you hit RETURN, decide you want to change it:
$ fgrep -l Bob < ~pete/wk/names
As shown, your cursor is beyond the last character of the line. First, type ESC to enter control mode; your cursor will move back one space so that it is on the s . Then if you type h , your cursor will move back to the e . If you type 3h from the e , you will end up at the n .
Now we will see the difference between the two "word" concepts. Go back to the end of the line by typing $ . If you type b , the word in question is "names", and the cursor will end up on the n :
$ fgrep -l Bob < ~pete/wk/n ames
If you type b again, the next word is the slash (it's a "sequence" of non-alphanumeric characters), so the cursor ends up over it:
$ fgrep -l Bob < ~pete/wk/ names
However, if you typed B instead of b , the non-blank word would be the entire pathname, and the cursor would end up at the beginning of it-that is, over the tilde:
$ fgrep -l Bob < ~ pete/wk/names
You would have had to type b four times-or just 4b -to get the same effect, since there are four "words" in the part of the pathname to the left of /names : wk , slash, pete , and the leading tilde.
At this point, w and W do the opposite: typing w gets you over the p , since the tilde is a "word", while typing W brings you to the end of the line. But whereas w and W take you to the beginning of the next word, e and E take you to the end of the current word. Thus, if you type w with the cursor on the tilde, you get to:
$ fgrep -l Bob < ~p ete/wk/names
Then typing e gets you to
$ fgrep -l Bob < ~pete /wk/names
And typing an additional w gets you to:
$ fgrep -l Bob < ~pete/ wk/names
On the other hand, E gets you to the end of the current non-blank word-in this case, the end of the line. (If you find these commands non-mnemonic, you're right. The only way to assimilate them is through lots of practice.)
Now that you know how to enter control mode and move around on the command line, you need to know how to get back into input mode so you can make changes and type in additional commands. A number of commands take you from control mode into input mode; they are listed in Table 2.8 All of them enter input mode a bit differently.
Most likely, you will use either i or a consistently, and you may use R occasionally. I and A are abbreviations for 0i and $a respectively. To illustrate the difference between i , a , and R , say we start out with our example line:
$ fgrep -l Bob < ~pete/wk/ names
If you type i followed by end , you will get:
$ fgrep -l Bob < ~pete/wkend/ names
That is, the cursor will always appear to be under the / before names . But if you type a instead of i , you will notice the cursor move one space to the right. Then if you type nick , you will get:
$ fgrep -l Bob < ~pete/wk/nickn ames
That is, the cursor will always be just after the last character you typed, until you type ESC to end your input. Finally, if you go back to the n in names , type R instead, and then type task , you will see:
$ fgrep -l Bob < ~pete/wk/tasks
In other words, you will be replacing (hence R ) instead of inserting text.
Why capital R instead of lowercase r ? The latter is a slightly different command, which replaces only one character and does not enter input mode. With r , the next single character overwrites the character under the cursor. So if we start with the original command line and type r followed by a semicolon, we get:
$ fgrep -l Bob < ~pete/wk; names
If you precede r with a number N , it will allow you to replace the next N existing characters on the line-but still not enter input mode. Lowercase r is effective for fixing erroneous option letters, I/O redirection characters, punctuation, etc.
Now that you know how to enter commands and move around the line, you need to know how to delete. The basic deletion command in vi-mode is d followed by one other letter. This letter determines what the unit and direction of deletion is, and it corresponds to a motion command, as listed previously in Table 2.7 . Table 2.9 shows some commonly-used examples.
These commands have a few variations and abbreviations. If you use a c instead of d , you will enter input mode after it does the deletion. You can supply a numeric repeat count either before or after the d (or c ). Table 2.10 lists the available abbreviations.
Most people tend to use D to delete to end of line, dd to delete an entire line, and x (as "backspace") to delete single characters. If you aren't a hardcore vi user, you may find it difficult to get some of the more esoteric deletion commands under your fingers.
Every good editor provides "un-delete" commands as well as
delete commands, and vi-mode is no exception. Vi-mode maintains
a delete buffer
that stores all of the modifications to
text on the current line only (note that this is different
from the full vi
editor). The command u
undoes the last
text modification command only, while U
undoes all such
commands on the current line. So if you make one change but
want to undo it, type u
; but if you make lots of changes and find
that the original is closer to what you want, you can undo everything
by typing U
. A related command is
There is also a way to save text in the delete buffer without having deleted it in the first place: just type in a delete command but use y ("yank") instead of d . This does not modify anything, but it allows you to retrieve the yanked text as many times as you like later on. The command to retrieve yanked text is p , which inserts the text on the current line to the left of the cursor. The y and p commands are powerful but far better suited to "real vi " tasks like making global changes to documents or programs than to shell commands, so we doubt you'll use them very often.
The next group of vi control mode commands we will cover allows you to move around in and search your history file. This is the all-important functionality that lets you go back and fix an erroneous command without retyping the entire line. These commands are summarized in Table 2.11 .
The first three can be preceded by repeat counts (e.g., 3k or 3- moves back three lines in the history file).
If you aren't familiar with vi and its cultural history, you may be wondering at the wisdom of choosing such seemingly poor mnemonics as h , j , k , and l for backward character, forward line, backward line, and forward character, respectively. Well, there actually is a rationale for the choices-other than that they are all together on the standard keyboard.
Bill Joy originally developed vi to run on Lear-Siegler ADM-3a terminals, which were the first popular models with addressable cursors (meaning that a program could send an ADM-3a a command to move the cursor to a specified location on the screen). The ADM-3a's h , j , k , and l keys had little arrows on them, so Joy decided to use those keys for appropriate commands in vi .
Another (partial) rationale for the command choices is that [CTRL-H] is the traditional backspace key, and [CTRL-J] denotes linefeed.
You enter the example command (RETURN works in both input and control modes, as does LINEFEED or [CTRL-J] ):
$ fgrep -l Bob < ~pete/wk/names
but you get an error message saying that your option letter was wrong. You want to change it to -s without having to retype the entire command. Assuming you are in control mode (you may have to type ESC to put yourself in control mode), you type k or - to get the command back. Your cursor will be at the beginning of the line:
$ f grep -l Bob < ~pete/wk/names
to get to the
Now let's say you get another error message, and you finally decide
to look at the manual page for the fgrep
command. You remember having
done this a while ago today, so rather than typing in the entire
(1) command, you search for the last one you used.
To do this, type ESC
to enter control mode (if you are already in
control mode, this will have no effect), then type
But typing /^ ma doesn't give you what you want: instead, the shell gives you:
$ make myprogram
The G command retrieves the command whose number is the same as the numeric prefix argument you supply. G depends on the command numbering scheme described in Chapter 3 in the section "Prompting Variables." Without a prefix argument, it goes to command number 1. This may be useful to former C shell users who still want to use command numbers.
There are some additional motion commands in vi-mode, although they are less useful than the ones we saw earlier in the chapter. These commands allow you to move to the position of a particular character in the line. They are summarized in Table 2.12 , in which x denotes any character.
All of these commands can be preceded by a repeat count.
Starting with the previous example: let's say you want to change Bob to Rob . Make sure that you're at the end of the line (or, in any case, to the left of the B in Bob ); then, if you type FB , your cursor will move to the B:
$ fgrep -l B ob < ~pete/wk/names
At this point, you could type r to replace the B with R . But let's say you wanted to change Bob to Blob . You would need to move one space to the right of the B . Of course, you could just type l . But, given that you're somewhere to the right of Bob , the fastest way to move to the o would be to type TB instead of FB followed by l .
As an example of how the repeat count can be used with character-finding commands, let's say you want to change the filename from names to namfile . In this case, assuming your cursor is still on the B , you need to get to the third e to the right, so you can type 3te , followed by l to put the cursor back on the e in names .
The character-finding commands also have associated delete commands. Read the command definitions in the previous table and mentally substitute "delete" for move. You'll get what happens when you precede the given character-finding command with a d . The deletion includes the character given as argument. For example, assume that your cursor is under the n in names :
$ fgrep -l Bob < ~pete/wk/n ames
If you want to change names to aides , one possibility is to type dfm . This means "delete right to next occurrence of m," i.e., delete "nam." Then you can type i (to enter input mode) and then "aid" to complete the change.
One final command rounds out the vi control mode commands for getting around on the current line: you can use the pipe character (| ) for moving to a specific column, whose number is given by a numeric prefix argument. Column counts start at 1; count only your input, not the space taken up by the prompt string. The default repeat count is 1, of course, which means that typing | by itself is equivalent to 0 (see Table 2.7 ).
Although the character-finding commands and | are not particularly useful, vi-mode provides one additional feature that we think you will use quite often: filename completion. This feature is not part of the real vi editor, and it was undoubtedly inspired by similar features in emacs and, originally, in the TOPS-20 operating system for DEC mainframes.
The rationale behind filename completion is simple: you should have to type only as much of a filename as is necessary to distinguish it from other filenames in the same directory. Backslash (\ ) is the command that tells the Korn shell to do filename completion in vi-mode. If you type in a word, type ESC to enter control mode, and then type \ , one of four things will happen; they are the same as for ESC ESC in emacs-mode:
A related command is
Less useful is the command =
, which does the same kind of filename
expansion as the
$ cc pro 1) problem.c 2) program.c
Several miscellaneous commands round out vi-mode; some of them are quite esoteric. They are listed in Table 2.13 .
A repeat count preceding _ causes the n -th word in the previous command to be inserted in the current line; without the count, the last word is used. Omitting the repeat count is useful because a filename is usually the last thing on a UNIX command line, and because users often run several commands in a row on the same file. With this feature, you can type all of the commands (except the first) followed by ESC _, and the shell will insert the filename.
Finally, the command @ allows you to create keyboard shortcuts by interacting with the shell's alias facility (see Chapter 3 ). If you create an alias called _x , where x is a letter, then the shell will expand the alias on the current line (but not run it) if you type @ followed by x . As with the similar facility in emacs-mode, we don't find this particularly useful.