Steve
Steve

Reputation: 388

Set iskeyword selectively

Often I need to search large xml mode files for the next occurrence of the word under the cursor but preferably not if it's a tag the closing tag

In the below example # is where the cursor is. Using * or # with iskeyword not including > or < will move between <Dealid> and </Dealid>.

<Deali#d>4444</Dealid>
... 50 lines
<Dealid>6666</Dealid>
... n lines
<Dealid>5643</Dealid>

I tried :set iskeyword+=< which worked fine for moving to the next match but it changed vey to include the < of the closing tag so moving to the start of 6666, for eg, and typing vey will yank 6666< rather than 6666.

Anyway of getting the best of both words or limit the iskeyword+=< to the * and # operators?

Upvotes: 3

Views: 193

Answers (2)

Peter Rincker
Peter Rincker

Reputation: 45147

Instead of changing 'iskeyword' maybe use a visual star search. This will allow you to visually select the text and then hit *. e.g. va<*

If you don't want to use a plugin you can put the following mapping in your ~/.vimrc file:

xnoremap * y/\V<c-r>=escape(@", '\')<cr><cr>

Note: this mapping clobbers the unnamed register so it would probably be best to use a more advanced mapping or a plugin.

Upvotes: 0

FDinoff
FDinoff

Reputation: 31439

This should do what you want.

nnoremap * :set iskeyword+=<<CR>*:set iskeyword-=<<CR>
nnoremap # :set iskeyword+=<<CR>#:set iskeyword-=<<CR>

It sets changes iskeyword before executing * or # and then resets it.

Upvotes: 5

Related Questions