Emacs Lisp: Cut Copy Paste to/from kill-ring

By Xah Lee. Date: . Last updated: .

These are operations that manipulate the kill-ring, which is user's copy/paste storage area.

Normally, you should not modify the kill-ring, unless your command is designed to put something in there for user. If you want to store some text, store them in a local variable [see Emacs Lisp: Variable]

Copy Region to Kill Ring

;; push text between buffer positions to kill-ring
(copy-region-as-kill 1 100)

Kill Region to Kill Ring

;; delete text between buffer positions and push to kill-ring
(kill-region 247 528)

String to Kill Ring

If you already have a string, use kill-new

;; push a string into kill-ring
(kill-new "cute cat")

Append String to Kill Ring

;; append string to the latest in kill-ring
(kill-append "cute cat" nil)
;; if second arg is t, do prepend

Paste from Kill Ring

;; paste from kill-ring
(yank)

Mark a Region

To mark a region, do (push-mark positon). The current cursor positon to positon will become the new region.

To make the region active, use (setq mark-active t).

[see Emacs Lisp: Region, Active Region]

(defun my-select-text-in-quote ()
  "Select text between the nearest left and right quotes."
  (interactive)
  (let ($pos
        ($skipChars "^\""))
    (skip-chars-backward $skipChars)
    (setq $pos (point))
    (skip-chars-forward $skipChars)
    (push-mark $pos)
    (setq mark-active t)))

(info "(elisp) The Kill Ring")


Practical Elisp ⭐

Writing Command

Text Processing

Get User Input

File/Buffer

Writing Script