pixelastic
pixelastic

Reputation: 5278

How to remove the highlight from a search inside an `operator-pending` mapping in vim ?

I'm trying to define an operator-pending mapping (using onoremap) to match all text between the previous { and the next }. This is for use in a css file, so I can easily delete, change or copy rules with ir.

I've used the syntax used in "Learn vimscript the hard way", which is a combination of execute and normal. What I have been unable to do is clear the highlight search after my last search. After using the mapping, all the } in the file get highlighted.

So far, here's what I got :

au Filetype css noremap <buffer> ir :<C-U>execute "normal! ?{\rjV/}\rk"<CR>

I know I need to call :nohlsearch but I don't know how to fit that in the mapping. The mapping results in a text being visually selected, so when :nohlsearch gets called it's applied to the selection, which does not work.

I need a way to clear the '<,'> markers but I couldn't find how to write <C-U> in the normal command to get it executed right. Another way might be to leave Visual mode, execute the :nohlsearch and then reselect the last selected text with gv, but even for that I can't find how to write <Esc> in my normal statement.

Upvotes: 0

Views: 160

Answers (1)

Tom Whittock
Tom Whittock

Reputation: 4230

In this particular case, it's probably easier to use something like:

au Filetype css noremap <buffer> ir :<C-U>call search('{', 'Wb')\|call search('}', 'Ws')\|normal! v''<CR>

Note that this does what your example does, not the right thing. Nested braces for example won't be handled correctly.

Calling the search function without the 's' flag won't set any marks or change the search register so you don't need to reset anything.

Note that this isn't really the best way of accomplishing what you want - you probably want the iB or i{ (they're the same) text object instead.

Upvotes: 3

Related Questions