Is there any way to search a string in a C/C++ source file while skipping commented lines?
This pattern searches for a string that is not preceded by the two C++ commenting conventions. I've also excluded '*' as the first non-whitespace character, as that's a common convention for multi-line comments.
/\(\(\/\*\|\/\/\|^\s*\*[^/]\).*\)\@
Only the first and fourth foo are matched.
foo
/* foo
* baz foo
*/ foo
// bar baz foo
Putting \v at the beginning of the pattern eliminates a bunch of backslashes:
/\v((\/\*|\/\/|^\s*\*[^/]).*)@
You can bind a hotkey to this pattern by putting this in your .vimrc
"ctrl+s to search uncommented code
noremap /\v((\/\*\|\/\/\|^\s*\*[^/]).*)@