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'.

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

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

Emacs paste command is named yankCtrl+y】, and paste previous command is named yank-popAlt+y】.

[see Emacs: Copy/Paste and kill-ring]

xah-paste-or-paste-previous command combines them, so now you only need one key, instead of 2 keys for 2 commands.

You can set the yank key for it, like this:

(global-set-key (kbd "C-y") 'xah-paste-or-paste-previous)

[see Emacs: How to Define Keys]

Emacs Copy/Paste

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys


ELisp Examples

ELisp Write Major Mode

ELisp Examples

Xah Commands

Text Transform Under Cursor

Commands Do thing-at-point

Command to Insert Things

Script Examples