Skip to content

Instantly share code, notes, and snippets.

@ultim8k
Last active May 11, 2026 00:42
Show Gist options
  • Select an option

  • Save ultim8k/d8326a0cd7646356acf0dc3baf8e78ff to your computer and use it in GitHub Desktop.

Select an option

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.

@Skatox
Copy link
Copy Markdown

Skatox commented Dec 8, 2022

Thanks for sharing this info.

@Pari-singh
Copy link
Copy Markdown

The above instruction comments out only 1 line at a time. Which you could just do by simply Shift + "I" for insert and then add # in front of the line.
I tried both your mentioned methods inc : s/^/# both just comment 1 line. If you also could provide solution for multiple line comment, that would be very helpful.

@cwwaism
Copy link
Copy Markdown

cwwaism commented Feb 20, 2023

This worked for me

:%s/^/#/g

@mateu
Copy link
Copy Markdown

mateu commented Mar 8, 2023

The above instruction comments out only 1 line at a time. Which you could just do by simply Shift + "I" for insert and then add # in front of the line. I tried both your mentioned methods inc : s/^/# both just comment 1 line. If you also could provide solution for multiple line comment, that would be very helpful.

One has to make sure to press Esc after inserting the # and that will add all the other #'s after the first line.

@ultim8k
Copy link
Copy Markdown
Author

ultim8k commented Mar 8, 2023

I just copy-pasted the solution from stack overflow for future reference. I remember it worked for me at the time.

@himanshugirdharwal
Copy link
Copy Markdown

How to uncomment now?

@mateu
Copy link
Copy Markdown

mateu commented Mar 16, 2023

How to uncomment now?

Select the same way as before and then press x to delete the selected characters.

@ahmedsmaali5
Copy link
Copy Markdown

Thanks a lot, now I can flex more while using vim.

@jahooker
Copy link
Copy Markdown

jahooker commented Oct 8, 2023

The above instruction comments out only 1 line at a time. Which you could just do by simply Shift + "I" for insert and then add # in front of the line. I tried both your mentioned methods inc : s/^/# both just comment 1 line. If you also could provide solution for multiple line comment, that would be very helpful.

@Pari-singh, are you certain that you are using Ctrl-v and not just Shift-v? I myself misread that part, and out of muscle memory used Shift-v. On re-reading, I realised my mistake. Shift-v (i.e. V) is VISUAL LINE mode while Ctrl-v is VISUAL BLOCK mode.

@sureshvv
Copy link
Copy Markdown

sureshvv commented Nov 19, 2023

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.

@sureshvv
Copy link
Copy Markdown

@jahooker why do you need capital I? lower case i does not seem to work?

@jahooker
Copy link
Copy Markdown

@jahooker why do you need capital I? lower case i does not seem to work?

Not sure, @sureshvv.
That's just how Vim works.
See https://stackoverflow.com/questions/12399572/vim-how-to-insert-in-visual-block-mode and https://vim.fandom.com/wiki/Inserting_text_in_multiple_lines.

@Tweekism
Copy link
Copy Markdown

Tweekism commented Jan 5, 2024

@jahooker why do you need capital I? lower case i does not seem to work?

Its because in block selection mode, your selection might be, for example: 7 lines long by 3 columns wide. That makes it ambiguous where vi should place the cursor for inserting.

Shift-I means place the cursor on the left hand side of the block selection
Shift-A means place the cursor on the right hand side of the block selection

These commands of course come from Normal mode, where they mean:-
Shift-I Move to the start of the line and enter insert mode
Shift-A Move to the end of the line and enter insert mode

@BautistaJorge616
Copy link
Copy Markdown

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
Copy Markdown

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

@Popcorn-Cola
Copy link
Copy Markdown

It wont work ,after entering # all lines are unselected

@eljaymane
Copy link
Copy Markdown

Very useful tip !

@aquarios77
Copy link
Copy Markdown

For me "Ctrl + v" > "Shift + i" > "#" > "Esc" sequence did not work iv VI , but worked in VIM (on the same OEL 8 host)

@feliperafaelbarbosa
Copy link
Copy Markdown

Thanks for this info!

@kumargaurav6
Copy link
Copy Markdown

TDEE Checker is an online TDEE calculator that estimates your Total Daily Energy Expenditure based on details like age, weight, height, gender, and activity level. It helps you understand how many calories your body needs each day to maintain, lose, or gain weight. The calculator also uses standard scientific formulas to provide accurate results and includes BMR estimation. It is useful for creating personalized diet and fitness plans.

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