How can I search in vim for a string containing 2 specific words?

I want to find a string that has both "foo" and "bar" in this order, but not necessarily next to each other.

I tried the following and it did not work:

/foo.*bar 
+6
vim
source share
3 answers

Using:

 :set magic /foo.*bar 

The magic parameter determines how VIM handles special characters in regular expressions. When VIM processes all characters literally, this means that the expression you wrote foo.*bar will really look for this string. However, when β€œmagic” is turned on, special regular expression characters take on a special meaning, and it works more as you expect. It is always recommended to use :set magic , if not dealing with really old Vi scripts, so just add it to your vimrc and you will be installed.

+11
source share

Works for me:

 /text.*text2 
+5
source share

h: search-pattern

1. A pattern is one or more branches separated by the "\ |" symbol. It corresponds to all that corresponds to one of the branches.

Example: foo\|beep matches "foo" and matches "beep". If there are several branches, the first is used.

2. A branch is one or more concat, separated by \& . It matches the last concat, but only if all previous concats also match in the same position.

Examples:
foobeep\&... matches "foo" in "foobeep".
.*Peter\&.*Bob matches a string containing both "Peter" and "Bob"

0
source share

All Articles