6

I'd like to make Emacs not just bury a buffer but kill it too when I press q in a *Completions*/*Help*/etc buffer. I can't get the substitute-key-definition function to work. I'm running Emacs24 on OSX.

Here's what I have:

(substitute-key-definition
        'quit-window '(lambda () (interactive) (quit-window "KILL")) global-map)
  • Have you got any particular reason for also wanting to kill it? – N.N. Mar 7 '12 at 17:13
  • 1
    @N.N E.g. once i've seen a completion i dont need it to be still open after quitting the window its in. – bneil Mar 7 '12 at 21:09
7

The other answer actually gives slightly incompatible behavior. Consider a situation where you already have a frame split into two windows, then you open a help buffer and quit it. quit-window is smart enough to know that it shouldn't kill the window, just the buffer. It actually even has a few more clever tricks up its sleeve so our best bet is to work with it and just tweak it a bit. Here's a simple way of achieving what you want:

(defadvice quit-window (before quit-window-always-kill)
  "When running `quit-window', always kill the buffer."
  (ad-set-arg 0 t))
(ad-activate 'quit-window)
  • 1
    Don't do this. Advising quit-window like this is BAD, nearly as bad as redefining it. It will more than likely break extensions and parts of Emacs, such as in [this example] (github.com/joaotavora/sly/pull/143#issuecomment-361030487). At least add a called-interactively-p call there. – Joao Tavora Jan 28 '18 at 12:54
  • 1
    Hi Joao, thanks for the feedback. While I appreciate the basis of your comment, I've used this every single day for at least six years and never encountered an issue. I am sorry this lead to a spurious GitHub issue on your project, but it is the nature of the beast that Emacs users will advise and even redefine to make their environment their own. Given that the risks here are quite low, I'm hesitant to call this "BAD"—though it certainly is not ideal. I do appreciate the suggestion to add called-interactively-p. – camdez Jan 28 '18 at 18:19
  • 1
    Sorry @camdez, but you can't convince me that, because it's worked for you, breaking an general purpose function's contract isn't a bad thing and/or a part of the Emacs experience. If anything, things that break sporadically are much worse than things that break frequently and reliably. Emacs has more than enough means of customization without breaking its API. The well-established fact that many people abuse Lisp's flexibility is another matter entirely. I or any other Emacs maintainer can give you many more examples where redefinition and other defadvice calls have caused mayhem. – Joao Tavora Feb 21 '18 at 0:06
2

Just redefining the function seemed to have worked for me ie

(defun quit-window () 
 "modified quit window"
 (interactive)
 (kill-buffer-and-window)
 )

I've never understood the purpose of having a completion or help buffer still available when you are switching buffers after you pressed q to quit it previously.

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.