17

I need to get selection as a string in my Emacs Lisp function.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115

2 Answers2

20

Selected text is called region in Emacs universe. See How do I access the contents of the current region in Emacs Lisp?

Community
  • 1
  • 1
Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115
12

The accepted answer pointed me to the right answer. I want to leave this piece of code for more ELisp beginners like me. regionp contains the "selection" (known as region in ELisp) as a variable in the ELisp function. The if condition checks if the region is active.

(defun get-selected-text (start end)
  (interactive "r")
    (if (use-region-p)
        (let ((regionp (buffer-substring start end)))
            (message regionp))))
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Carlo Espino
  • 1,354
  • 1
  • 15
  • 21