Heads up! I've posted a follow up to this tip that details a quicker way to comment in vim by using a simple function. Check it out here.
From the “holy crap why didn’t I find this before” category…
When editing in vim, if you find you need to comment out a block of code, try the following:
- Jump to the start of the line at the top or bottom of the block
Ctrl+vto enter Visual Block mode
- Navigate to the end of the block. You’ll notice only the first column is highlighted (for the most part)
Ito enter Insert mode before the first non-blank character on the line
- Type whatever the comment syntax is for your language, eg. “//”
Escto get back to Normal mode.
You can follow similar steps to uncomment the same block,
Ctrl+v to highlight the comment characters followed by
x or any deletion command to remove the comment.