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

(info "(elisp) Buffer Contents")

Get Text Selection (Active Region)

[see Elisp: Region, Active Region]

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 Elisp: Syntax Table Tutorial]

Here's how to control exactly what word you want be. 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, …}.

;; 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 Elisp: Using thing-at-point]

Grab Between Matching Pairs

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 Line, between Quotes, Extend Selection

Reference

(info "(elisp) Buffer Contents")

Writing Emacs Command

  1. Elisp: How to Write Commands
  2. Elisp: Region, Active Region
  3. Elisp: Get Buffer String
  4. Elisp: Functions on Line
  5. Elisp: Cut Copy Paste to/from kill-ring
  6. Elisp: Get User Input
  7. Elisp: Get universal-argument
  8. Elisp: Find Replace String in Buffer
  9. Elisp: Using thing-at-point
Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs