Command History

bash lets you display or modify previous commands. Commands in the history list can be modified using:

  • Line-edit mode

  • The fc command

Line-Edit Mode

Line-edit mode lets you emulate many features of the vi and Emacs editors. The history list is treated like a file. When the editor is invoked, you type editing keystrokes to move to the command line you want to execute. On most terminals, arrow keys work in both Emacs mode and vi command mode. You can also change the line before executing it. See Table 20-23 for some examples of common line-edit commands. When you’re ready to issue the command, press Return.

The default line-edit mode is Emacs. To enable vi mode, enter:

$ set -o vi

Note that vi starts in input mode; to type a vi command, press Esc first.

The mode you use for editing bash commands is entirely separate from the editor that is invoked for you automatically within many commands (for instance, the editor invoked by mail readers when you ask them to create a new mail message). To change the default editor, set the VISUAL or EDITOR variable to the filename or full pathname of your favorite editor:

$ export EDITOR=emacs

Table 20-23. Common editing keystrokes

vi

Emacs

Result

k

Ctrl-p

Get previous command.

j

Ctrl-n

Get next command.

/string

Ctrl-r string

Get previous command containing string.

h

Ctrl-b

Move back one character.

l

Ctrl-f

Move forward one character.

b

M-b

Move back one word.

w

M-f

Move forward one word.

X

Del

Delete previous ...

Get Mac OS X Panther in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.