separate searches in windows in VIM
Question
When I split VIM to show me a few files or a different parts of the same file, is there any way for me to have one search in one window and a different search in another one? For instance, I'd like the upper window to have the search pattern foo
and the lower window to have the search pattern bar
.
The active search pattern affects what is highlighted when using search highlighting, and it is pretty annoying when I have foo
highlighted in the upper window, and then I switch to the lower window and search for bar
, and foo
stops being highlighted in the upper window.
Edit: This question seems to be related, though I'm not sure it's an exact duplicate.
Solution
I use this script for highlighting multiple search patterns.
OTHER TIPS
Canopus's answer is probably the nicest way to do it if you're using this often, but if you want to do it with a vanilla vim install (or more to the point, without installing any plugins), you can simply do:
:call matchadd('Search', 'foo')
:call matchadd('Search', 'bar')
You can then clear all matches with
:call clearmatches()
There are also ways of being more fussy about what you clear (with :call matchdelete(...)
) by saving the output of matchadd to a variable. You can read more about this in :help matchadd()
and :help matchdelete()
If you're not using too complicated a syntax file (and specifically, not using the rainbow.vim highlighting), you can probably also do it with:
:syn keyword Search foo
:syn keyword Search bar
and clear it with
:syn clear Search
The only possible advantage of this that I can think of is that if there are a lot of matches that you're trying to highlight, a keyword
highlight is a lot quicker than a match
highlight (as the latter uses a regular expression search). You can still do a syn keyword
match if you're using rainbow.vim, but the command is much more complicated.