centos – Highlight for two strings simultaneously in vi or less editor?

To search for either foo or bar, it’s

/foo|bar

in less where less regexps extend POSIX extended (egrep-like) regexps where | is an alternation operator.

And:

/foo\|bar

Or /vfoo|bar in vim where vim regexps extend the vi regexps which themselves extend POSIX basic regexps where | alone matches a literal |.

awk '/foo/ && /bar/ would match on lines that contain both foo and bar. Searching a foo provided it’s found on a line also containing bar or a bar provided it’s found on a line also containing foo could be done in vim using its look-around regexp operators:

 /\vfoo(.*bar)@=|(bar.*)@<=foo|bar(.*foo)@=|(foo.*)@<=nbar

That is \v for very-magic where many more operators or recognised without the need of a leading \, and either:

  • foo provided it’s followed by any amount of characters and bar
  • foo provided it’s preceded by bar followed by any amount of characters
  • bar provided it’s followed by any amount of characters and foo
  • bar provided it’s preceded by foo followed by any amount of characters

To turn on highlighting of search matches in vim:

:set hls

Read more here: Source link