Commenting out multiple lines in Vim without external plugins

Written by Johannes on May 17, 2013 Categories: Python, Vim

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

One minor pet peeve I had with Vim has been resolved by this neat little trick. Commenting out multiple lines in Python has always been somewhat of a chore for me in Vim. Thankfully, it is actually quite easy:

  • Hit Ctrl+V
  • Select the lines you want to comment out (don’t use the mouse for that)
  • Hit “0″ + “I” (capital i) to write simultaneously at the beginning of the selected lines
  • type “#”
  • Hit “ESC”

Update: it’s actually even easier (thank you to Tobi for pointing out this most elegant use case of search and replace in Vim):

  • Hit Shift+V to select the lines you want to comment out.
  • Enter “:s/^/#/g” to search and “replace” (in this case insert) # at the linestart.

Job’s done. One very neat little trick.

No Comments

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" escaped="">