Emacs: 1 Key to Copy/Paste from Register

By Xah Lee. Date: . Last updated: .

Here's a command that lets you copy/paste text to register 1.

(defun xah-copy-to-register-1 ()
  "Copy current line or text selection to register 1.
When no selection, copy current line, with newline char.
See also: `xah-paste-from-register-1', `copy-to-register'.

URL `http://ergoemacs.org/emacs/elisp_copy-paste_register_1.html'
Version 2017-01-18"
  (interactive)
  (let (-p1 -p2)
    (if (region-active-p)
        (progn (setq -p1 (region-beginning))
               (setq -p2 (region-end)))
      (progn (setq -p1 (line-beginning-position))
             (setq -p2 (line-end-position))))
    (copy-to-register ?1 -p1 -p2)
    (with-temp-buffer (insert "\n")
                      (append-to-register ?1 (point-min) (point-max)))
    (message "Copied to register 1: 「%s」." (buffer-substring-no-properties -p1 -p2))))

Here's paste from register 1:

(defun xah-paste-from-register-1 ()
  "Paste text from register 1.
See also: `xah-copy-to-register-1', `insert-register'.
URL `http://ergoemacs.org/emacs/elisp_copy-paste_register_1.html'
Version 2015-12-08"
  (interactive)
  (when (use-region-p)
    (delete-region (region-beginning) (region-end)))
  (insert-register ?1 t))

You should give them a easy key, such as 【Alt+3】 【Alt+4】. 〔➤see Emacs: How to Define Keys

Why the commands?

When you call {copy-to-register, insert-register}, each time you have to tell it which register to use. Rather annoying. 〔➤see Emacs: Copy to Register

Most of the time all you need is 1 register.

The single key commands xah-copy-to-register-1 and xah-paste-from-register-1 by-pass the asking and just use register 1.

This saves you 2 key strokes and a brain cycle.

Append to Register 1, Accumulate Text

Sometimes you want to append text. That is, you want to copy different parts of buffer and have them collected. For example, you want to copy all color names red, blue, green etc that's in a buffer, and paste them elsewhere.

Here's a append to register 1 command.

(defun xah-append-to-register-1 ()
  "Append current line or text selection to register 1.
When no selection, append current line with newline char.
See also: `xah-paste-from-register-1', `copy-to-register'.

URL `http://ergoemacs.org/emacs/elisp_copy-paste_register_1.html'
Version 2015-12-08"
  (interactive)
  (let (-p1 -p2)
    (if (region-active-p)
        (progn (setq -p1 (region-beginning))
               (setq -p2 (region-end)))
      (progn (setq -p1 (line-beginning-position))
             (setq -p2 (line-end-position))))
    (append-to-register ?1 -p1 -p2)
    (with-temp-buffer (insert "\n")
                      (append-to-register ?1 (point-min) (point-max)))
    (message "Appended to register 1: 「%s」." (buffer-substring-no-properties -p1 -p2))))

This command need to have a easy key to be useful. I recommend 【Alt+2】. 〔➤see Emacs: How to Define Keys

Clear Register 1

With append to register 1, sometimes you need to clear it first.

Here's command to clear register 1.

(defun xah-clear-register-1 ()
  "Clear register 1.
See also: `xah-paste-from-register-1', `copy-to-register'.

URL `http://ergoemacs.org/emacs/elisp_copy-paste_register_1.html'
Version 2015-12-08"
  (interactive)
  (progn
    (copy-to-register ?1 (point-min) (point-min))
    (message "Cleared register 1.")))

This command need to have a easy key to be useful. I recommend 【Alt+1】. 〔➤see Emacs: How to Define Keys

Copy/Paste Topic

  1. Emacs: Copy/Paste and kill-ring History
  2. Emacs: Copy/Cut Current Line If No Selection
  3. Emacs: Copy/Cut All or Selection
  4. Emacs: Paste or Paste Previous
  5. Emacs: Copy to Register
  6. Emacs: 1 Key to Copy/Paste from Register
  7. Emacs: Copy Rectangle Region to kill-ring
  8. Emacs: Sync Copy/Paste Clipboards with Linux X11
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs