Vim search in C/C++ code lines

前端 未结 4 898
再見小時候
再見小時候 2020-12-24 03:27

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

4条回答
  •  感动是毒
    2020-12-24 04:09

    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*\*[^/]).*)@

提交回复
热议问题