[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
System User's Guide: Operating System and Devices

Inline Editing in the Korn Shell or POSIX Shell

Normally, you type each command line from a terminal device and follow it by a new-line character (RETURN or LINE FEED). When you activate the emacs, gmacs, or vi inline editing option, you can edit the command line.

The following commands enter edit modes:

set -o emacs Enters emacs editing mode and initiates an emacs-style inline editor.
set -o gmacs Enters emacs editing mode and initiates a gmacs-style inline editor.
set -o vi Enters vi editing mode and initiates a vi-style inline editor.

An editing option is automatically selected each time the VISUAL or EDITOR variable is assigned a value ending in any of these option names.

Note: To use the editing features, your terminal must accept RETURN as a carriage return without line feed. A space must overwrite the current character on the screen.

Each editing mode opens a window at the current line. The window width is the value of the COLUMNS variable if it is defined; otherwise, the width is 80 character spaces. If the line is longer than the window width minus two, the system notifies you by displaying a mark at the end of the window. As the cursor moves and reaches the window boundaries, the window is centered about the cursor. The marks displayed are:

> Indicates that the line extends on the right side of the window.
< Indicates that the line extends on the left side of the window.
* Indicates that the line extends on both sides of the window.

The search commands in each edit mode provide access to the Korn shell history file. Only strings are matched. If the leading character in the string is a ^ (caret), the match must begin at the first character in the line.

This section discusses:

emacs Editing Mode

The emacs mode is entered when you enable either the emacs or gmacs option. The only difference between these two modes is the way each handles the Ctrl-T edit command. To edit, move the cursor to the point needing correction and insert or delete characters or words, as needed. All of the editing commands are control characters or escape sequences.

Edit commands operate from any place on a line (not just at the beginning). Do not press the Enter key or line-feed (Down Arrow) key after edit commands, except as noted.

Ctrl-F Moves the cursor forward (right) one character.
Esc-F Moves the cursor forward one word (a string of characters consisting of only letters, digits, and underscores).
Ctrl-B Moves the cursor backward (left) one character.
Esc-B Moves the cursor backward one word.
Ctrl-A Moves the cursor to the beginning of the line.
Ctrl-E Moves the cursor to the end of the line.
Ctrl-] c Moves the cursor forward on the current line to the indicated character.
Esc-Ctrl-] c Moves the cursor backward on the current line to the indicated character.
Ctrl-X Ctrl-X Interchanges the cursor and the mark.
ERASE Deletes the previous character. (User-defined erase character as defined by the stty command, usually the Ctrl-H key sequence.)
Ctrl-D Deletes the current character.
Esc-D Deletes the current word.
Esc-Backspace Deletes the previous word.
Esc-H Deletes the previous word.
Esc-Delete Deletes the previous word. If your interrupt character is the Delete key, then this command does not work.
Ctrl-T Transposes the current character with the next character in emacs mode. Transposes the two previous characters in gmacs mode.
Ctrl-C Capitalizes the current character.
Esc-C Capitalizes the current word.
Esc-L Changes the current word to lowercase.
Ctrl-K Deletes from the cursor to the end of the line. If preceded by a numerical parameter whose value is less than the current cursor position, this editing command deletes from the given position up to the cursor. If preceded by a numerical parameter whose value is greater than the current cursor position, this editing command deletes from the cursor up to the given cursor position.
Ctrl-W Deletes from the cursor to the mark.
Esc-P Pushes the region from the cursor to the mark on the stack.
KILL User-defined kill character as defined by the stty command, usually the Ctrl-G key sequence or an @ (ampersand). Kills the entire current line. If two kill characters are entered in succession, all subsequent kill characters cause a line feed (useful when using paper terminals).
Ctrl-Y Restores the last item removed from the line. (Yanks the item back to the line.)
Ctrl-L Line feeds and prints the current line.
Ctrl-@ (Null character) Sets a mark.
Esc-space Sets a mark.
Ctrl-J (New line) Executes the current line.
Ctrl-M (Return) Executes the current line.
EOF Processes the end-of-file character, normally the Ctrl-D key sequence, as an end-of-file only if the current line is null.
Ctrl-P Fetches the previous command. Each time the Ctrl-P key sequence is entered, the previous command back in time is accessed. Moves back one line when not on the first line of a multiple-line command.
Esc-< Fetches the least recent (oldest) history line.
Esc-> Fetches the most recent (youngest) history line.
Ctrl-N Fetches the next command line. Each time the Ctrl-N key sequence is entered, the next command line forward in time is accessed.
Ctrl-R String Reverses search history for a previous command line containing the string specified by the String parameter. If a value of 0 is given, the search is forward. The specified string is terminated by an Enter or new-line character. If the string is preceded by a ^ (caret character), the matched line must begin with the String parameter. If the String parameter is omitted, then the next command line containing the most recent String parameter is accessed. In this case, a value of 0 reverses the direction of the search.
Ctrl-O (Operate) Executes the current line and fetches the next line relative to the current line from the history file.
Esc Digits (Escape) Defines the numeric parameter. The digits are taken as a parameter to the next command. The commands that accept a parameter are Ctrl-F, Ctrl-B, ERASE, Ctrl-C, Ctrl-D, Ctrl-K, Ctrl-R, Ctrl-P, Ctrl-N, Ctrl-], Esc-., Esc-Ctrl-], Esc-_, Esc-B, Esc-C, Esc-D, Esc-F, Esc-H, Esc-L, and Esc-Ctrl-H.
Esc Letter (Soft-key) Searches the alias list for an alias named _Letter. If an alias of this name is defined, its value is placed into the input queue. The Letter parameter must not specify one of the escape functions.
Esc-[ Letter (Soft-key) Searches the alias list for an alias named __Letter (double underscore). If an alias of this name is defined, its value is placed into the input queue. This command can be used to program function keys on many terminals.
Esc-. Inserts on the line the last word of the previous command. If preceded by a numeric parameter, the value of this parameter determines which word to insert rather than the last word.
Esc-_ Same as the Esc-. (Escape, dash, period) key sequence.
Esc-* Attempts file name substitution on the current word. An asterisk is appended if the word does not match any file or contain any special pattern characters.
Esc-Esc File name completion. Replaces the current word with the longest common prefix of all file names that match the current word with an asterisk appended. If the match is unique, a / (slash) is appended if the file is a directory and a space is appended if the file is not a directory.
Esc-= Lists the files that match the current word pattern as if an asterisk were appended.
Ctrl-U Multiplies the parameter of the next command by 4.
\ Escapes the next character. Editing characters and the ERASE, KILL and INTERRUPT (normally the Delete key) characters can be entered in a command line or in a search string if preceded by a \ (backslash). The backslash removes the next character's editing features, if any.
Ctrl-V Displays the version of the shell.
Esc-# Inserts a # (pound sign) at the beginning of the line and then executes the line. This causes a comment to be inserted in the history file.

vi Editing Mode

The vi editing mode has two typing modes. When you enter a command, you are in Input mode. To edit, you must enter the Control mode by pressing the Esc key.

Most control commands accept an optional repeat Count parameter prior to the command. When in vi mode on most systems, canonical processing is initially enabled. The command is echoed again if:

The Esc character terminates canonical processing for the remainder of the command, and you can then modify the command line. This scheme has the advantages of canonical processing with the type-ahead echoing of raw mode. If the viraw option is also set, canonical processing is always disabled. This mode is implicit for systems that do not support two alternate end-of-line delimiters and might be helpful for certain terminals.

Available vi edit commands are grouped in the following categories:

Input Edit Commands

Note: By default, the editor is in input mode.
ERASE (User-defined erase character as defined by the stty command, usually Ctrl-H or #.) Deletes the previous character.
Ctrl-W Deletes the previous blank separated word.
Ctrl-D Terminates the shell.
Ctrl-V Escapes the next character. Editing characters, such as the ERASE or KILL characters, can be entered in a command line or in a search string if preceded by a Ctrl-V key sequence. The Ctrl-V key sequence removes the next character's editing features (if any).
\ Escapes the next ERASE or KILL character.

Motion Edit Commands

Motion edit commands move the cursor:

[Count]l Moves the cursor forward (right) one character.
[Count]w Moves the cursor forward one alphanumeric word.
[Count]W Moves the cursor to the beginning of the next word that follows a blank.
[Count]e Moves the cursor to the end of the current word.
[Count]E Moves the cursor to the end of the current blank-separated word.
[Count]h Moves the cursor backward (left) one character.
[Count]b Moves the cursor backward one word.
[Count]B Moves the cursor to the previous blank-separated word.
[Count]| Moves the cursor to the column specified by the Count parameter.
[Count]fc Finds the next character c in the current line.
[Count]Fc Finds the previous character c in the current line.
[Count]tc Equivalent to f followed by h.
[Count]Tc Equivalent to F followed by l.
[Count]; Repeats for the number of times specified by the Count parameter the last single-character find command: f, F, t, or T.
[Count], Reverses the last single-character find command the number of times specified by the Count parameter.
0 Moves the cursor to the start of a line.
^ Moves the cursor to the first nonblank character in a line.
$ Moves the cursor to the end of a line.

Search Edit Commands

Search edit commands access your command history:

[Count]k Fetches the previous command.
[Count]- Equivalent to the k command.
[Count]j Fetches the next command. Each time the j command is entered, the next command is accessed.
[Count]+ Equivalent to the j command.
[Count]G Fetches the command whose number is specified by the Count parameter. The default is the least recent history command.
/String Searches backward through history for a previous command containing the specified string. The string is terminated by a RETURN or new-line character. If the specified string is preceded by a ^ (caret ), the matched line must begin with the String parameter. If the value of the String parameter is null, the previous string is used.
?String Same as /String except that the search is in the forward direction.
n Searches for the next match of the last pattern to /String or ? commands.
N Searches for the next match of the last pattern to /String or ? commands, but in the opposite direction. Searches history for the string entered by the previous /String command.

Text-Modification Edit Commands

Text-modification edit commands modify the line:

a Enters the input mode and enters text after the current character.
A Appends text to the end of the line. Equivalent to the $a command.
[Count]cMotion
c[Count]Motion Deletes the current character through the character to which the Motion parameter specifies to move the cursor, and enters input mode. If the value of the Motion parameter is c, the entire line is deleted and the input mode is entered.
C Deletes the current character through the end of the line and enters input mode. Equivalent to the c$ command.
S Equivalent to the cc command.
D Deletes the current character through the end of line. Equivalent to the d$ command.

[Count]dMotion

d[Count]Motion Deletes the current character up to and including the character specified by the Motion parameter. If Motion is d, the entire line is deleted.
i Enters the input mode and inserts text before the current character.
I Inserts text before the beginning of the line. Equivalent to the 0i command.
[Count]P Places the previous text modification before the cursor.
[Count]p Places the previous text modification after the cursor.
R Enters the input mode and types over the characters on the screen.
[Count]rc Replaces the number of characters specified by the Count parameter, starting at the current cursor position, with the characters specified by the c parameter. This command also advances the cursor after the characters are replaced.
[Count]x Deletes the current character.
[Count]X Deletes the preceding character.
[Count]. Repeats the previous text-modification command.
[Count]~ Inverts the case of the number of characters specified by the Count parameter, starting at the current cursor position, and advances the cursor.
[Count]_ Appends the word specified by the Count parameter of the previous command and enters input mode. The last word is used if the Count parameter is omitted.
* Appends an * (asterisk) to the current word and attempts file name substitution. If no match is found, it rings the bell. Otherwise, the word is replaced by the matching pattern and input mode is entered.
\ File name completion. Replaces the current word with the longest common prefix of all file names matching the current word with an asterisk appended. If the match is unique, a / (slash) is appended if the file is a directory. A space is appended if the file is not a directory.

Miscellaneous Edit Commands

Miscellaneous edit commands include:

[Count]yMotion
y[Count]Motion Yanks the current character up to and including the character marked by the cursor position specified by the Motion parameter and puts all of these characters into the delete buffer. The text and cursor are unchanged.
Y Yanks from the current position to the end of the line. Equivalent to the y$ command.
u Undoes the last text-modifying command.
U Undoes all the text-modifying commands performed on the line.
[Count]v Returns the command fc -e ${VISUAL:-${EDITOR:-vi}} Count in the input buffer. If the Count parameter is omitted, then the current line is used.
Ctrl-L Line feeds and prints the current line. This command is effective only in control mode.
Ctrl-J (New line) Executes the current line, regardless of the mode.
Ctrl-M (Return) Executes the current line, regardless of the mode.
# Sends the line after inserting a # (pound sign) in front of the line. Useful if you want to insert the current line in the history without executing it.

If the command line contains a pipe (|) or semicolon or new-line character, then additional # (pound signs) will be inserted in front of each of these symbols. To delete all pound signs, retrieve the command line from history and enter another #.

= Lists the file names that match the current word as if an asterisk were appended to it.
@Letter Searches the alias list for an alias named _Letter. If an alias of this name is defined, its value is placed into the input queue for processing.

[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]