Vim Tips Wiki

Search for a C-style for statement

1,612pages on
this wiki

Redirected from VimTip1320

Tip 1320 Printable Monobook Previous Next

created 2006 · complexity intermediate · author QBerrt · version 5.7

Q: How to do a search that will find both of the following examples?

for (
  int i=0;


for ( int i=0; i<3; i++)

A: Use this pattern

\<for\>[ ^I\n]\{-0,}([ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*[ ^I\n]\{-0,})


  • \<for\> //Match the word "for"
  • [ ^I\n]\{-0,} //Match any whitespace (space " ", tab "^I", newline "\n") 0 or more times with a non-greedy search (the negative makes it non-greedy)

Why go to all this trouble instead of searching for \<for\>?

Let's say I wanted to find all places where I do a for loop against MAX_INT. I could say:

/\<for\>[ ^I\n]\{-0,}([ ^I\n]\{-0,}.*;[ ^I\n]\{-0,}.*MAX_INT.*;[ ^I\n]\{-0,}.*[ ^I\n]\{-0,})


Another possibility using \_, which adds newline matching to various patterns:


May or may not satisfy your requirement exactly, but works for the given examples.

This may suffice as well for the first 2 examples:


Around Wikia's network

Random Wiki