Thursday, May 15, 2008

Start of Match

When searching or writing macros in Vim, it's sometimes helpful to have a search position your cursor at the end of the matching term rather than the beginning. You can use the start of match atom to accomplish this.

/some term\zs

This would match "some term" and position your cursor at the end of the match. See (help :regexp) for other atoms and general regexp goodness.

Thanks to Chris Sutter for contributing this tip.

1 comment:

Rich Cheng said...

An alternative way to do this is:

/some term/e

This will leave the cursor on the final character of the term (as opposed to after it, with your version).

It has the advantage that, if you're using hlsearch, the search term will be highlighted, rather than the character following.