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.

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

URL `http://ergoemacs.org/emacs/emacs_paste_or_paste_previous.html'
Version 2017-01-11"
  (interactive)
  (progn
    (when (and delete-selection-mode (region-active-p))
      (delete-region (region-beginning) (region-end)))
    (if (eq real-last-command this-command)
        (yank-pop 1)
      (yank))))

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

〔►see Emacs: Copy/Paste and kill-ring History

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

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: Show kill-ring
  6. Emacs: Copy to Register
  7. Emacs: 1 Key to Copy/Paste from Register
  8. Emacs: Copy Rectangle Region to kill-ring
  9. Emacs: Sync Copy/Paste Clipboards with Linux X11

Enhanced Editing Commands Topic

  1. Emacs: Select Line, between Quotes, Extend Selection
  2. Emacs: Copy/Cut Current Line If No Selection
  3. Emacs: Copy/Cut All or Selection
  4. Emacs: Paste or Paste Previous
  5. Emacs: Delete Whitespace around Cursor
  6. Emacs: Move Cursor to Beginning of Line/Paragraph
  7. Emacs: Move Cursor by Text Block
  8. Emacs: Move Cursor to Bracket/Quote
  9. Emacs: Move Cursor to Punctuation
  10. Emacs: Delete Text Block
  11. Emacs: Toggle Comment Current Line
  12. Emacs: Insert Date Time
Like it? Buy Xah Emacs Tutorial. Thanks.