tags:

views:

336

answers:

4

Hey Folks,

Is there a way to search for multiple strings simultaneously in Vim? I recall reading somewhere that it was possible but somehow forgot the technique.

So for example, I have a text file and I want to search for "foo" and "bar" simultaneously (not necessarily as a single string, can be in different lines altogether).

How do I achieve that?

Thank you

A: 
/(foo|bar)
Paul Betts
by default you need to escape bars and parentheses: /\(foo\|bar\)but either way, in this case no need to group them! /foo\|bar
wilhelmtell
A: 

Vim supports regular expressions by starting in command mode with a '/'.

So using something like "/(foo\|bar)" (as was stated before) would solve the problem. It's good to know why that works and what you are using (regular expressions).

ChronoPositron
+11  A: 
/^joe.*fred.*bill/          : find joe AND fred AND Bill (Joe at start of line)
/fred\|joe                  : Search for FRED OR JOE
Codeslayer
This works as well. And is much simpler than the paranthetical answer I gave below
AJ
If only I found this earlier... :(
ojblass
+3  A: 

Actually I found the answer soon after I posted this (yes I did google earlier but was unable to locate it. Probably was just searching wrong)

The right solution is

/(foo\|bar)

@Paul Betts: The pipe has to be escaped

AJ
I think that escaping the pipe is required in some RegExp dialects and not others.
Steve Moyer
it is required in vi
jop
Yes ... I agree that it's needed in vi, but the escaping of characters is terribly inconsistent and attempting to directly copy that fragment into another implementation must be undertaken with care.
Steve Moyer
Damn it, so close!
Paul Betts
Yeah it is inconsistent. I will have to check but I don't think Vi follows PCRE (perl compatible regular expressions) where we would not have to
AJ
You need to escape the parens too (in vi(m)).And there are many, many different regex's out there. Vim's is derived from vi, which in turn is derived from ex. It's terribly inconsistent about special characters, but you can use \v or \V to change the behavior.
Zathrus