Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Vim, I often find myself performing one search immediately followed by another search. I am wondering if I can combine the two searches into one command.

For example, if I want to find the first instance of the foobar variable within a function named transmogrify, I would enter:

/def transmogrify
/foobar

Or if I'm in a log file and I want to search for an error that happened after a specific date:

/2012-07-26
/ERROR

Is there a better way to do this? And no, I'm not looking to record a macro.

share|improve this question
up vote 8 down vote accepted

Easiest might be, e.g.

/def transmogrify/;/foobar
share|improve this answer

You can use the | symbol to do an OR operation when searching.

Taking your second example, for example:

/2012-07-26\|ERROR

Even better, if you have these set in your vimrc:

" Make search expressions "very magic"
nnoremap / /\v
vnoremap / /\v

Then you don't have to escape the |. E.g.:

/\v2012-07-26|ERROR
share|improve this answer
1  
In my brain that wouldn't work because it should stop once it finds the first term because that makes the whole statement true, but in real life it probably does work. – Rob Jul 26 '12 at 15:53
    
@Rob is right; this didn't work when I just tested it. Thanks, though! – arussell84 Jul 26 '12 at 15:56
    
It sort of works. It will get you what you want, you'll have to navigate through all the foobar or error though. It doesn't not work because of what I said, but because it matches both terms, which is what the | is for here. – Rob Jul 26 '12 at 15:59
    
Yes, I see what you mean. I misunderstood what you were asking for. – kenny Jul 26 '12 at 16:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .