Elisp: Cut Copy Paste to/from kill-ring

By Xah Lee. Date: . Last updated: .

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)

Note: User's kill-ring content should not change unexpectedly. Emacs lisp function should not use the kill-ring as temp storage of text. If your command is specifically designed to put text to the kill-ring so that user can paste it later, then good. Else, just save text to a variable for your elisp program's use. e.g. (setq x (buffer-substring-no-properties pos1 pos2))

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

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 Elisp: Region, Active Region]

(defun my-select-text-in-quote ()
  "Select text between the nearest left and right quotes."
  (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")

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



Basic Functions

Writing Command

Writing Script

Lisp Data Structure

Lisp Symbol

Elisp Misc