The Emacs *shell* has a nice command history accessible via C-c C-l (comint-dynamic-list-input-ring). However, in classic Emacs fashion, the *Input History* buffer which displays the history is not selected automatically. I want to change this.

Trouble is, I can't get a reference for the *Input History* buffer.

My thought is to write a wrapper around the comint-dynamic-list-input-ring function which calls the command and then switches to the *Input History* buffer.

(defun my-see-shell-history ()
  "Open comint command history and switch to *Input History* buffer.

     (switch-to-buffer-other-window "*Input History*"))

This doesn't work. Nothing happens; no error, no change in buffer focus. Nada.

If I try the following, a new buffer called *Input History* is created:

  1. M-: (comint-dynamic-list-input-ring)
  2. M-: (switch-to-buffer-other-window "*Input History*")

What's confusing is that the *Input History* created by comint-dynamic-list-input-ring seems to be a buffer that doesn't exist. I can have the *Input History* buffer open on my screen (after calling C-c C-l) yet it doesn't show up in M-x list-buffers. However, I can manually select the buffer and run M-: (current-buffer) and see that it is #<buffer *Input History*>.

What's going on here? How can I reference the *Input History* buffer?

  • You can't access the input history buffer as it's an ephemeral buffer (as its name begins with space) and so the list-buffers and buffer-menu commands don’t mention it. I tried using temp-buffer-show-hook to spot this buffer and select its window but for some reason it was never selected - perhaps that's also because it's an ephemeral buffer?
    – stevoooo
    Apr 30 '19 at 12:37

Oh, we can do this just by advising the comint-dynamic-list-input-ring function:

(defadvice comint-dynamic-list-input-ring (before advice activate)
  (push ?\a unread-command-events))

(defadvice comint-dynamic-list-input-ring (after advice activate)
  (other-window 1))

The "after" advice does the actual work of switching to the other window - you might also want to move point to somewhere more useful of course. The "before" advice is required as the comint-dynamic-list-input-ring function waits for the space key to be pressed to dismiss the input history buffer. Here we just fake a different key which gets ignored and leaves the input history buffer up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.