Function: yank-pop

yank-pop is an interactive and byte-compiled function defined in simple.el.gz.

Signature

(yank-pop &optional ARG)

Documentation

Replace just-yanked stretch of killed text with a different stretch.

The main use of this command is immediately after a yank or a yank-pop. At such a time, the region contains a stretch of reinserted ("pasted") previously-killed text. yank-pop deletes that text and inserts in its place a different stretch of killed text by traversing the value of the kill-ring variable and selecting another kill from there.

With no argument, the previous kill is inserted. With argument N, insert the Nth previous kill. If N is negative, it means to use a more recent kill.

The sequence of kills wraps around, so if you keep invoking this command time after time, and pass the oldest kill, you get the newest one.

You can also invoke this command after a command other than yank or yank-pop. This is the same as invoking yank-from-kill-ring, including the effect of the prefix argument; see there for the details.

This command honors the yank-handled-properties and yank-excluded-properties variables, and the yank-handler text property, in the way that yank does.

View in manual

Probably introduced at or before Emacs version 22.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun yank-pop (&optional arg)
  "Replace just-yanked stretch of killed text with a different stretch.
The main use of this command is immediately after a `yank' or a
`yank-pop'.  At such a time, the region contains a stretch of
reinserted (\"pasted\") previously-killed text.  `yank-pop' deletes
that text and inserts in its place a different stretch of killed text
by traversing the value of the `kill-ring' variable and selecting
another kill from there.

With no argument, the previous kill is inserted.
With argument N, insert the Nth previous kill.
If N is negative, it means to use a more recent kill.

The sequence of kills wraps around, so if you keep invoking this command
time after time, and pass the oldest kill, you get the newest one.

You can also invoke this command after a command other than `yank'
or `yank-pop'.  This is the same as invoking `yank-from-kill-ring',
including the effect of the prefix argument; see there for the details.

This command honors the `yank-handled-properties' and
`yank-excluded-properties' variables, and the `yank-handler' text
property, in the way that `yank' does."
  (interactive "p")
  (if (not (eq last-command 'yank))
      (yank-from-kill-ring (read-from-kill-ring "Yank from kill-ring: ")
                           current-prefix-arg)
    (setq this-command 'yank)
    (unless arg (setq arg 1))
    (let ((inhibit-read-only t)
          (before (< (point) (mark t))))
      (if before
          (funcall (or yank-undo-function 'delete-region) (point) (mark t))
        (funcall (or yank-undo-function 'delete-region) (mark t) (point)))
      (setq yank-undo-function nil)
      (set-marker (mark-marker) (point) (current-buffer))
      (insert-for-yank (current-kill arg))
      ;; Set the window start back where it was in the yank command,
      ;; if possible.
      (set-window-start (selected-window) yank-window-start t)
      (if before
          ;; This is like exchange-point-and-mark, but doesn't activate the mark.
          ;; It is cleaner to avoid activation, even though the command
          ;; loop would deactivate the mark because we inserted text.
          (goto-char (prog1 (mark t)
                       (set-marker (mark-marker) (point) (current-buffer))))))
    nil))