Selecting, Changing, and Deleting Blocks of Text with Vim


tags: Vim

Vim has a powerful array of movement commands. Something that only became clear to me recently is that they can be combined in absolutely wonderful ways with other commands to select, change, and delete (and quite likely other options) varying blocks of text.

There's some humour to be found in the fact that thousands of others have discovered this before me, and dozens have made web pages similar to this, all of the "oh wow vim!" variety. If I can spread the word just a little bit, great. If not, this will act as a reminder for me.

Movement commands in Vim are many and varied: w moves you forward by a word, W does the same thing but only considers whitespace as a separator. b goes the opposite direction of w, and B is the opposite of W. ) moves forward be sentences (I'll let you guess what the reverse is), and } moves forward by paragraphs.

You can combine commands with movements, so cw is change word and d} is delete paragraph. But these only act from your current cursor position forward (or backward, if you're using cW or d{). So my happy discovery of the week is cit and daw and their infinite variants. Let me explain.

HTML Tricks

cit means change inner tag. This is a fantastic thing for HTML authors. For example:

<a href="page.html">Link</a>

If your cursor is anywhere between the very first "<" and the very last ">", the cit command will delete the word "Link" and place the cursor in insert mode so you can type to replace it. If you'd rather just delete it, dit will do that. Similar commands (the logic of constructing them should rapidly become clear) include ci" at any point on top of "page.html" would empty the quotes and put you in insert mode. If you're inside a set of angle brackets, ci< or ci> will let you change their contents. It should also be noted that you can select stuff in the same manner with the v Visual command, so vit works too.

Text Tricks

daw means delete a word. I'm sure somewhere in the Vim documentation there's a very thorough explanation of what "a word" is as opposed to "inner word," but it mostly has to do with surrounding whitespace and whether or not the whitespace gets changed - or for quotes or braces or any such enclosing characters, a will have them be included in the selection while i will not. So from my point of view I'm likeliest to use daw and ciw because if I'm changing the word I still need the whitespace around it, but if I'm deleting the word the trailing space can go.

Movement Twists

Unfortunately there's not a one-to-one translation from the standard Vim movement commands you know to the "Text object selection" (this is what Vim's documentation calls this) commands we're using now. So to select a sentence you would use vas (think about it: va) has to mean select a set of parentheses). Likewise the paragraph indicator morphs from } for movement to p for selecting text objects.

These kinds of changes are one of many things that confuse the crap out of non-Vim or new Vim users, and I honestly can't blame them. I see the logic, but it's still ugly because normally s is substitute and p is paste while ) is move forward a sentence and } is move forward a paragraph ... but we've changed context and all the character meanings shift. It's nasty. But it's all about muscle memory: start practising and the power will be yours.


c2ap appears to mean change 2 paragraphs. Multipliers seem to work okay with a, but rather differently with i - give it a try.