Emacs Lisp: Get Buffer String

By Xah Lee. Date: . Last updated: .

Get String from Region

Grab text of given begin/end positions.

;; return string between position 3 to 99
(buffer-substring-no-properties 3 99)

Get Text Selection

(buffer-substring-no-properties (region-beginning) (region-end))

[see Emacs Lisp: Region, Active Region]

Get Current Char

char-before
return the unicode codepoint (integer) of character before cursor. [see Emacs Lisp: Character Type]
char-after
return the unicode codepoint (integer) of character after cursor.

Get Current Word

;; return the identifier under cursor
;; this is actually current symbol
(current-word)
;; usually includes underscore _ , may include hyphen -, dollar $, etc, depending on current syntax table

;; return the word cursor is on, usually not including underscore _
(current-word t t)

Exactly what characters is considered a part of word depends on current buffer's syntax table.

For example, if you have $ite▮m_blue-red, and cursor is before m, result is one of the following:

If you are beginner in elisp, don't worry about syntax table. The current major mode usually sets the syntax table correctly for programing language identifiers.

[see Emacs Lisp: Syntax Table]

Here's how to control exactly the sequence of string you want. Suppose, you want any letter A to Z, a to z, 0 to 9, and including LOW LINE _, but exclude HYPHEN-MINUS -.

(defun my-get-word ()
  "print the word under cursor.
Word here is any A to Z, a to z, and low line _"
  (interactive)
  (let (
        p1
        p2
        (case-fold-search t))
    (save-excursion
      (skip-chars-backward "_a-z0-9" )
      (setq p1 (point))
      (skip-chars-forward "_a-z0-9" )
      (setq p2 (point))
      (message "%s" (buffer-substring-no-properties p1 p2)))))

Get Current Line

;; return current line as string
(buffer-substring-no-properties (line-beginning-position) (line-end-position) )

Get Thing at Point

thing-at-point is a way to get the “thing” under cursor.

The thing can be {word, symbol, line, sentence, URL, file name, etc}.

;; grab a “thing” at point. The “thing” is text unit. It can be 'word 'symbol 'list 'sexp 'defun 'filename 'url 'email 'sentence 'whitespace 'line 'number 'page

;; grab the current filename
(setq str (thing-at-point 'filename))

Sometimes, you need to not just grab current word, but do other things such as delete the word. You need to know the beginning and ending positions of the region you are interested.

Use bounds-of-thing-at-point

[see Emacs Lisp: thing-at-point]

Get Text Between Brackets

Grab the current text between delimiters such as between angle brackets <…>, parens (…), double quotes "…", etc.

The trick is to use skip-chars-backward and skip-chars-forward. In the following example, the p1 is set to the position of the double quote to the left of cursor (the first char to the right of the quote). Similarly, for p2 to the right of cursor.

(defun my-select-inside-quotes ()
  "Select text between double straight quotes
on each side of cursor."
  (interactive)
  (let (p1 p2)
    (skip-chars-backward "^\"")
    (setq p1 (point))
    (skip-chars-forward "^\"")
    (setq p2 (point))

    (goto-char p1)
    (push-mark p2)
    (setq mark-active t)))

More examples: Emacs Select Word Command Problem

Reference

Emacs Lisp, Convert String/Buffer