开发者

Easily comment (C++) code in vim

I have looked at the following question:

How to comment out a block of Python code in Vim

But th开发者_StackOverflowat does not seem to work for me. How do I comment code easily without resorting to plugins/scripts?


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).


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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