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.
See also: `xah-paste-from-register-1', `copy-to-register'.

URL `http://ergoemacs.org/emacs/elisp_copy-paste_register_1.html'
Version 2017-01-23"
  (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)
    (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"
  (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.

Copy Append to Register

Emacs: Copy Append Text

Copy/Paste Topic

  1. Copy/Paste and kill-ring History
  2. Copy/Cut Current Line If No Selection
  3. Copy/Cut All or Selection
  4. Paste or Paste Previous
  5. Show kill-ring
  6. Copy to Register
  7. 1 Key to Copy/Paste from Register
  8. Copy Append
  9. Sync Copy/Paste Clipboards with Linux X11

Elisp Commands Do thing-at-point

  1. Count Words
  2. Run Current File
  3. Delete Current File
  4. Copy File Path
  5. Convert Line Ending
  6. Make Backup
  7. Copy Column of Text to Clipboard
  8. Convert Image File
  9. Dired Rename Space to Hyphen
  10. Python 2to3 Wrapper
  11. Move Code to Files
  12. Update Web Feed
  13. Updating Page Tags
  14. Single Key Copy/Paste Register

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp