I used to use IDE and I’m sure all of you familiar with command Ctrl + /
or Cmd + /
to add comment to your codes.
And here is how to bring that magic to VIM. Put these following lines to your .vimrc
" Commenting blocks of code.
autocmd FileType c,cpp,java,scala let b:comment_leader = '// '
autocmd FileType sh,ruby,python let b:comment_leader = '# '
autocmd FileType conf,fstab let b:comment_leader = '# '
autocmd FileType tex let b:comment_leader = '% '
autocmd FileType mail let b:comment_leader = '> '
autocmd FileType vim let b:comment_leader = '" '
noremap ,cc :silent s/^/=escape(b:comment_leader,'\/')/:nohlsearch
noremap ,cu :silent s/^\V=escape(b:comment_leader,'\/')//e:nohlsearch
Now you can select your text and type ,cc
to comment and ,cu
to uncomment.
Reference: https://stackoverflow.com/a/1676672/1936697