Next: , Up: Shell History

36.5.1 Shell History Ring

M-p
C-<UP>
Fetch the next earlier old shell command.


M-n
C-<DOWN>
Fetch the next later old shell command.


M-r
Begin an incremental regexp search of old shell commands.
C-c C-x
Fetch the next subsequent command from the history.
C-c .
Fetch one argument from an old shell command.
C-c C-l
Display the buffer's history of shell commands in another window (comint-dynamic-list-input-ring).

Shell buffers provide a history of previously entered shell commands. To reuse shell commands from the history, use the editing commands M-p, M-n, M-r and M-s. These work just like the minibuffer history commands (see Minibuffer History), except that they operate within the Shell buffer rather than the minibuffer.

M-p fetches an earlier shell command to the end of the shell buffer. Successive use of M-p fetches successively earlier shell commands, each replacing any text that was already present as potential shell input. M-n does likewise except that it finds successively more recent shell commands from the buffer. C-<UP> works like M-p, and C-<DOWN> like M-n.

The history search command M-r begins an incremental regular expression search of previous shell commands. After typing M-r, start typing the desired string or regular expression; the last matching shell command will be displayed in the current line. Incremental search commands have their usual effects—for instance, C-s and C-r search forward and backward for the next match (see Incremental Search). When you find the desired input, type <RET> to terminate the search. This puts the input in the command line. Any partial input you were composing before navigating the history list is restored when you go to the beginning or end of the history ring.

Often it is useful to reexecute several successive shell commands that were previously executed in sequence. To do this, first find and reexecute the first command of the sequence. Then type C-c C-x; that will fetch the following command—the one that follows the command you just repeated. Then type <RET> to reexecute this command. You can reexecute several successive commands by typing C-c C-x <RET> over and over.

The command C-c . (comint-input-previous-argument) copies an individual argument from a previous command, like ESC . in Bash. The simplest use copies the last argument from the previous shell command. With a prefix argument n, it copies the nth argument instead. Repeating C-c . copies from an earlier shell command instead, always using the same value of n (don't give a prefix argument when you repeat the C-c . command).

These commands get the text of previous shell commands from a special history list, not from the shell buffer itself. Thus, editing the shell buffer, or even killing large parts of it, does not affect the history that these commands access.

Some shells store their command histories in files so that you can refer to commands from previous shell sessions. Emacs reads the command history file for your chosen shell, to initialize its own command history. The file name is ~/.bash_history for bash, ~/.sh_history for ksh, and ~/.history for other shells.