Fetch the next earlier old shell command
Fetch the next later old shell command (
Begin an incremental regexp search of old shell commands
Fetch the next subsequent command from the history
Fetch one argument from an old shell command
Display the buffer’s history of shell commands in another window
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, and M-r. These work
similar to the minibuffer history commands (see Minibuffer History), except that they operate within the Shell buffer rather
than the minibuffer, and
M-r in a Shell buffer invokes
incremental search through shell command history.
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 . (
copies an individual argument from a previous command, like
ESC . in Bash and
zsh. 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 commands, always
using the same value of n (don’t give a prefix argument when
you repeat the C-c . command).
If you set
comint-insert-previous-argument-from-end to a
nil value, C-c . will instead copy the nth
argument counting from the last one; this emulates ESC .
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.
If you run the shell on a remote host, this setting might be
overwritten by the variable
tramp-histfile-override. It is
recommended to set this variable to