Emacs: Saving You From Yourself

Another quickie today. Evan Moses has a short post that shows how he solved a problem particular to his workflow. He’s an Evil user, which means that when he wants to save a buffer he types :w. Unfortunately, he’s prone to fat fingering that command and types :w2 instead. That writes the buffer to a file named “2” instead of the file that the buffer is visiting.

We can all relate. Most of us have some sort of similar mistyping that we’re prone to make. But this is Emacs and it’s infinitely configurable so Moses decided to save himself further grief by writing a bit of Elisp.

He simply wrote a function to check whether or not the argument to evil-write is 2. If it is, the function asks if he really wants to save to a file named 2. The function returns the result of that question.

Then he advised evil-write to run his function before writing and to abort the write if the function returns nil. You can check out the code in his post. It’s simple and exactly what you’d expect.

I’m writing about this not because I think many people are fat fingering w as w2 but because it’s another example of how Emacs’ unsurpassed configurability lets you fix small but annoying problems. Notice how simple the solution is. You don’t have to rewrite the evil-write function. You just tell Emacs to run your function first so that it can ask if saving to the file 2 is what you really want.

This entry was posted in General and tagged . Bookmark the permalink.