Work, Hobbies, Me

Vim Tip #4 – Moving around on a line

Posted: February 6th, 2009 | Author: chyatt | Filed under: Development | Tags: , | No Comments »

Today I found myself repeatedly wanting to modify the last word of the url in a in a line of code like this:

$base_url = 'http://www.example.com/shared';

For the purposes of this example, say I wanted to replace the word shared with the word sample.

There are probably many ways to achieve this.  Based on last week’s experience, perhaps this is most appropriate:

:s/shared/sample

But again I found myself automatically wanting to make use of an even earlier tip.  I wanted to navigate quickly to that last word and use ‘cw’ to change it.  I couldn’t figure out how to do this quickly so I decided to look it up.  Here are some options:

$2bcw
$bbcw

Both of these options move the cursor to the end of the line ($), then back 2 words (2b or bb), then change word (cw).  Of particular interest to me here was the “b” command, which complements the “w” command nicely.