VIMTIP! i & a's Double Life

In Normal mode, i & a will put you into Insert mode (i starts you at the cursor position & a starts you after the cursor), but did you know if you use them with an operator of some sort, ex: c, v, or d, they make grabbing all the text within an element or the element itself a piece of cake? If you didn’t, you’re in for a treat!

i grabs the text “inside” an object. a captures the object itself and any surrounding whitespace.

Let’s take a look at an example:

<h3>Beyonce's Greatest Hits</h3>

Say I need to change the text within the <h3>: I could make my way to the beginning of line ^, w over to the B, start Visual mode v, w my way through the text and change or delete from there. Or I can just make sure my cursor is in that line somewhere and enter vit. That’ll select everything within the tags in way less keystrokes! vat would select the whole <h3> element.

These commands are able to get into more than just html tags. Here are a few of the more useful ones I’ve been using:

  • viw selects the word your cursor is currently over
  • vaw selects the word and trailing whitespace
  • vis selects the entire sentence your cursor is inside of.
  • vas selects the sentence and trailing whitespace
  • vi( selects the text inside the parenthesis. This also works with any other text grouping containers – ex: ( [ { < ‘ “ `
  • va( selects from opening parenthesis to the closing one and any trailing whitespace. Also works with other text groupings.

I used v for these examples so you’d be able to see what it’s actually selecting but swap v out for a c or d and you’ll be ripping out text faster than a double-click delete! Vim has the nitty gritty here: :help text-objects

As my grandad always used to say:

You know I i’d my way into your grandmother’s heart, don’t ya?

Happy Vim'ing!

comments powered by Disqus