automatically close completions in Emacs shell (comint) mode

I recently started running shells inside Emacs via shell mode, and so far I’m pretty happy with it. I’ve found a number of things to tweak, though, including the completions buffer, which is great except that it overstays its welcome and doesn’t close when you’re done with it.

I searched the web and found Dmitriy Igrishin’s message to gnu.emacs.help. He kindly provided me a patch against Emacs 23.2’s comint.el, which I translated into the advice below. Put it in your .emacs and you should be good to go!

(defun comint-close-completions ()
  "Close the comint completions buffer.
Used in advice to various comint functions to automatically close
the completions buffer as soon as I'm done with it. Based on
Dmitriy Igrishin's patched version of comint.el."
  (if comint-dynamic-list-completions-config
      (progn
        (set-window-configuration comint-dynamic-list-completions-config)
        (setq comint-dynamic-list-completions-config nil))))

(defadvice comint-send-input (after close-completions activate)
  (comint-close-completions))

(defadvice comint-dynamic-complete-as-filename (after close-completions activate)
  (if ad-return-value (comint-close-completions)))

(defadvice comint-dynamic-simple-complete (after close-completions activate)
  (if (member ad-return-value '('sole 'shortest 'partial))
      (comint-close-completions)))

(defadvice comint-dynamic-list-completions (after close-completions activate)
    (comint-close-completions)
    (if (not unread-command-events)
        ;; comint's "Type space to flush" swallows space. put it back in.
        (setq unread-command-events (listify-key-sequence " "))))

2 thoughts on “automatically close completions in Emacs shell (comint) mode

  1. It works! The ugly completion buffer disappears ! Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *