|
The Line Edit ModeAfter turning on the emacs line editor, you probably won't even notice anything different because you can type in and execute commands the same way as before:
To use the line editor, you enter emacs commands. emacs commands are either control characters�that is, characters typed in by holding down the Ctrl key and pressing another character�or they are characters preceded by the ESCAPE or Esc key. You may enter emacs commands anytime you want; there are no separate modes like the vi line editor. Note that emacs commands are not followed by an Enter. We cover only a few of them here; for a complete list of commands, refer to the documentation for Bash or the Korn shell. First, let's look at moving the cursor around. The Ctrl+b command moves the cursor to the left, and the Ctrl+f command moves it to the right. Try this out by pressing Ctrl+b and Ctrl+f a few times. The cursor should move around on the line. If you try to move the cursor past the left or right side of the line, the shell simply ignores you.
After the cursor is on the character you want to change, you can use the Ctrl+d command to delete the current character.
Note that the b moved to the left when the r was deleted and is now the current character. To add characters to the command line, you simply type them in. The characters are inserted before the current character.
Note that the current erase character (usually either # or Ctrl+h) will always delete the character to the left of the cursor. The Ctrl+a and Ctrl+e commands may be used to move the cursor to the beginning and end of the command line, respectively.
Note that the Ctrl+e command places the cursor one space to the right of the last character on the line. (When you're not in emacs mode, the cursor is always at the end of the line, one space to the right of the last character typed in.) When you're at the end of the line, anything you type will be appended to the line.
Two other commands useful in moving the cursor are the Esc f and Esc b commands. The Esc f command moves the cursor forward to the end of the current word, where a word is a string of letters, numbers, and underscores delimited by blanks or punctuation. The Esc b command moves the cursor backward to the beginning of the previous word.
At any time you can press the Enter key and the current line will be executed as a command.
Accessing Commands from Your HistorySo far, you've learned how to edit the current line. As we said before, the shell keeps a history of recently entered commands. To access these commands, you can use the emacs commands Ctrl+p and Ctrl+n. The Ctrl+p command replaces the current line on your terminal with the previously entered command, putting the cursor at the end of the line. Let's assume that these commands have just been entered:
Now use Ctrl+p to access them:
Every time Ctrl+p is used, the current line is replaced by the previous line from the command history.
To execute the command being displayed, just press Enter.
The Ctrl+n command is the reverse of the Ctrl+p command and is used to display the next command in the history. The Ctrl+r command is used to search through the command history for a command containing a string. The Ctrl+r is entered followed by the string to search for, followed by the Enter key. The shell then searches the command history for the most recently executed command that contains that string on the command line. If found, the command line is displayed; otherwise, the shell "beeps" the terminal. When the Ctrl+r is typed, the shell replaces the current line with ^R:
The search is initiated when Enter is pressed.
To execute the command that is displayed as a result of the search, Enter must be pressed again.
To continue the search through the command history, you simply type Ctrl+r followed by an Enter. Bash handles Ctrl+r a little differently. When you type Ctrl+r, Bash replaces the current line with (reverse-i-search)`':
As you type text, the line is updated inside the `' with the text you type, and the rest of the line is updated with the matching command:
Note how Bash highlights the matching part of the command by placing the cursor on it. As with the Korn shell, the command is executed by pressing Enter. When you've found the command in the history (either by Ctrl+p, Ctrl+n, or Ctrl+r), you can edit the command using the other emacs commands we've already discussed. Note that you don't actually change the command in the history: That command cannot be changed after it is entered. Instead, you are editing a copy of the command in the history, which will itself be entered in the history when you press Enter. Table 15.2 summarizes the basic line edit commands.
|
|
No comments:
Post a Comment