Vim Tip of the Day: The Truth About Nouns

Nouns commonly refer to objects (“cat”, “house”), but Vim nouns typically indicate navigations in connection with some object and aren’t simple references (e.g. e = “end of the word”, w = “start of the next word”). This is a clue that there’s something funny about these “nouns.” It turns out that the nouns we employ when talking about text — words, paragraphs, and so on — aren’t exactly the nouns that Vim itself operates on, which is ranges. Whenever we describe an action via the Vim language in terms of nouns that make sense to us, Vim translates that into an implied character range. For instance, when you say e, Vim understands that to mean, “from here to the end of the word.” It then takes action on that range if you’ve also indicated a verb.

Why do we care about this? For one thing, sometimes, the nouns you are interested in are simple references but can’t be expressed as simple motions. You’d like to say “this paragraph,” and Vim should understand that you mean, “this paragraph which the cursor is in right now, of course.” In such cases, the noun we seek to manipulate is not expressible as a motion, but it is still a range, and Vim gives us a second kind of linguistic noun (in addition to motions) to indicate these, called “text objects.”

To use these, just point at the objects with the cursor, and then leverage the concepts “in” (i) and “around” (a) to manipulate them. For example, if you were referring to quoted text, in would indicate the text inside quotes, and around would indicate the text including the quotes. More examples:

Change in word = ciw
Delete around paragraph = dap (this one is handy!)
Delete in paragraph = dip (can also be used to collapse whitespace)
Yank (copy) sentence = yis
Change in parens = cib or ci( — either open or close paren works
Change in box = ci[
Change in quotes = ci"
Select in parens = vib (we’ll learn more about (v)isual mode later)
Change in tag = cit
etc.

Try other variations on your own!

Note that we use p and s here for paragraph and sentence, respectively, since { and ( refer to the actual delimiters in this case. Also, unlike the usual nouns (w, }, etc.), these “text objects” cannot be used on their own without verbs, since they aren’t navigations.

🌈 😌 👆

6 comments

    1. sid

      Do you mean something like this?

      <item>
          <inner>
              hello
          </inner>
          also hello
      </item>

      Depending on where you position your cursor, cit will change the contents of either the inner tag or the enclosing item tag.

  1. eric

    Excellent post! I’d never really played with i/a but now have and cannot believe I have been missing this functionality all this time! Thank you.

    1. sid

      Hi Eric, you are very welcome and I’m really glad to hear that the post helped you discover these. There’s so much good stuff in Vim hiding right under one’s nose!

  2. judd

    Thanks for the reminder about text objects. I should have been using them to modify the tons of json I had to manage!

    I scraped by with `bufdo s/”old”/”new”/g` but there were always the leftovers…

    1. sid

      Nice, you mean something like :bufdo g/pattern/norm ... with text objects instead? Sounds like a nifty way to do it. Glad you found it useful 🙂

Leave a Reply

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