Vim: Text objects

What's a text object?

Text objects are a remarkably useful feature of Vim. They allow you to perform a command inside or around the specified object, where an object is a piece of text with some kind of semantic structure, like a word, paragraph, quoted string, HTML tag, etc.

Examples

  1. gqip to reflow text (gq) inside the current paragraph (ip)
  2. gUiw to convert inside the current word (iw) to uppercase (gU)
  3. ci" to change inside the current matching pair of double quotes
  4. da{ to delete inside and around the current matching pair of curly braces

Default text objects

See :help text-objects for Vim's excellent built-in documentation on text objects.

Plugins

There are various Vim plugins that define new kinds of text objects, or otherwise super-power text objects.

vim-indent-object

vim-indent-object provides text objects for indented text.

vim-textobj-comment

vim-textobj-comment provides text objects for comments, defined by the comments and commentstring settings for the given filetype. It works for /* paired */ and // simple comment delimiters.

vim-surround

vim-surround doesn't provide text objects, but it does provide powerful utilities for surrounding text objects.