Simple Control Mode Commands

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-8. 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) plus the underscore (_), or any sequence of only non-alphanumeric characters; we'll just call this a word.[6]

Table 2-8. Basic vi control mode commands

广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元

Command

Description

h

Move left one character

l

Move right one character

w

Move right one word

b

Move left one word

W

Move to beginning of next non-blank word

B

Move to beginning of preceding non-blank word

e

Move to end of current word

E

Move to end of current non-blank word

0

Move to beginning of line

^

Move to first non-blank character in line

$

Move to end of line

All of these commands except the last three can be preceded by a number that acts as a repeat count. Whenever you type a number for the repeat count, the number replaces the command prompt for the duration of the repeat command. If your keyboard has cursor motion keys ("arrow" keys), you can use the left and right arrows to move between characters instead of the h and l keys. Repeat counts will work with the cursor keys as well.

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 Duchess < ~cam/book/alice_in_wonderland[]

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 d. Then if you type h, your cursor will move back to the n. If you type 3h from the n, you will end up at the r.

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 alice_in_wonderland, and the cursor will end up on the a:

$ fgrep -l Duchess < ~cam/book/[a]lice_in_wonderland

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 Duchess < ~cam/book[/]alice_in_wonderland

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—over the tilde:

$ fgrep -l Duchess < [~]cam/book/alice_in_wonderland

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 /alice_in_wonderland: book, slash, cam, and the leading tilde.

At this point, w and W do the opposite: typing w gets you over the c, 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 Duchess < ~[c]am/book/alice_in_wonderland

Then typing e gets you to:

$ fgrep -l Duchess < ~ca[m]/book/alice_in_wonderland

And typing an additional w gets you to:

$ fgrep -l Duchess < ~cam[/]book/alice_in_wonderland

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.)