Elisp: Cut Copy Paste, kill-ring

By Xah Lee. Date: . Last updated: .

What is Kill Ring

Emacs kill-ring stores a history of copied text.

💡 TIP: you should not add or modify kill-ring, unless your command is designed to copy something in there for user. If you want to store some text, get the text and store them in a local variable.

Copy Region to Kill Ring

copy-region-as-kill
(copy-region-as-kill BEG END &optional REGION)

copy a text block from BEG END to kill-ring.

If REGION is true, copy region instead.

;; push text between buffer positions to kill-ring
(copy-region-as-kill 1 100)
;; copy current region to kill-ring
(copy-region-as-kill 1 100 t)
;; first 2 args are ignored

Cut Region to Kill Ring

kill-region
(kill-region BEG END &optional REGION)

cut a text block from BEG END to kill-ring.

If REGION is true, copy region instead.

;; delete text between buffer positions and push to kill-ring
(kill-region 247 528)
;; delete current region and push to kill-ring
(kill-region 1 1 t)
;; first 2 args are ignored

String to Kill Ring

kill-new
(kill-new STRING &optional REPLACE)

push string to kill-ring

if REPLACE is true, replace the first item in kill-ring

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

Append or Prepend, String to Kill Ring

;; append string to the latest in kill-ring
(kill-append "cute cat" nil)

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

Paste from Kill Ring

;; paste from kill-ring
(yank)

Mark a Region

To mark a region, do

The current cursor positon to positon will become the new region.

[see Elisp: Mark, Region, Active Region]

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

Reference

Elisp, text processing functions