Thursday, January 07, 2010

quick * and # search hack

A friend of mine introduced me to a quick way to use the * and # commands (which let you quickly jump to the next or previous instance of the 'word' under your cursor). Generally a word is ([A-z][0-9])+. When you use the :set lisp option though, it then includes the '-' character as well. An old school option that makes for a nice quick hack.

There is however a general solution: it turns out there is yet another thing I didn't know. You can change what a 'word' is in vim by modifying the 'iskeyword' setting.

To see what characters are considered to be part of a word you can type:

:set isk

I've found that its pretty easy to change things up when you need a slightly different usage from */#. For instance, if you are searching some keywords of the form:

final int TRANSIENT-RAT-SCH-CODE = -1;

Suppose your cursor is on the 'T'. If you hit * or # you would be brought to the next or previous instance of 'TRANSIENT' because '-' isn't in the 'iskeyword' setting. Quick fix? Do:

:set isk+=-

Then it'll match the whole big bad thing.

No comments: