Over at the Emacs subreddit, kosakgroove has just learned about Emacs registers and considers it a game changer. His post is little more than a headline so all the action is in the comments. Surprisingly—at least to me—many of the commenters were unaware that registers even existed.
For me, registers are useful in two situations:
- I want to save my current window configuration so that I can run some function—eshell, for example—in full screen, and reinstate my previous window configuration when I’m done.
- Saving values or counters in keyboard macros.
For example, here’s how I run eshell
:
(global-set-key (kbd "H-e") (lambda (exit) "Bring up a full-screen eshell or restore previous config. With a prefix argument, exit eshell before restoring previous config." (interactive "P") (if (string= "eshell-mode" major-mode) (progn (when exit (insert "exit") (eshell-send-input)) (jump-to-register :eshell-fullscreen)) (progn (window-configuration-to-register :eshell-fullscreen) (eshell) (delete-other-windows)))))
The important thing to notice in this example is the use of :eshell-fullscreen
as the register key. That’s a trick I learned from Magnar Sveen. In interactive use, the key has to be a single character but because of an implementation artifact, any symbol will do from within Elisp. That’s nice because you don’t have to worry about having a user write something over your saved window configuration. I use this same trick a lot in my init.el
.
Here’s an example of using a counter register in a keyboard macro. Both of these example are a bit specialized. You can simply save some text or a position in a register and insert it later when needed. Registers really are an almost magical power and it pays to get familiar with them.