Vim Tips Wiki

Search for a C-style for statement

Redirected from VimTip1320

1,624pages on
this wiki
Add New Page
Talk0 Share
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:


Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki