Command History

The shell lets you display or modify previous commands. Using the history command, you can manage the list of commands kept in the shell’s history; see history for more information. This section focuses on the facilities for editing stored commands. Commands in the history list can be modified using:

  • Line-edit mode

  • The fc command

  • C-shell–style history

Line-Edit Mode

Line-edit mode emulates 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. You can also change the line before executing it. When you’re ready to issue the command, press the Enter key.

Emacs editing mode is the default. To control command-line editing, you must use either set -o vi or set -o emacs; Bash does not use variables to specify the editor.

Note that the vi editing mode starts in input mode; to type a vi command, press the Escape key first.

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

ESC-b

Move back one word.

w

ESC-f

Move forward one word.

X

DEL

Delete previous character.

x

CTRL-d

Delete character under cursor.

dw

ESC-d

Delete word forward.

db

ESC-h

Delete word backward.

xp

CTRL-t

Transpose two characters.

Both editing modes allow you to use ...

Get bash Pocket Reference 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.