Comments in Vim – The Effective way

about a 2 minute

Two days ago, a colleague of mine came to me with a question. He just started using Vim, so he’s a total beginner. He needed to comment random lines in a big configuration file. Another colleague of mine already recommended him a great plugin vim-commentary.

The problem was, that my colleague was a beginner with Vim. He still didn’t know how to install a Vim plugin, and he wanted to complete his work quickly. So no time for learning how to install a plugin and how to use it.

As I always try to spread the word about Vim as much as I can, I quickly came up with solution which worked for him. He actually really liked it. (and we got another Vim user! :) )

You might find it useful, when you need to comment or uncomment lots of random lines. And you don’t have or don’t want the help of any plugin.

All you need to do is to add these two lines to your .vimrc:

map ic :s/^/#/g<CR>:let @/ = ""<CR>
map rc :s/^#//g<CR>:let @/ = ""<CR>

ic (insert comment) will insert # at the beginning of the line.
rc (remove comment) will remove # at from the beginning of the line.

Change ic and rc to fit your needs, and enjoy ;)

Over the years, Vim got a reputation that it’s really difficult to learn it. I’ve heard many times from guys who are convinced it will take them months to reach proficiency. That’s simply wrong.

That’s why I’ve started to write a book: Mastering Vim Quickly (from WTF to OMG in no time) which will teach you Vim the way I learned it – easily and quickly.

If you want to get updates and sample content from the book, leave me your email below, and I’ll make sure to keep you updated. You could also go to Mastering Vim Quickly page and check it out.

If you liked the post, we should get connected - follow me on Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *