Section 7.6. Editing Commands


[Page 262 (continued)]

7.6. Editing Commands

The Korn shell contains simplified built-in versions of the vi, gmacs, and emacs editors that may be used to edit the current command or previous commands. To select one of these built-in editors, set either the VISUAL or the EDITOR variable to a string that ends in the name of one of these editors. In the following example, I selected the vi editor:

380 $ VISUAL=vi         ...select the built-in "vi" editor. 381 $ _ 


Each built-in editor is now described separately.

7.6.1. The Built-In vi Editor

This description assumes that you are familiar with the vi/vim editors. If you're not, please consult the description of the vim editor in Chapter 3, "GNU Utilities for Nonprogrammers."

To edit the current line, press the Esc key to enter the built-in vi editor's control mode and make the required changes. To enter append or insert mode from control mode, press the a key or the i key, respectively. To go back to control mode from either of these modes, press the Esc key. To re-execute the command, press the Enter key. Be warned that if you type a Control-D inside the editor, it terminates the shell, not just the editor.

When in control mode, key sequences fall into one of several categories:

  • standard vi key sequences (described in Chapter 3, "GNU Utilities for Nonprogrammers").

  • additional movement.

  • additional searching.

  • filename completion.

  • alias replacement.

The last four categories of key sequences are described in the following subsections.

7.6.1.1. Additional Movement

The cursor up (k or -) and cursor down (j or +) keys select the previous and next commands in the history list, respectively. This allows you to easily access history from within the built-in editor. To load a command with a particular number, enter command mode and then enter the number of the command followed by the G key. Here's an example:


[Page 263]

125 $ echo line 1 line 1 126 $ echo line 2 line 2 127 $ ...at this point, I pressed the Esc key followed by       ...k twice (up, up). This loaded command #125 onto       ...the command line, which I then executed by       ...pressing the Enter key. line 1 128 $ ...at this point, I pressed Esc followed by 125G.       ...This loaded command #125 onto the command line,       ...which I then executed by pressing the Enter key. line 1 129 $ _ 


7.6.1.2. Additional Searching

The standard search mechanisms /string and ?string search backward and forward through history, respectively. Here's an example:

127 $ echo line 1 line 1 138 $ echo line 2 line 2 139 $ ...at this point, I pressed the Esc key followed       ...by /ech, which loaded the last command       ...containing "ech" onto the command line.       ...Then I pressed n to continue the search to       ...the next command that matched. Finally, I       ...pressed the Enter key to execute the command. line 1 $ _ 


7.6.1.3. Filename Completion

If you type an asterisk (*) in control mode, it is appended to the word that the cursor is over and then processed as if it were a wildcard by the filename substitution mechanism. If no match occurs, a beep is soundedotherwise, the word is replaced by an alphabetical list of all the matching filenames and the editor enters input mode automatically. Here's an example:

114 $ ls m* m           m3          main.c            mbox m1          madness.c   main.o            mon.out m2          main        makefile          myFile 115 $ ls ma  ...at this point I pressed the Esc key              ...the * key, and then the Enter key. 
[Page 264]
115 $ ls madness.c main main.c main.o makefile madness.c main.c makefile main main.o 116 $ _


If you type an equal sign ( = ) in control mode, the editor displays a numbered list of all the files that have the current word as a prefix and then redraws the command line:

116 $ ls ma       ...at this point I pressed the Esc key                   ...and then the = key. 1) madness.c 2) main 3) main.c 4) main.o 5) makefile 116 $ ls ma_      ...back to the original command line. 


If you type a \ in control mode, the editor attempts to complete the current filename in an unambiguous way. If a completed pathname matches a directory, a / is appended; otherwise, a space is appended. Here's an example:

116 $ ls ma   ...at this point I pressed the Esc key               ...and then the \ key.               ...No completion was performed, since "ma"               ...is a prefix of more than one file. 116 $ ls mak  ...at this point I pressed the Esc key               ...and then the \ key. The editor               ...completed the name to be "makefile". 116 $ ls makefile _ 


7.6.1.4. Alias Replacement

If you find yourself typing the same pattern again and again from the editor, you can make good use of the alias replacement mechanism. If you give _letter an alias of word, the sequence @letter is replaced by word when you're in command mode. In the following example, the letter i at the start of the alias causes the built-in editor to go into insert mode, and the literal Esc at the end of the string causes it to leave vi mode:

123 $ alias _c='icommon text^['     ...^[ was Control-V                                     ...followed by Esc 124 $ echo      ...at this point I pressed Esc followed by @c. 124 $ echo common text_ 


7.6.2. The Built-in emacs/gmacs Editor

This description assumes that you are familiar with the emacs editor. If you're not, please consult the description of the emacs editor in Chapter 3, "GNU Utilities for Nonprogrammers."

Most of the emacs key sequences are supported. You may move the cursor and manipulate text using the standard emacs key sequences. To re-execute the command, press the Enter key.


[Page 265]

The main difference between the built-in editor and standard emacs is that the cursor-up, cursor-down, search-forward, and search-backward key sequences operate on the history list. For example, the cursor-up key sequence, Control-P, displays the previous command on the command line. Similarly, the search-backward key sequence, Control-R string, displays the last command that contains string.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net