预计阅读本页时间:-
Entering and Changing Text
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-9. All of them enter input mode a bit differently.
Table 2-9. Commands for entering vi input mode
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
Command
Description
i
Text inserted before current character (insert)
a
Text inserted after current character (append)
I
Text inserted at beginning of line
A
Text inserted at end of line
R
Text overwrites existing text
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 Duchess < ~cam/book[/]alice_in_wonderland
If you type i followed by end, you will get:
$ fgrep -l Duchess < ~cam/bookend[/]alice_in_wonderland
That is, the cursor will always appear to be under the / before alice_in_wonderland. But if you type a instead of i, you will notice the cursor move one space to the right. Then if you type miss_, you will get:
$ fgrep -l Duchess < ~cam/book/miss_[a]lice_in_wonderland
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 first a in alice_in_wonderland, type R instead, and then type through_the_looking_glass, you will see:
$ fgrep -l Duchess < ~cam/book/through_the_looking_glas[s]
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 Duchess < ~cam/book[;]alice_in_wonderland
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, and so on.