Search code examples
emacselisp

Automatically close emacs shell mode tab-completion buffer?


This has driven me crazy for a long time; I wonder if there is a way to fix it? Hopefully I can describe the situation well.

For simplicity's sake, say I've got the following directory structure: ~jer/dirA and ~jer/dirB

Within a shell within emacs, I start off in my top-level directory (~jer), type 'cd dir', and hit tab.

My window splits in 2, and I've got a *Completions* buffer. This is cool; I see that my choices are 'dirA' and 'dirB', I type an 'A' (so my full command is 'cd dirA') and hit enter, but the *Completions* buffer stays open and I have to manually close it (generally with 'C-x 1' because I'm in the shell buffer that I want to save, but if I already have a split window this is even more annoying, because the *Completions* buffer takes the place of the other one that was already there, and I have to switch to that one and hit C-x k to manually kill it).

So my question: is there a way to make the *Completions* die automatically once I finish my command? In the example above, as soon as I hit enter after typing 'cd DirA' I would want the buffer to be killed.

Thanks, and I hope this makes sense. Note, I don't think this is a duplicate of Is there any way to automatically close filename completetion buffers in Emacs?, because that's about using find-file (and in that case the *Completions* buffer does close.


Solution

  • I think this is exactly what you want.
    The delete-completion-window-buffer function will be executed every time you enter a command. It finds all current windows and gets the buffer out of it. Then it will check whether the buffer's name is "*Completions*", the buffer that makes you mad, and if so kill the buffer and delete the corresponding window.
    At last, it passes the output string to your next hook comint-preoutput-filter-functions.
    Why there is an output argument? See the comint-preoutput-filter-functions's document; better explained there.

    (defun delete-completion-window-buffer (&optional output)                                                                
      (interactive)                                                                                                
      (dolist (win (window-list))                                                                                  
        (when (string= (buffer-name (window-buffer win)) "*Completions*")                                          
          (delete-window win)                                                                                      
          (kill-buffer "*Completions*")))                                                                          
      output)                                                                                                      
    
    (add-hook 'comint-preoutput-filter-functions 'delete-completion-window-buffer)
    

    But actually, the completion buffer doesn't bother me a lot. What bothers is that the command "clear" doesn't work well. In order to solve your problem I google shell-mode, nothing there.
    But I got an solution to my problem EmacsWiki.

    (defun clear-shell ()                                                                                          
      (interactive)                                                                                                
      (let ((comint-buffer-maximum-size 0))                                                                        
        (comint-truncate-buffer)))                                                                                 
    
    (define-key shell-mode-map (kbd "C-l") 'clear-shell)                                                           
    

    I bind it to Ctrl-L, the normal terminal binding.
    Nice code. Hope you like it!