The Power and Precision of Vim’s Text Objects: Efficent, Elegant, Awesome.

Vim’s text objects are not only a powerful, flexible and precise way to specify a region of text, but also intuitive and efficient.
They can be used with any command that can be combined with a motion (e.g., “d“, “y“, “v“, “r“), but in this post I will be using the “c” command (“change”) to illustrate them.

Imagine you were on a line looked like this, with the cursor on the letter “r” of the word “dry”:

print “Enter run mode (‘test’, ‘dry’, or ‘full’)”

Then, after typing “c” to start the “change” command, you can type “i” or “a” followed by another character to define a text object to which to apply the “change”.

Using “i” gives you the “inner” text object (i.e., a less-inclusive or non-greedy version that excludes the wrapping characters or delimiters) while “a” gives the full object (i.e., a more-inclusive or greedy version, that includes the wrapping characters or delimiters).

The third and final character of the command sequence gives the criteria by which the text object is defined.

This is typically the initial letter of the name of the object (e.g., “w” for “word”, “s” for “sentence”) or a character that delimits/wraps a region of text (e.g. a parenthesis for text in parentheses, a quote for quoted text, a curly brace for text in curly-braces).

For example:

  • Typing “ci'” will delete the word [dry], not including the quotes, and enter insert mode:

    From:

    print “Enter run mode (‘test’, ‘dry’, or ‘full’)”

    To:

    print “Enter run mode (‘test’, ‘ ‘, or ‘full’)”
  • Typing “ca'” will delete the word [‘dry’], including the quotes, and enter insert mode:

    From:

    print “Enter run mode (‘test’, ‘dry’, or ‘full’)”

    To:

    print “Enter run mode (‘test’,  , or ‘full’)”
  • Typing “ci(” or “ci)” will delete everything inside the parentheses, but not the parentheses themselves, and enter insert mode:

    From:

    print “Enter run mode (‘test’, ‘dry’, or ‘full’)”

    To:

    print “Enter run mode ( )”
  • Typing “ca(” or “ca)” will delete the everything inside the parentheses as well as the parentheses themselves, and then enter insert mode:

    From:

    print “Enter run mode (‘test’, ‘dry’, or ‘full’)”

    To:

    print “Enter run mode  
  • Typing “ci"” will delete everything inside the double-quotes, but not the double-quotes themselves, and enter insert mode:

    From:

    print “Enter run mode (‘test’, ‘dry’, or ‘full’)”

    To:

    print  
  • Typing “ca"” will delete everything inside the double-quotes, as well as the double-quotes themselves, and enter insert mode:

    From:

    print “Enter run mode (‘test’, ‘dry’, or ‘full’)”

    To:

    print  
<p>The idiom is naturally extended to many types of different delimiting characters in the most intuitive way possible (i.e., simply by specifying the delimiting character or first character of the name of the text object):
</p>

<ul>
    <li>"<code>i{</code>" or "<code>i}</code>" selects curly-brace surrounded text, with the greedier versions being "<code>a{</code>" and "<code>a}</code>", respectively.
    <li>"<code>i[</code>" or "<code>i]</code>" selects square-bracket surrounded text, with the greedier versions being "<code>a[</code>" and "<code>a]</code>" respectively.
    <li>"<code>i'</code>" selects single-quote surrounded text, with the greedier version being "<code>a'</code>".
    <li>"<code>it</code>"  selects the text within the surrounding HTML/XML tag or container, with the greedier version being "<code>at</code>".
    <li>"<code>iw</code>"  selects the surrounding word, with the greedier version being "<code>aw</code>".
    <li>"<code>is</code>"  selects the surrounding sentence, with the greedier version being "<code>as</code>".
    <li>"<code>ip</code>"  selects the surrounding paragraph, with the greedier version being "<code>ap</code>".
    <li> etc.
</ul>

<p>
Once you start using Vim's <a href="http://vimdoc.sourceforge.net/htmldoc/motion.html#object-select">text objects</a>, there really is no going back.
The power and precision they provide to specify regions of text to pass onto other Vim commands makes for an extremely efficient and elegant <i>modus operandi</i>, for which there simply is no equivalent in any other text editor.

For more information, please refer to the Vim documentation, either online, or by typing “:help text-objects” inside Vim.

Leave a Reply

Your email address will not be published. Required fields are marked *