Easily comment (C++) code in vim

后端 未结 4 1986
旧时难觅i
旧时难觅i 2021-02-13 12:52

I have looked at the following question:

How to comment out a block of Python code in Vim

But that does not seem to work for me. How do I comment code easily wit

相关标签:
4条回答
  • 2021-02-13 13:10

    Use ctrl-V to do a block selection and then hit I followed by //[ESC].

    Alternatively, use shift-V to do a line-based select and then type :s:^://[Enter]. The latter part could easily go into a mapping. eg:

    :vmap // :s:^://<CR>
    

    Then you just shift-V, select the range, and type // (or whatever you bind it to).

    0 讨论(0)
  • 2021-02-13 13:16

    You can add this to your .vimrc file

    map <C-c> :s/^/\/\//<Enter>

    Then when you need to comment a section just select all lines (Shift-V + movement) and then press CtrlC.

    To un-comment you can define in a similar way

    map <C-u> :s/^\/\///<Enter>

    that removes a // at begin of line from the selected range when pressing CtrlU.

    0 讨论(0)
  • 2021-02-13 13:22

    There's always #ifdef CHECK_THIS_LATER ... #endif which has the advantage of not causing problems with nested C-style comments (if you use them) and is easy to find and either uncomment or remove completely later.

    0 讨论(0)
  • 2021-02-13 13:23

    You can use the NERD commenter plugin for vim, which has support for a whole bunch of languages (I'm sure C++ is one of them). With this installed, to comment/uncomment any line, use <Leader>ci. To do the same for a block of text, select text by entering the visual mode and use the same command as above.

    There are other features in this such as comment n lines by supplying a count before the command, yank before comment with <Leader>cy, comment to end of line with <Leader>c$, and many others, which you can read about in the link. I've found this plugin to be extremely useful and is one of my 'must have' plugins.

    0 讨论(0)
提交回复
热议问题