Emacs Windowing Improvements
One element of Emacs’s behavior that annoyed me for quite a while is
Emacs’s handling of windows (or rather, the natural consequence of
having multiple windows which arrive at various times). Often I end up
with a random window that I really don’t want to be there. Usually the
window is occupied by a temporary buffer (or something like *info*
)
which has outstayed its welcome.
In most cases point is not in the window, so I have to switch to the other window and then kill the buffer with C-x o C-x k RET. But then I still have the window sitting around occupying space in front of the code I was actually trying to look at. So next I eliminate the window with C-x 0.
You see why this is annoying. I’m using seven keystrokes (C-x o C-x k RET C-x 0) just to clean up the mess from a temporary buffer. Of course this is Emacs, so a solution is only an elisp hack away. So I defined two new functions which relieve my woes:
(defun delete-window-replacement (&optional; p)
"Kill current window. If called with PREFIX, kill the buffer too."
(interactive "P")
(if p
(kill-buffer nil))
(delete-window))
(defun delete-other-windows-replacement (&optional; p)
"Make the selected window fill its frame. If called with PREFIX,
kill all other visible buffers."
(interactive "P")
(if p
(dolist (window (window-list))
(unless (equal (window-buffer window) (current-buffer))
(kill-buffer (window-buffer window)))))
(delete-other-windows))
I bind these in place of the original delete-window (C-x 0) and delete-other-windows (C-x 1):
(global-set-key "C-x0" 'delete-window-replacement)
(global-set-key "C-x1" 'delete-other-windows-replacement)
My functions work exactly as the built-ins they replace unless they are given a prefix argument (in fact they call the original functions). So now, if point is in a window I don’t want, which contains a buffer I don’t want, all I have to do is C-u C-x 0 and I’m back to my original work. Or, if my cursor is sitting in my original code, and the other buffer is the problem, I type C-u C-x 1. It’s natural enough that I’d like to see it in the default Emacs distribution.