2.4. Vi Editing ModeLike 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 and hit ENTER to run them. In addition, you have minimal editing capabilities via control characters, which are summarized in Table 2-6. Table 2-6. Editing commands in vi input mode
NOTE: At least some of these editig commands -- depending on which version of Unix you have -- are the same as those provided by modern Unix systems in the terminal interface. Vi-mode uses your "erase" character as the "delete previous character" key; usually it is set to DEL or CTRL-H (BACKSPACE). CTRL-V causes 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 is 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. 2.4.1. Simple Control Mode CommandsA full range of vi editing commands are available to you in control mode. The simplest of these move you around the command line[25] and are summarized in Table 2-7. Vi-mode contains two "word" concepts. The simplest is any sequence of non-whitespace characters; we'll call this a nonblank 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.[26]
Table 2-7. Basic vi control mode commands
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 ENTER, 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 moves back one space so that it is on the s. Then if you type h, your cursor moves back to the e. If you type 3h from the e, you end up at the n. Now we look at 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 ends up on the n:
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:
However, if you typed B instead of b, the nonblank word would be the entire pathname, and the cursor ends up at the beginning of it -- that is, over the tilde:
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:
Then typing e gets you to:
And typing an additional w gets you to:
On the other hand, E gets you to the end of the current nonblank word -- in this case, the end of the line. (While at first glance the commands may appear non-mnemonic, there is generally some order to the choice of command letters. Each command letter is usually the first letter of the English word for the operation. Lowercase letters work on words, while the uppercase versions work on nonblank words. Getting the hang of this is undoubtedly tougher if English isn't your native language, but that also applies to the emacs-mode commands.) 2.4.2. Entering and Changing TextNow 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. Table 2-8. Commands for entering vi input mode
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:
If you type i followed by end, you get:
That is, the cursor always appears to be over 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 get:
That is, the cursor is always 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:
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:
If you precede r with a number N, it allows you to replace the next N existing characters on the line -- but still not enter input mode. Vi-mode replaces the N characters on the line with N copies of the character you type after the r. Lowercase r is effective for fixing erroneous option letters, I/O redirection characters, punctuation, etc. 2.4.3. Deletion CommandsNow 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. Table 2-9. Some vi-mode deletion commands
These commands have a few variations and abbreviations. If you use a c instead of d, you 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. Table 2-10. Abbreviations for vi-mode delete commands
Every good editor provides "undelete" 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 and want to undo it, type u; 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 . (dot), which redoes the last text modification command. 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 ("puts") the text on the current line to the right of the cursor. The uppercase version, P, puts text to the left of the cursor. The various cut and paste commands are summarized in Table 2-11. Table 2-11. Vi-mode cut and paste commands
The d and p commands are quite useful together for rearranging the order of options or arguments on a command line. As an example, most Unix C compilers accept a -l option that indicates the name of a library to use when linking a compiled program. The -L option specifies a directory in which the compiler should look for libraries, besides looking in the standard places for system libraries.
cc -o myprog myprog.c -Lmylibdir -lmylib This command looks for the library file libmylib.a in the directory mylibdir when compiling and linking myprog.c. So far so good. The catch is that typically the -L option must appear on the command line before the -l option. Let's suppose you accidentally typed them the other way around, and therefore that the compilation failed. You can use the d and p commands to rearrange things. Start by recalling the line:
Next, move to the last option with 5w. Then back up to the preceding space with h. Your command line now looks like this:
$ cc -o myprog myprog.c -lmylib-Lmylibdir Type D to delete the rest of the line:
Now move back to the preceding c character with Bhh:
Finally, use p to insert the moved option:
Then hit ENTER and you're done. This looks like a lot of typing. But, as we will soon see, there are additional commands that let you search for characters on the command line, making it much easier to move around. And if you're an experienced vi user, you'll be right at home. 2.4.4. Moving Around in the History FileThe next group of vi control mode commands we 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-12. Table 2-12. Vi control mode commands for searching the history file
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 a command to an ADM-3a to make it 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. The primary rationale for these choices, however, is that with these keys, it's never necessary to move your hands off the "home row" of the keyboard. Perhaps + and - are better mnemonics than j and k, but the latter have the advantage of being more easily accessible to touch typists. In either case, these commands are the most basic ones for moving around the history file. To see how they work, let's take the same examples we used when discussing emacs-mode earlier. You enter the example command (ENTER works in both input and control modes, as does newline 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:
Type w to get to the -, then l or space to get to the l. Now you can replace it by typing rs; press ENTER to run the command. 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 man(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 has no effect), then type / followed by man or ma. To be on the safe side, you can also type ^ma; the ^ means match only lines that begin with ma.[27]
But typing /^ma doesn't give you what you want; instead, the shell gives you:
To search for "man" again, you can type n, which does another backward search using the last search string. Typing / again without an argument and hitting ENTER accomplishes the same thing. 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 Section 3.4.2.3, in Chapter 3. 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. 2.4.5. Character-Finding CommandsThere are some additional motion commands in vi-mode. These commands allow you to move to the position of a particular character in the line. They are summarized in Table 2-13, in which x denotes any character. All of these commands can be preceded by a repeat count. Table 2-13. Vi-mode character-finding commands
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 right of the B in Bob); then, if you type FB, your cursor moves to the B:
At this point, you could type rR 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:
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. A better way, though, is to use cfm. This means "change everything from under the cursor up to and including the next occurrence of m." This deletes "nam" and enters input mode for you. The % command is very useful for finding the matching "pair" character when used with parentheses, square brackets, and curly braces. All of these occur frequently in matched pairs on shell command lines. 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). 2.4.6. Filename and Variable Completion and ExpansionVi-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 happens; they are the same as for TAB (or ESC ESC) in emacs-mode:
As in emacs-mode, starting with ksh93h, you may use TAB instead of ESC \. However, this only works if you use set -o viraw in addition to set -o vi. (The viraw option is a bit more CPU-intensive -- although probably not noticeably -- and is required on some older Unix systems for vi-mode to work at all.) Fortunately, beginning with ksh93n, the viraw option is automatically enabled when you use vi-mode. A related command is *, which is the same as ESC * in emacs-mode as described earlier in this chapter.[28] It behaves similarly to ESC \, but if there is more than one completion possibility (number four in the list above), it lists all of them and allows you to type further. Thus, it resembles the * shell wildcard character.
Finally, the command = does the same kind of filename expansion as the * shell wildcard, but in a different way. Instead of expanding the filenames onto the command line, it prints them in a numbered list with one filename on each line. Then it gives you your shell prompt back and retypes whatever was on your command line before you typed =. For example, if the files in your directory include program.c and problem.c, and you type pro followed by ESC and then =, you will see this:
Beginning with ksh93m, prefixing the = command with a count indicates selection of a particular option. Returning to the previous example: after listing both problem.c and program.c, the command line looks like this:
If you want program.c, it's enough to type 2 =, and the shell picks expansion number 2. The command line changes to: $ cc program.c As in emacs-mode, you can also do command completion from vi-mode. The *, \, and = commands, when used on the first word of the command line, expand aliases, functions, and commands. Also as in emacs-mode, starting with ksh93l, these expansions work when you've opened a quoted string but haven't closed it yet, and for variable expansions with $ and "$. 2.4.7. Miscellaneous CommandsSeveral miscellaneous commands round out vi-mode; some of them are quite esoteric. They are listed in Table 2-14. Table 2-14. Miscellaneous vi-mode commands
[29] The line is also "executed" by the shell. However, # is the shell's comment character, so the shell ignores it. The first of these can be preceded by a repeat count. A repeat count of n preceding the ~ changes the case of the next n characters.[30] The cursor advances accordingly.
A repeat count preceding _ causes the nth 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 inserts the filename. 2.4.8. Macro Expansion with AliasesJust as described earlier for emacs-mode, you may use the shell's alias facility (described in the next chapter) to create macros, i.e., single-letter abbreviations for longer sequences of commands. If you create an alias named _x, where x is a letter, then when you type @ x, vi-mode expands the alias and reads it as command-mode input. As before, suppose that you want a command to capitalize the first letter of the current word. You could define an alias as follows: alias _C='B~' Now, if you type ESC @ C, the cursor moves to the beginning of the current word (B), and then capitalizes the current letter (~).
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|