预计阅读本页时间:-
Moving Around in the History List
Now we know how to get around the command line efficiently and make changes. But that doesn't address the original issue of recalling previous commands by accessing the history list. emacs-mode has several commands for doing this, summarized in Table 2-4.
Table 2-4. emacs-mode commands for moving through the history list
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
Command
Description
CTRL-P
Move to previous line
CTRL-N
Move to next line
CTRL-R
Search backward
ESC-<
Move to first line of history list
ESC->
Move to last line of history list
CTRL-P and CTRL-N move you through the command history. If you have cursor motion keys (arrow keys) you can use them instead. The up-arrow is the same as CTRL-P and the down-arrow is the same as CTRL-N. For the rest of this discussion, we'll stick to using the control keys because they can be used on all keyboards.
CTRL-P is by far the one you will use most often—it's the "I made a mistake, let me go back and fix it" key. You can use it as many times as you wish to scroll back through the history list. If you want to get back to the last command you entered, you can hold down CTRL-N until bash beeps at you, or just type ESC->. As an example, you hit RETURN to run the command above, but you get an error message telling you that your option letter was incorrect. You want to change it without retyping the whole thing.
First, you would type CTRL-P to recall the bad command. You get it back with point at the end:
$ grep -l Duchess < ~cam/book/alice_in_wonderland[]
After CTRL-A, ESC-F, two CTRL-Fs, and CTRL-D, you have:
$ grep -[]Duchess < ~cam/book/alice_in_wonderland
You decide to try -s instead of -l, so you type s and hit RETURN. You get the same error message, so you give up and look it up in the manual. You find out that the command you want is fgrep—not grep—after all.
You sigh heavily and go back and find the fgrep command you typed in an hour ago. To do this, you type CTRL-R; whatever was on the line will disappear and be replaced by (reverse-i-search)`':. Then type fgrep, and you will see this:
$ (reverse-i-search)`fgrep': fgrep -l Duchess <~cam/book/ \
alice_in_wonderland[]
The shell dynamically searches back through the command history each time you type a letter, looking for the current substring in the previous commands. In this example, when you typed f the shell would have printed the most recent command in the history with that letter in it. As you typed more letters, the shell narrowed the search until you ended up with the line displayed above. Of course, this may not have been the particular line you wanted. Typing CTRL-R again makes the shell search further back in the history list for a line with "fgrep" in it. If the shell doesn't find the substring again, it will beep.
If you try the fgrep command by hitting RETURN, two things will happen. First, of course, the command will run. Second, this line will be entered into the history list at the end, and your "current line" will be at the end as well. You will no longer be somewhere else in the command history.
Another handy trick to save typing if you have already done a search is to type CTRL-R twice in a row. This recalls the previous search string you typed in.[3]
CTRL-P, CTRL-N, and CTRL-R are clearly the most important emacs-mode commands that deal with the command history. The others are less useful but are included for compatibility with the full emacs editor.