Comment And Uncomment Lines in Vim

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

Posted in Vim

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.