Search code examples
vimhighlight

Highlight keyword only on specific lines


Sometimes I just want to highlight the keyword on specific lines. Since the keyword is really common, it can appear everywhere in the file.

For instance, I'm searching for 1s on lines starting with check in the following file:

...
[block-1]
test 31 for instruction block1_test
stim 011000011100101
check xxxxx1xxxx1xxxx
...

Using /1 will highlight every 1, and makes it a bit annoying to find the one I want.

Though using /^check\>.*1 narrowers the search result, it matchs from the very beginning to the last 1 on that line.

I'd like to make all the 1s on check lines clearer to see, to find. Can I achieve this?


Solution

  • You can use a look-behind operator \@<= in your pattern to match 1 but only in lines that start with check.

    This search pattern does what you requested:

    /\(^check\>.*\)\@<=1
    

    Using "very magic" option :h magic:

    /\v(^check .*)@<=1