Selecting element content with Vim

When editing an XML file in Vim, I wanted to delete the content of an element so that I could replace it with some completely new text. I thought I could use ct< to change up until the next < character but that didn’t work because the end tag was on a subsequent line.

I learned I could use c/t<CR>, but I use hlsearch and that left me with all of my < characters highlighted. Yuck.

Then I stumbled upon the it motion. A quick cit and all of the content inside the current tag (no matter where the cursor actually is) gets deleted and I’m ready to replace. Perfect!

Comments (4)

  1. Jan! wrote::

    Ah, yes. Good old inner/outer motions. As a quick reminder, use cat to change everything including the opening and closing tag (“outerHTML”, anyone?), vit to select the inner contents, yit to copy the contents, etc.

    The motions also work for quotes (ci") and the like.

    I learned a lot from “VIM for PHP programers”: http://zmievski.org/2007/02/vim-for-php-programmers-slides-and-resources – it is not limited to PHP.

    Friday, May 28, 2010 at 2:28 am #
  2. Jason Diamond wrote::

    Hi Jan. After I posted my tip, I found myself using ci’ and ci(. Don’t know how I lived withot them before.

    I just skimmed through the first dozen slides of the talk you linked and I can tell I’ll learn a lot from it. Thanks!

    Friday, May 28, 2010 at 11:56 am #
  3. Despite thinking I’m some kind of relatively experienced Vim user, I’m embarrassed to say I had no clue bout any of the ci*, vi*, or yi* stuff before, and had never used any of them at all. But now that I’ve found out a bit, I’m can see I’m absolutely going to be using this stuff all the time (though I’ve got a bit more reading up of the docs and trying out to do first…).

    Anyway, thanks immensely for taking the time to post this tip.

    Tuesday, August 31, 2010 at 11:43 pm #
  4. Jason Diamond wrote::

    Hi Mike. I’ve been using Vim for years and still think I only know a small fraction of what it can do. I’m glad you found the tip useful!

    Wednesday, September 1, 2010 at 11:06 am #