Vim search in C/C++ code lines

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

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

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 04:04

    Here is how I would proceed:

    1. Delete all C/C++ comments (using the replace command %s)
    2. Proceed to the search using regular search command /
    3. Set a mark at the position using m a (to set the mark "a")
    4. Undo the deletion of the comments using u
    5. Jump to the mark "a" using ``a`
    6. Eventually deleting the mark using delm a (it would be overwritten in the case you don't delete it, so no big deal)

    Of course you can do that in one big operation/function. I do not master Vim scripting good enough to give an example of that though.

    I admit my solution is a bit "lazy" (and you can probably do it way better) but that's all I came to.

    Hope it helps.

提交回复
热议问题