This page is about Emacs’ Minibuffer History.

Emacs Minibuffer History

Text you enter in the minibuffer (commands, input arguments) is typically added to a minibuffer history, also called input history.

Different contexts use different minibuffer histories. For example, if you input a filename argument to a command such as ‘find-file’, it is added to a history of filename inputs.

You can later retrieve a previously entered input from a minibuffer history and reuse it (perhaps editing it first). Use the following key sequences to do that:

Naturally, these keys work only during minibuffer input.

‘M-p’ and ‘M-n’ move one item through a history list each time you use them.

Rebinding Cycling Keys to History Completing Keys

‘M-s’ and ‘M-r’ are, by default, bound to ‘next-complete-history-element’ and ‘previous-complete-history-element’, respectively (see above). Alternatively, you might want to rebind ‘M-p’, ‘M-n’, ‘up’ and ‘down’ to the history completion commands:

 (define-key minibuffer-local-map (kbd "M-p") 'previous-complete-history-element)
 (define-key minibuffer-local-map (kbd "M-n") 'next-complete-history-element)
 (define-key minibuffer-local-map (kbd "<up>") 'previous-complete-history-element)
 (define-key minibuffer-local-map (kbd "<down>") 'next-complete-history-element)

Packages that Extend Minibuffer History Behavior

Removing Killed Buffers from the History

Commands such as ‘switch-to-buffer’ (‘C-x b’) create a new buffer of the given name if no such buffer exists. Killing a buffer does not automatically remove it from the buffer-name minibuffer history, ‘buffer-name-history’. That means that when you try to use that name later, a new buffer with that name is created. This might not be what you want most of the time.

This code removes a buffer name from ‘buffer-name-history’ when the buffer is killed:

  (add-hook 'kill-buffer-hook
   (lambda ()
    (setq buffer-name-history
           buffer-name-history :test 'string=))))

You can just use EmacsLisp’s ‘delete’ here and drop the `:test string=’, instead of requiring the CommonLisp library code (cl-seq.el) at runtime for ‘delete*’. ‘delete’ uses ‘equal’, which does the same thing here (uses `string=’), and about as efficiently. – DrewAdams

CategoryCommands CategoryCompletion