This page describes library thing-cmds.el, which defines various commands that use ThingAtPoint things.

The commands select various Lisp objects and text entities that are at or near point (cursor position), or they move point to such an object or text entity. The commands are described individually below.

Completion or cycling is available to choose the type of thing to use. Pretty much all thing types are completion candidates. User option ‘thing-types’ determines the types that are available for cycling.

Key Bindings

If you invoke command ‘thgcmd-bind-keys’ (e.g. in your init file) then the following key bindings are created:


Command ‘select-things’ (alias `mark-things`) selects, that is marks, successive things, starting at point. Point is moved to the beginning of the enclosing thing (or to the end, if you use a negative prefix argument). Mark is put at the same place that command ‘forward-’thing would put it using the same prefix argument.

If the region is not active, you are prompted for the type of thing to use – completion is available. The default type for completion is the first element in user option ‘thing-types’ (which, by default, is `sexp’).

If the region is active, you are not prompted, and the last-used type of thing is used again. On consecutive uses of ‘select-things’, the region is active, so you are not prompted, and the region is extended to successive things of the same type.

This is true even if the region is empty – as long as it is active, it is extended using the last-used type of thing, without any prompting. You can thus just hit ‘C-SPC’ to set mark and activate an empty region, then use ‘select-things’ to select successive things of the last type used.


Command ‘cycle-thing-region’ selects a single thing near the cursor. You are not prompted for the type of thing. Instead, successive uses of the command select a different type of thing. The first type of thing selected is determined by the first element in option ‘thing-types’ (which, by default, is `sexp’).

In TransientMarkMode, you can follow this with ‘select-things’ to select successive things of the same type, but to do that you must first use ‘C-x C-x’ (‘exchange-point-and-mark’): Using the bindings created by command ‘thgcmd-bind-keys’, this becomes `M-@ C-x C-x C-M-SPC’. This command has an alias: ‘select-thing-near-point’.

Here’s a variant of cycle-thing-region that iterates over ‘thing-types’ until either it finds some thing near point, or the list is exhausted.

  (defun cycle-thing-region-with-continue ()
    "Select the first thing from `thing-types' found near point.
 Successive uses select the next thing from the list to be found near point.
 On each use, the command will iterate over the list until something is found.
 This can be really slow when `thing-types' is long and so is the buffer."
    (if (eq last-command this-command)
        (goto-char thgcmd-thing-region-point)
      (setq thgcmd-thing-region-index  0
            thgcmd-thing-region-point   (point)))
    (let ((success nil))
      (if (= thgcmd-thing-region-index (length thing-types))
          (setq thgcmd-thing-region-index 0))
      (while (and (not success) (< thgcmd-thing-region-index (length thing-types)))
        (setq thing (elt thing-types thgcmd-thing-region-index))
        (setq thgcmd-thing-region-index  (1+ thgcmd-thing-region-index))
        (setq success (thing-region thing))
        (when success
          (setq thgcmd-last-thing-type  (intern thing))
          (message "%s" (capitalize
                         (elt thing-types (1- thgcmd-thing-region-index))))))))


Like ‘cycle-thing-region’, command ‘thing-region’ selects a single thing near the cursor. Unlike ‘cycle-thing-region’, you are prompted for the type of thing – completion is available. The cursor is placed at the end of the region. You can return it to the original location by using ‘C-u C-SPC’ (once or twice, depending on the thing type).

`mark-enclosing-list', `mark-enclosing-list-forward', `mark-enclosing-list-backward'

Command ‘mark-enclosing-list’ selects a list that surrounds the cursor. If you repeat the command it selects a wider enclosing list each time you repeat it.

If you use a numeric prefix argument then the selection is that many lists up, surrounding point. 1 means select the immediately enclosing list. 2 means select the list immediately enclosing that first enclosing list, and so on. A negative prefix argument puts point at the beginning of the new region instead of the end.

This command moves point, and the starting position is added to the mark ring before doing anything else, so you can easily return to that position (e.g. using ‘C-u C-SPC’).

Commands ‘mark-enclosing-list-backward’ and ‘mark-enclosing-list-forward’ are the same, but they specifically put point at the end and beginning of the region, respectively.

`next-visible-thing', `previous-visible-thing', `next-visible-thing-repeat', `previous-visible-thing-repeat'

Command ‘next-visible-thing’ moves point to the start of the next visible thing of a given type. Things that are currently invisible (using text property ‘invisible’) are ignored.

If user option ‘ignore-comments-flag’ is non-‘nil’ then things that are commented out are also ignored. They are, in effect, temporarily hidden while scanning for the next thing. If you also use library Icicles then you can use `C-M-;’ in the minibuffer to toggle this option.

Command ‘previous-visible-thing’ is the same, except that it moves backward, to the first visible thing before point.

These commands prompt you for the type of thing to use, but only the first time they are invoked in succession. So if you bind them to simple repeatable keys, such as ‘f11’ and ‘f12’, then you can hit those keys over and over to move forward and back successive things.

Library thing-cmds.el does not bind these two commands, because simple repeatable keys are rare (mostly taken already).

However, if you invoke command thgcmd-bind-keys (e.g. in your init file) then similar commands, ‘next-visible-thing-repeat’, ‘previous-visible-thing’, are bound to keys ‘C-x up’ and ‘C-x down’, respectively.

You can repeat movement in either direction without being prompted for the thing type after the first invocation: `C-x up up up up...’. However, unlike bindings for ‘next-visible-thing’ and ‘previous-visible-thing’, if you change direction you are prompted again for the thing type. That’s simply due to definition of command ‘repeat’, which is used for the implementation.

Libraries referenced here: Lisp:thing-cmds.el

CategoryCode CategoryComments CategoryRegion