Emacs Tip: Registers

Table of Contents

img

The Simple Genius of Emacs Registers

It’s been in emacs since the beginning, and is not had in many other editors, yet many people don’t know about it. Registers. The documentation talks about many types, most of which I haven’t tried yet. The three I use the most, however, are:

  • Text in register. Suppose I want a chunk of text but am doing something that will pollute my kill ring, so I can’t just yank (paste) every time. Stick the text in a register with selecting the text and C-x r s SOME-KEY and later just do C-x r i SOME-KEY (insert-register) to pull it out. Sometimes I stick it on a key representing the thing I am storing, but often I put it on the p key because my fingers and mind like doing C-x rip; it feels satisfying. I use it often when doing fast twitter-messaging, storing the common boilerplate (the post’s hashtags, @ references, etc) in a register I can keep using until the talk I’m live-tweeting is over.

  • Windows in register. Window setups can be carefully crafted: I might have four panes, one of my DB, one of my code, one or two of my REPLs, and one of my browser. It’s my dashboard view. But sometimes I need to leave the dashboard and focus on just one of those. Window registers to the rescue: I save my view with C-x r w SOME-KEY and come back with C-x r j SOME-KEY (jump-to-register) when I’m done with my focused work. Again, this is for when I’m doing stuff that pollutes my stack from just doing a winner-undo to go back to my last screen. It’s faster and more light-weight than doing any of the fancy Perspective or Desktop libraries, which exist if you need persistent memory of screens (I don’t). WARNING: multiple buffers visiting the same file will only save one of the locations, so I would lose my place in that case. Which leads us to…

  • Point in register: if you’re working in multiple file locations (in the same file or many), especially the case when I’m saving screen real-estate (like when I was an undergrad in college and worked on a 10-inch Dell Netbook Mini): save the location of your cursor with C-x r <space> SOME-KEY (point-to-register) and then go back any time with C-x r j SOME-KEY (jump-to-register). These days I usually have lots of screen real-estate and so accomplish this by just splitting my screen (C-x 2) to leave a cognitive bookmark in front of my eyes, but when my boundaries exceed that real-estate, this is still the easy winner.

  • I haven’t ever tried number registers, but I also just saw that they have special features like incrementing the number in the register, so you can do actions with counts. Cool!

  • If you’re a pro with macros but only need them for one session, it looks like macro registers are for you

Finally, if I ever need a review of all the registers I’ve saved, I just do C-x r j and wait a moment, and I see a helpful reminder of what is in my registers, even the ones (like saved strings) that aren’t jumpable. This preview is a mini-version of M-x list-registers.

img

Resources

Tory Anderson avatar
Tory Anderson
Full-time Web App Engineer, Digital Humanist, Researcher, Computer Psychologist