Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 763187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:31:12+00:00 2026-05-14T16:31:12+00:00

Is there any way to search a string in a C/C++ source file while

  • 0

Is there any way to search a string in a C/C++ source file while skipping commented lines?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:31:13+00:00Added an answer on May 14, 2026 at 4:31 pm

    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:

    function! SearchWithSkip(pattern, flags, stopline, timeout, skip)
    "
    " Returns true if a match is found for {pattern}, but ignores matches
    " where {skip} evaluates to false. This allows you to do nifty things
    " like, say, only matching outside comments, only on odd-numbered lines,
    " or whatever else you like.
    "
    " Mimics the built-in search() function, but adds a {skip} expression
    " like that available in searchpair() and searchpairpos().
    " (See the Vim help on search() for details of the other parameters.)
    " 
        " Note the current position, so that if there are no unskipped
        " matches, the cursor can be restored to this location.
        "
        let l:matchpos = getpos('.')
    
        " Loop as long as {pattern} continues to be found.
        "
        while search(a:pattern, a:flags, a:stopline, a:timeout) > 0
    
            " If {skip} is true, ignore this match and continue searching.
            "
            if eval(a:skip)
                continue
            endif
    
            " If we get here, {pattern} was found and {skip} is false,
            " so this is a match we don't want to ignore. Update the
            " match position and stop searching.
            " 
            let l:matchpos = getpos('.')
            break
    
        endwhile
    
        " Jump to the position of the unskipped match, or to the original
        " position if there wasn't one.
        "
        call setpos('.', l:matchpos)
    
    endfunction
    

    Here are a couple of functions that build on SearchWithSkip() to implement syntax-sensitive searches:

    function! SearchOutside(synName, pattern)
    "
    " Searches for the specified pattern, but skips matches that
    " exist within the specified syntax region.
    "
        call SearchWithSkip(a:pattern, '', '', '',
            \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "' . a:synName . '"' )
    
    endfunction
    
    
    function! SearchInside(synName, pattern)
    "
    " Searches for the specified pattern, but skips matches that don't
    " exist within the specified syntax region.
    "
        call SearchWithSkip(a:pattern, '', '', '',
            \ 'synIDattr(synID(line("."), col("."), 0), "name") !~? "' . a:synName . '"' )
    
    endfunction
    

    Here are commands that make the syntax-sensitive search functions easier to use:

    command! -nargs=+ -complete=command SearchOutside call SearchOutside(<f-args>)
    command! -nargs=+ -complete=command SearchInside  call SearchInside(<f-args>)
    

    That was a long way to go, but now we can do stuff like this:

    :SearchInside String hello
    

    That searches for hello, but only within text that Vim considers a string.

    And (finally!) this searches for double everywhere except comments:

    :SearchOutside Comment double
    

    To repeat a search, use the @: macro to execute the same command repeatedly, like pressing n to 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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Entity Framework designer is terrible - I've had the… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If by "hijack" you meant sniff the packets then what… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If you want two actions to be atomic, embed them… May 14, 2026 at 5:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.