Quick Commenting of Blocks in Vim

How to comment and uncomment blocks in Vim.

For a long time I have struggled with vim to comment blocks for any language or configuration file that only provides single line commenting (for me, usually python). Today a light turned on somewhere (and I got some help in code4lib from decasm), and I realized that to comment 4 lines all I have to do is

0 Ctrl-V jjj I # Ctrl-C

The Ctrl-C could be Esc, of course, but I'm used to Ctrl-C by now and there's an odd pause when I use Esc. To uncomment that text, do

0 Ctrl-V jjj d

The d could be substituted by an x, of course.

Ah, Vim. A minute to learn, a lifetime to master.

Update: As noted in my unanswered message on the vim-dev list (maybe "bug or a feature" sounded too flippant), Ctrl-C no longer works here. Too bad, as it's about the only time I hit Esc in vim.

keywords: vim, comment, python created 2007-02-02 last modified 2008-08-01