shedit — Interactive command and history editing in the shell

Format

set –o editmode

EDITOR=editprog

VISUAL= editprog

Usage notes

POSIX uses a number of keys for such things as erase and kill processing. By default, the shell leaves command-line editing to POSIX, using these familiar editing keys. However, these functions are not particularly powerful or friendly. As an alternative, the shell has built-in facilities for interactive command editing and file name generation that not only aid in composing new commands but also make it easy for you to modify and re-execute previous commands. This capability is distinct from that provided by the fc command, which passes previous command lines to a separate program for editing. The built-in facilities mimic the emacs, gmacs, or vi screen editors, and enable the following commands (see set and vi for details).
set –o emacs
set –o gmacs
set –o vi

These facilities are also enabled (with the corresponding option set) by assigning a value ending in vi to the environment variables EDITOR or VISUAL. (See sh.)

Unlike full-screen editors, shell editing uses a one-line window, extending from the end of the prompt to the next-to-last column. Multiline history entries are displayed with newlines represented as ^J.

The number of columns on the output device is obtained from the COLUMNS environment variable if defined; otherwise, it is assumed to be 80.

A command line that extends into the rightmost column can be scrolled horizontally. If you try to move the cursor beyond the edge of the window, the line is scrolled to approximately center the cursor in the window. The second last column displays a character marking a scrollable line: < indicates extra data off the left, > indicates extra data off the right, and * indicates extra data off both sides.

emacs/gmacs editing mode

When the emacs/gmacs editing mode has been enabled, ordinary printable characters from the keyboard are entered in the command line and echoed. Various control characters introduce command sequences for such things as moving the cursor, scrolling through the command history, and modifying the current command. The only difference between emacs and gmacs is in the handling of Ctrl-T.

The command sequences recognized are listed in functional groups. The notation Meta– represents EscK, followed by the letter. The terminology is historical. Many commands accept an optional preceding count which is entered in decimal as Meta-digits, or as Ctrl-, which multiplies the current count (initially 1) by 4.

