Emacs: Paste or Paste Previous 📜

By Xah Lee. Date: . Last updated: .

Here's a command that paste text, and if repeated, paste the previous item.

If a numerical argument is given, paste that many times.

(defun xah-paste-or-paste-previous ()
  "Paste. When called repeatedly, paste previous.
This command calls `yank', and if repeated, call `yank-pop'.

If `universal-argument' is called first with a number arg, paste that many times.

URL `http://xahlee.info/emacs/emacs/emacs_paste_or_paste_previous.html'
Created: 2017-07-25
Version: 2020-09-08"
  (interactive)
  (progn
    (when (and delete-selection-mode (region-active-p))
      (delete-region (region-beginning) (region-end)))
    (if current-prefix-arg
        (progn
          (dotimes (_ (prefix-numeric-value current-prefix-arg))
            (yank)))
      (if (eq real-last-command this-command)
          (yank-pop 1)
        (yank)))))

Set Up Key

this command combines paste (yank) and paste previous (yank-pop) into one command, so now you only need one key, instead of 2 keys for 2 commands.

You can set the normal yank command's key for it.

Emacs, copy paste