Elisp: thing-at-point

By Xah Lee. Date: . Last updated: .

thing-at-point is a function that grabs the current word, file path, url, etc and return a string.


(thing-at-point THING &optional NO-PROPERTIES)

Return a string that's the current THING under cursor. THING should be any of

  • 'filename
  • 'url
  • 'word
  • 'symbol
  • 'number
  • 'whitespace
  • 'line
  • 'sentence
  • 'page
  • 'list
  • 'sexp
  • 'defun
  • 'email
  • 'uuid
(defun ff ()
  "print current word."
  (message "%s" (thing-at-point 'word)))

Get Boundary Positions of a Thing

Sometimes you also need to know a thing's boundary (the begin and end positions) , because you may need to delete it.


Return a Cons Pair that's the boundary positions of the text unit under cursor.

(defun my-get-boundary-and-thing ()
  "example of using `bounds-of-thing-at-point'"
  (let (xbounds xpos1 xpos2 xthing)
    (setq xbounds (bounds-of-thing-at-point 'symbol)
          xpos1 (car xbounds)
          xpos2 (cdr xbounds)
          xthing (buffer-substring-no-properties xpos1 xpos2))
     "thing begin at [%s], end at [%s], thing is [%s]"
     xpos1 xpos2 xthing)))

thing-at-point and Syntax Table

The exact meaning of “thing”, depends on current buffer's Syntax Table.

Emacs Lisp, Get Thing at Point