Say I have multiple blocks of the following code in a file (spaces is irrelavent):
sdgfsdg dfg
dfgdfgf ddfg
dfgdfgdfg dfgfdg
How do you find/highlight all the occurrences?
What I ideally want to do is to visually select the code block and then press search to find all occurrences.
Quick and dirty partial solution:
The
hlsearch
option (on by default in some vim configs, but I always turn it off) makes vim highlight all found instances of the current search. Pressing*
in normal mode searches for the word under the cursor. So this will highlight all instances of the word under the cursor.Maybe you should look at : Search for visually selected text
I've taken it from here
Try this. Include this script somewhere in your runtimepath (see
:help runtimepath
). A simple option would be to put it in your vimrc. Visually select the thing you want to search for and press,/
(the comma key and then the forward-slash key).The text being searched for is stored in the
/
register. You can't yank or delete directly into this register, but you can assign to it using `let'.Try this:
"ay
to yank that highlighted selection into registera
:let @/ = @a
to copy registera
into the search register/
At this point, all code matching your selection will be highlighted, and you can navigate through occurrences using n/N just as you would a regular search.
Of course, you can use any temporary register instead of
a
. And it shouldn't be too difficult to get this command sequence mapped for easy use.