Elisp: Get Buffer String

By Xah Lee. Date: . Last updated: .

Get String from Region

Grab text of given begin/end positions.

The property means Text Properties. Usually you no need it.

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

Get Current Word

;; return the word under cursor
(current-word)
;; usually includes lowline _ hyphen - , but really depends on current syntax table

current-word does not give you the boundary positions. You need the boundary if you need to delete the word.

Exactly what characters is considered a part of word depends on current buffer's Syntax Table. e.g. whether low line _ or hyphen - count as part of word.

Get boundary of word by syntax table, add hyphen

;; return a vector [begin-position end-position]
;; that is the boundary of word, with hyphen considered part of word.
;; word means any char with word syntax in syntax table of current buffer.
;; plus hyphen (manually added in the code).
(vector (progn (skip-chars-backward "-[:word:]") (point))
        (progn (skip-chars-forward "-[:word:]") (point)))

Get Boundary of Word by Manually Specify Word Characters

;; get boundary of word by manually specify word characters.
;; you can control whether hyphen or lowline characters are included or not
;; return a vector [begin-position end-position]

;; note, this won't work if the word contain chinese chars or other language

;; 2026-01-05

(let ((case-fold-search nil))
  (save-excursion
    (vector
     (progn (skip-chars-backward "_a-zA-Z0-9") (point))
     (progn (skip-chars-forward "_a-zA-Z0-9") (point)))))

Get Current Line

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

Get File Path, URL

Get Text Between Quote or Brackets

Grab the current text between quotes or brackets.

;; return the boundary of text between QUOTATION MARK (codepoint 34) on each side of cursor.
;; return a vector [begin-position end-position]
(vector
 (progn (skip-chars-backward "^\"") (point))
 (progn (skip-chars-forward "^\"") (point)))

Get Current Char

char-before

return the integer ID of character before cursor.

char-after

return the integer ID of character after cursor.

Reference

Elisp, Convert String, Buffer

Elisp, text processing functions