Is there any way to search a string in a C/C++ source file while skipping commented lines?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is an intriguing question.
I think @sixtyfootersdude has the right idea — let Vim’s syntax highlighting tell you what’s a comment and what’s not, and then search for matches within the non-comments.
Let’s start with a function that mimics Vim’s built-in
search()routine, but also provides a “skip” parameter to let it ignore some matches:Here are a couple of functions that build on
SearchWithSkip()to implement syntax-sensitive searches:Here are commands that make the syntax-sensitive search functions easier to use:
That was a long way to go, but now we can do stuff like this:
That searches for
hello, but only within text that Vim considers a string.And (finally!) this searches for
doubleeverywhere except comments:To repeat a search, use the
@:macro to execute the same command repeatedly, like pressingnto repeat a search.(Thanks for asking this question, by the way. Now that I’ve built these routines, I expect to use them a lot.)