Elisp: Cut Copy Paste to/from kill-ring

By Xah Lee. Date: . Last updated: .

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))

Copy Region to Kill Ring

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

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 Elisp: 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")

Elisp: Writing Command

  1. Writing Command, Basics
  2. Mark and Region
  3. Get Buffer String
  4. Work with Lines
  5. Copy Cut Paste kill-ring
  6. Get User Input
  7. Interactive Form
  8. Get universal-argument
  9. Find Replace Text
  10. thing-at-point
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.