Useful Packages for Buffer and File Handling in Emacs
I recently discovered a set of useful packages and commands that facilitate some common file and buffer handling scenarios. Despite being really simple, all provide a real improvement over the standard, built-in alternatives, at least for me.
Changing Files and Buffers Simultaneously
The bufferfile package provides functions to rename or delete a buffer and its associated file (or, equivalently, a file and all of its associated buffers) in a single command.
(global-set-key (kbd "C-x C-r") 'bufferfile-rename)
(global-set-key (kbd "C-x C-v") 'bufferfile-delete)
Creating Untitled Buffers
Occasionally, I need a scratch pad, to jot something down — and when I need it, I need it now, without having to think about it: after all, the purpose is to catch what’s on my mind, not to set up a new project. The untitled-new-buffer package is very helpful here: it creates, as the name suggests, a new, untitled buffer (in the existing “window”). Very handy, because it frees me from having to think about filenames or directory locations — in particular for a buffer that may never even be saved to a file!
(global-set-key (kbd "C-x C-n") 'untitled-new-buffer)
Closing Buffers and Their Windows
Emacs does not really have the notion of a “dialog box”: a temporary window, displaying an ephemeral buffer. Anything that pops up (which for Emacs means: a buffer in a window), even for a temporary task, such as displaying a help message, will loiter around until it is explicitly closed — both the window and the buffer.
I am not a fan of Emacs “windows” to begin with, because they destroy my visual experience: they tend to pop up and obscure whatever I was working on, and then they don’t disappear again or go to the background; instead I must close them explicitly. The same goes for buffers: Emacs temporary buffers (for Help, Messages, Completions, what-have-you) persist and pollute the space of actual buffers I am working on and want to switch between.
So, I was pleased to find two simple functions on the EmacsWiki that roll all of that into a single command, each. Each closes a window and kills the associated buffer; one acts on the “current” window, the other on the “next” window.
(defun close-and-kill-this-pane ()
"If there are multiple windows, then close this pane and kill the buffer in it also."
(interactive)
(kill-this-buffer)
(if (not (one-window-p))
(delete-window)))
(defun close-and-kill-next-pane ()
"If there are multiple windows, then close the other pane and kill the buffer in it also."
(interactive)
(other-window 1)
(kill-this-buffer)
(if (not (one-window-p))
(delete-window)))
I bound them as follows:
(global-set-key (kbd "C-x C-u") 'close-and-kill-this-pane)
(global-set-key (kbd "C-x C-q") 'close-and-kill-next-pane)
The Key Bindings
I spent some time thinking about the key bindings: all of the functionality described here is strictly convenience functionality, if it is to achieve its purpose, then the key bindings need to be equally convenient, both manually, and, more importantly, cognitively. (In other words, easy to remember, for me.)
I associate the prefix C-x very much with file handling: C-x C-i,
C-x C-s, C-x C-w, C-x i, C-x s, but also with buffer
handling: C-x b, C-x k, C-x 0, C-x 1. Hence, I wanted a
C-x prefix. That left the second key to select.
In keeping with the established pattern for actions that act on
individual buffers and files (such as C-x C-f, C-x C-s, C-x C-w),
I wanted the second key to also be prefixed with C-. So, which keys
were available and made some sort of mnemonic sense?
The key combinations C-x C-r and C-x C-n are by default bound to
find-file-read-only and set-goal-column — two actions I have
never used (and, in fact, didn’t even know about). Their mnemonics are
very clear: rename and new. That was easy.
The combination C-x C-v is usually bound to find-alternate-file:
it replaces the current buffer with the new file. (I guess the idea
is that this is mostly useful if you accidentally opened an incorrect
of possibly non-existent file.) Again, I had never used this before
(easier to kill the buffer and load a new file than to remember this
command), and hence available to delete file and buffer — not
something I do all the time, hence the less intuitive mnemonic
“vanquish”.
This left the commands to kill a window and its buffer. The
combination C-x C-q (“QUIT!") was an obvious fit for the
most common scenario of closing the other window —
most likely one of those pesky Emacs Help or Messaging jobs.
(By default bound to toggle read-only mode: something I have
never done, and will likely never need.) And finally, to kill this
window, I use C-x C-u, with the idea that I put the window
and its buffer “under”. (By default, this combo is bound to
“upcase-region”, which is apparently such an unintuitive
function that, again by default, it is disabled — hence
essentially freeing up this combination of keys.)