Friday, October 30, 2009

The Line Edit Mode








 

 










The Line Edit Mode



After 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:





$ set -o emacs

$ echo hello

hello

$ pwd

/users/pat

$



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.





$ mary had a little larb_ Ctrl+b $ mary had a little larb

$ mary had a little larb Ctrl+b $ mary had a little larb

$ mary had a little larb Ctrl+b $ mary had a little larb

$ mary had a little larb Ctrl+f $ mary had a little larb



After the cursor is on the character you want to change, you can use the Ctrl+d command to delete the current character.





$ mary had a little larb Ctrl+d $ mary had a little lab



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.





$ mary had a little lab m $ mary had a little lamb

$ mary had a little lamb m $ mary had a little lammb

$ mary had a little lammb Ctrl+h $ mary had a little lamb



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.





$ mary had a little lamb Ctrl+a $ mary had a little lamb

$ mary had a little lamb Ctrl+e $ mary had a little lamb_



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.





$ mary had a little lamb_ da $ mary had a little lambda_



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.





$ mary had a little lambda_ Esc b $ mary had a little lambda

$ mary had a little lambda Esc b $ mary had a little lambda

$ mary had a little lambda Esc b $ mary had a little lambda

$ mary had a little lambda Esc f $ mary had a_little lambda

$ mary had a_little lambda Esc f $ mary had a little_lambda



At any time you can press the Enter key and the current line will be executed as a command.





$ mary had a little_lambda Hit Enter; enter command

ksh: mary: not found

$ _



Accessing Commands from Your History



So 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:





$ pwd

/users/pat

$ cd /tmp

$ echo this is a test

this is a test

$ _



Now use Ctrl+p to access them:





$ _ Ctrl+p $ echo this is a test_



Every time Ctrl+p is used, the current line is replaced by the previous line from the command history.





$ echo this is a test_ Ctrl+p $ cd /tmp_

$ cd /tmp_ Ctrl+p $ pwd_



To execute the command being displayed, just press Enter.





$ pwd_ Hit Enter

/tmp

$ _



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:





$ _ Ctrl+r test $ ^Rtest_



The search is initiated when Enter is pressed.





$ ^Rtest_ Enter $ echo this is a test_



To execute the command that is displayed as a result of the search, Enter must be pressed again.





$ echo this is a test_ Hit Enter again

this is a test

$ _



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)`':





$ _ Ctrl+r (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:





(reverse-i-search)`': _ c (reverse-i-search)`c': echo this is a test

(reverse-i-search)`c': echo this is a test d (reverse-i-search)`cd': cd /tmp



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.





















































































































Table 15.2. Basic emacs Line Edit Commands


Command





Meaning





Ctrl+b





Move left one character





Ctrl+f





Move right one character





Esc+f





Move forward one word





Esc+b





Move back one word





Ctrl+a





Move to start of line





Ctrl+e





Move to end of line





Ctrl+d





Delete current character





Esc+d





Delete current word





erase char





(User-defined erase character, usually # or Ctrl+h), delete previous character





Ctrl+p





Get previous command from history





Ctrl+n





Get next command from history





Ctrl+r string





Search history for the most recent command line containing string














     

     


    No comments:

    Post a Comment