ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

Emacs: Using Registers (Multiple Clipboards)

Xah Lee,

If you are new to emacs, you may not know that emacs's clipboard (called “kill-ring”) actually contains all copy history.

• To paste, you can press 【Ctrl+y】. After that, you can press 【Alt+y】 to paste previous copied content. You can repeat 【Alt+y】 for earlier content. In ErgoEmacs, the keys are 【Ctrl+v】 and 【Ctrl+⇧ Shift+v】.

Multiple Clipboards: Emacs Register

Emacs has a “register” concept. Emacs's “register” is like a clipboard, it lets you store text in them, and paste from them. In a sense, it's like all of a sudden you have tens of different clipboards. (it can also store cursor location, file path, ….) Here's how to use:

Saving Keystrokes For Register

If you use register to store/paste text often, the following commands are useful:

(defun copy-to-register-1 ()
  "Copy current line or text selection to register 1.
See also: `paste-from-register-1', `copy-to-register'."
  (interactive)
  (let* (
         (bds (get-selection-or-unit 'line ))
         (inputStr (elt bds 0) )
         (p1 (elt bds 1) )
         (p2 (elt bds 2) )
         )
    (copy-to-register ?1 p1 p2)
    (message "copied to register 1: 「%s」." inputStr)
))

(defun paste-from-register-1 ()
  "paste text from register 1.
See also: `copy-to-register-1', `insert-register'."
  (interactive)
  (insert-register ?1 t))

(The above code requires “xeu_elisp_util” at Emacs Lisp: get-selection-or-unit)

You can give them a single key, such as {F7, F8}. 〔☛ Emacs: How to Define Keys

About 3 years ago, i wrote and use these commands. But a few months ago, i tried to use emacs's keys for register, because: ① emacs's default commands can deal with multiple registers, or store cursor location, file names, etc (which i haven't used yet) It's more general. Sometimes i do need to use a second register. ② It requires 2 extra key-press for each copy or paste. I thought it's not too bad. ③ One less maintenance of my personal elisp commands.

After using the emacs default way for half a year, i find it not as convenient as using the above commands. So, i went back to my old way. Here's the rational:

  1. Although this works with only just one register, but i find that ≈95% of time i just need to use one single register.
  2. When using emacs register command, you need to enter a name. This adds a tiny micro-burden on the brain.
  3. When i use a register, often the pattern is to put text in it, then paste 5 or 10 times in a short period. Saving 2 keystrokes per paste is quite convenient.
  4. Copy current line if there's no text selection, is quite convenient. Saves about 4 keystrokes.
  5. When i need to use a second register, i can still rely on emacs register command.

For other use of registers, see: (info "(emacs) Registers").

Elisp tip: When coding the register command, you need to represent a character for the register name. A character in elisp is just a integer (represented by its Unicode code point in decimal.) But integer is hard to understand as a char. A easy way to represent char is to start with a question mark. For example, the integer for char “1” is 49. So, instead of writing (insert-register 49), you should write (insert-register ?1) ((info "(elisp) Basic Char Syntax"))

blog comments powered by Disqus