When I saw my colleague commenting his lines of source code with some special combo key in VC++, I would like to do that in vim too. Vim is great that it is highly flexible to extend features with your own define key combos.
Vim by default bundle with many default key bindings, fortunately you can overlay it with your own action. I find c and cc in normal mode useless for me. Therefore, I use it to performs comment and uncomment action.
What commenting action I needed? Just add a simple // to the front of each line you selected in visual mode. That means when I press select lines of code in visual mode (press v to switch to visual mode from normal mode) and hits some combos, I want all my codes commented.
Uncomment action is just the opposite.
Okay, how the key bindings looks like? Copy those line into your ~/.vimrc
:map cc v:s/^/\/\//g <CR><CR> :map cx v:s/^\/\///g <CR><CR> :vmap cc :s/^/\/\//g <CR> :vmap cx :s/^\/\///g <CR>
I am mapping cc to perform action of v:s/^/\/\//g <CR><CR>, the rest is mapped accordingly. Bare in mind for :vmap makes a constrain to key mapping, that the action will be carry on in visual mode only.
After getting the comments and fine tune the regex part, it leads to the better one
:map cc v:s!^!//!g <CR><CR> :map cx v:s!^\s*//!!g <CR>v=<CR> :vmap cc :s!^!//!g <CR> :vmap cx :s!^\s*//!!g <CR>v=
[tags]vim tips, vi, programming [/tags]