Skip to content

Instantly share code, notes, and snippets.

@ultim8k
Last active June 19, 2024 21:53
Show Gist options
  • Save ultim8k/d8326a0cd7646356acf0dc3baf8e78ff to your computer and use it in GitHub Desktop.
Save ultim8k/d8326a0cd7646356acf0dc3baf8e78ff to your computer and use it in GitHub Desktop.
(un) comment multiple lines vim

From: http://stackoverflow.com/questions/1676632/whats-a-quick-way-to-comment-uncomment-lines-in-vim

For those tasks I use most of the time block selection.

Put your cursor on the first # character, press Ctrl``V (or Ctrl``Q for gVim), and go down until the last commented line and press x, that will delete all the # characters vertically.

For commenting a block of text is almost the same: First, go to the first line you want to comment, press Ctrl``V, and select until the last line. Second, press Shift``I``#``Esc (then give it a second), and it will insert a # character on all selected lines. For the stripped-down version of vim shipped with debian/ubuntu by default, type : s/^/# in the second step instead.

@BautistaJorge616
Copy link

BautistaJorge616 commented Feb 21, 2024

If you hit : vim and shows something like :'<,'> you need to do something like this:

SHIFT + v and select the code block
: you are gonna see something like this :'<,'> add to see :'<,'>s/^/#/ and hit enter

@Hemrajbhattarai
Copy link

This worked for me
:%s/^/#/g

@cwwaism what if you just want a few lines rather than entire file? Visual blocks easier than line numbers.

If you just want for certain lines do this. This will comment lines 7 to 10 and you can change it accordingly.
:7,10s/^/#/g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment