5

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.

9

Easiest might be, e.g.

/def transmogrify/;/foobar
1

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
4
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.