Character-Finding Commands

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-13, in which x denotes any character.

All of these commands can be preceded by a repeat count.

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

Table 2-13. vi-mode character-finding commands

Command

Description

fx

Move right to next occurrence of x

Fx

Move left to previous occurrence of x

tx

Move right to next occurrence of x, then back one space

Tx

Move left to previous occurrence of x, then forward one space

;

Redo last character-finding command

,

Redo last character-finding command in opposite direction

Starting with the previous example: let's say you want to change Duchess to Duckess. Make sure that you're at the end of the line (or, in any case, to the left of the h in Duchess); then, if you type Fh, your cursor will move to the h:

$ fgrep -l Duc[h]ess < ~cam/book/alice_in_wonderland

At this point, you could type r to replace the h with k. But let's say you wanted to change Duchess to Dutchess. You would need to move one space to the right of the u. Of course, you could just type l. But, given that you're somewhere to the right of Duchess, the fastest way to move to the c would be to type Tu instead of Fu 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 alice_in_wonderland to alice. In this case, assuming your cursor is still on the D, you need to get to one character beyond the second slash. To do this, you can type 2fa. Your cursor will then be on the a in alice_in_wonderland.

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 a in alice_in_wonderland:

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

If you want to change alice_in_wonderland to natalie_in_wonderland, one possibility is to type dfc. This means "delete right to next occurrence of c," i.e., delete "alic". Then you can type i (to enter input mode) and then "natali" 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 (|) to move 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-8).