List of cursor movement combinations:
nCtrl-B
Moves the cursor back n characters.
nCtrl-F
Moves the cursor forward n characters.
Ctrl-A
Moves the cursor to beginning of line.
Ctrl-E
Moves the cursor to end of line.
nMeta–b
Moves the cursor back to the nth previous beginning of word (string of alphanumerics).
nMeta–f
Moves the cursor forward to nth beginning of word.
Ctrl–]c
Moves the cursor forward to next character c on current line.
Meta–space
Sets mark at cursor position.
Ctrl-@
Sets mark at cursor position.
Ctrl-x Ctrl-X
Exchanges cursor position and mark.
List of line search combinations; these numbers display a different history line.
nCtrl-P
Selects the nth previous command line from history.
nCtrl-N
Selects the nth next command line from history.
Meta–<
Selects the earliest command line from history.
Meta–>
Selects the latest command line from history.
nCtrl-RstringEnter
Selects the nth previous command line matching string. If n is zero, then select the next matching command after the current line.
List of text change combinations:
n erase
Deletes n characters to the left of the cursor. This is the erase character.
nBackspace
Deletes n characters to the left of the cursor.
nCtrl-H
Deletes n characters to the left of the cursor.
nCtrl-D
Deletes n characters to the right of the cursor. If the current line is empty, the shell is ended.
nMeta–Ctrl-H
Deletes to the nth beginning of word before the cursor.
nMeta–h
Deletes to the nth beginning of word before the cursor.
nMeta-d
Deletes to the nth beginning of word after the cursor.
nCtrl-K
Deletes from the cursor to the end of line. If n is zero, then deletes from the beginning of line to the cursor.
kill
Deletes the entire current line. This is the line kill character.
Ctrl-G
Deletes the entire current line.
Ctrl-W
Deletes from cursor position to the mark (set with Meta-space or Ctrl-@.
Ctrl-T
In emacs mode, transposes the current character with the previous character and moves the cursor forward. If the cursor is at the end of the line, or in gmacs mode, transposes the previous two characters.
Ctrl-Y
Restores the last text deleted in emacs mode.
Ctrl-C
Capitalizes character under cursor.
Ctrl-^
Capitalizes character under cursor.
Meta-c
Capitalizes word to right of cursor.
Meta-l
Lowercases word to right of cursor.
Meta-u
Uppercases word to right of cursor.
nMeta-.
Inserts the nth word of the previous command. If n is not given or it is zero, inserts the last word of the previous command.
nMeta-_
Inserts the nth word of the previous command. If n is not given or it is zero, inserts the last word of the previous command.
Meta-*
Replaces the current word with the list of files which would match that word with an * appended.
Meta-Esc
Used to complete a path name. If there is only one existing path name that matches as much as you've typed, the path name is completed and a space is added after the complete path name. If there are several matching path names, the shell expands what you've typed by adding all the characters that are common to all matching path names.
Meta-=
Lists all path names matching the current word.
List of miscellaneous combinations:
Ctrl-J
Executes the current command line.
Ctrl-M
Executes the current command line.
Ctrl-L
Re-displays the current command line.
Ctrl-O
Remembers the next command line, executes the current command line, then selects the remembered line.
Ctrl-U
Multiplies the count on the following command by 4 (for each Ctrl-U.
Ctrl-V
Displays the version of the shell.
Takes the next character literally. Thus, you can enter command and control characters in a command line or search string.
eof
Terminates the shell. This is the end-of-file character.
Ctrl-D
Terminates the shell.
Metan
Enters a count for the following command.

vi editing mode

When the vi editing facilities have been enabled, the shell is initially in input mode after each new prompt. Keyboard input is normally inserted at the current position in the current command line; the exceptions are the following action keys.
erase
Deletes the character to the left of the cursor. This is the erase character.
Backspace
Deletes the character to the left of the cursor.
eof
Terminates the shell. This is the end-of-file character.
Ctrl-D
Terminates the shell.
Ctrl-W
Deletes the word (white-space delimited string) to the left of the cursor.
kill
Deletes the current line. This is the line kill character.
Ctrl-X
Deletes the current line.
Ctrl-J
Deletes the current line.
Ctrl-M
Deletes the current line.
Enter
Executes the current line.
Esc
Switches from input mode to command mode.

If you press the Esc key, the shell enters command mode and keyboard input is interpreted as commands to reposition the cursor, scroll through the command history, delete or change text, or reenter input mode. In command mode, input is not echoed; it is acted upon. Many commands take an optional count, n, which is entered as a preceding decimal number (not echoed); the command is executed that number of times. Except where otherwise noted, n defaults to 1.

Ctrl-V
Takes the next character literally; useful for entering any of these action keys as text.
\
Escapes the following action key. If the next character is any action key except Ctrl-J, Ctrl-M, or Enter, the is erased and the escaped character is entered literally. Otherwise, the is entered and the next character is treated normally.

Cursor movement

These commands reposition the cursor in the command line.
nh
Moves back n characters.
n1
Moves forward n characters.
0
Moves to the first character on the line.
^
Moves to the first nonblank character on the line.
$
Moves to the last character on the line.
nw
Moves to the beginning of the nth next word (string of alphanumerics, or of nonblank nonalphanumerics).
nW
Moves to the beginning of the nth next fullword (string of nonblanks).
nb
Moves to the nth previous beginning of word.
nB
Moves to the nth previous beginning of fullword.
ne
Moves to the nth next end of word.
nE
Moves to the nth next end of fullword.
nfc
Moves to the nth next character c.
nFc
Moves to the nth previous character c.
ntc
Moves to the character before the nth next character c.
nTc
Moves to the character after the nth previous character c.
n;
Repeats the previous f, F, t, or T command.
n,
Repeats the previous f, F, t, or T command, but in the opposite direction.

Line search

These commands change the current displayed command line.
nj
Selects the nth next command line from history.
n+
Selects the nth next command line from history.
nk
Selects the nth previous command line from history.
n–
Selects the nth previous command line from history.
nG
Selects the command with history number n, or the latest command if n is omitted.
/stringEnter
Selects the first command line, searching backwards, that matches string. If string is omitted, the previous search string is used.
?stringEnter
Selects the first command line, searching forwards, that matches string. If string is omitted, the previous search string is used.
n
Repeats the last string search (‘/’ or ‘?’) command.
N
Repeats the last string search, but in the opposite direction.

Text change

The following commands alter the text in the current command line. Some of these commands operate on a text block, defined by an immediately following cursor movement command. This is designated by m (for movement) in the text change command. The text block extends from the current cursor position to the new position determined by the movement command.
i
Enters input mode, inserting text before the character under the cursor.
I
Inserts before the first nonblank on line (^i).
a
Moves the cursor forward one character and enter input mode, appending text after the character originally under the cursor.
A
Appends to end of line ($a).
ndm
Deletes text block. If n is given, it is applied to the movement.
dd
Deletes entire command line.
D
Deletes from cursor to end of line (d$).
nx
Deletes n characters to right of cursor (ndl).
nX
Deletes n characters to left of cursor (ndh).
ncm
Change text block; deletes block of text and enters input mode. If n is given, it is applied to the movement.
cc
Change entire command line.
s
Change entire command line.
ns
Change next n characters from cursor.
np
Puts back, after the character under the cursor, n copies of the last block deleted by a text change command.
nP
Puts back, before the character under the cursor, n copies of the last block deleted by a text change command.
rc
Replaces the single character under the cursor with the character c, and advances the cursor one position.
R
Enters replace mode: a special case of input mode in which each character entered overwrites that under the cursor, and advances the cursor one position.
u
Undoes the last text change to the current line. This is itself a text change command, and so acts as a toggle for the last change.
U
Undoes all changes to the current line.
n
Inverts the case of the next n characters, advancing the cursor over them.
n.
Repeats the last text change command. If n is given, it overrides the count originally given with the repeated command.
n_
Appends after the character under the cursor, the nth argument from the previous command line (default last), and enter input mode.
*
Replaces the current word with the list of file names that matches the word with an * appended. If there is no match, an audible alarm sounds and the word is not changed. Otherwise, the cursor is positioned at the end of the list and input mode is entered.
\
Used to complete a path name. If there is only one existing path name that matches as much as you've typed, the path name is completed and a space is added after the complete path name. If there are several matching path names, the shell expands what you've typed by adding all the characters that are common to all matching path names.
=
Lists all path names matching the current word.

Miscellaneous

nym
Yanks text block into the delete buffer. Does not alter the command line or cursor position, but makes the text block available to subsequent put or p commands. If n is given, it is applied to the movement.
yy
Yanks the entire command line.
Y
Equivalent to y$. Yanks the rest of the line.
#
Equivalent to I#Enter.
nv
Executes fc –e ${VISUAL:–${EDITOR:–vi}} n. If n is omitted, the history number of the current line is used.
Ctrl-L
Redisplays the current line.
Ctrl-J
Executes the current line.
cm
Executes the current line.
Enter
Executes the current line.
@letter
Inserts the value of the alias named _letter. The symbol letter represents a single alphabetic character from the portable character set; implementations may support additional characters as an extension. If the alias _letter contains other editing commands, these commands are performed as part of the insertion. If the _letter alias is not enabled, this command has no effect.

Limits

Selecting a previous history line for editing while at a secondary prompt (that is, while entering a subsequent line of a new multiline command) yields unexpected results.

Related information

fc, set, sh, vi