GNU Emacs

38.5.3 Shell History References

Various shells, including csh and bash, support history references that begin with ‘ !’ and ‘ ^’. Shell mode recognizes these constructs, and can perform the history substitution for you.

If you insert a history reference and type TAB, this searches the input history for a matching command, performs substitution if necessary, and places the result in the buffer in place of the history reference. For example, you can fetch the most recent command beginning with ‘ mv’ with ! m v TAB. You can edit the command if you wish, and then resubmit the command to the shell by typing RET.

Shell mode can optionally expand history references in the buffer when you send them to the shell. To request this, set the variable comint-input-autoexpand to input. You can make SPC perform history expansion by binding SPC to the command comint-magic-space. See Changing Key Bindings Interactively.

Shell mode recognizes history references when they follow a prompt. See Shell Prompts, for how Shell mode recognizes prompts.