How to comment multiple lines at once?

  • How can I select a bunch of text and comment it all out?

    Currently I go to the first line, go to insert mode then type # left-arrowdown-arrow and then I repeat that sequence, perhaps saving a few keystrokes by using the . repeat feature to do each line.

    Is there anyway I could (for instance) select either multiple lines in visual mode or by using a range of lines and an ex ('colon') command and for that range comment out all the lines with a # to make them a "block comment".

    The ability to quickly 'de-comment' (remove the #'s) for a block comment would also be nice.

    sed '/START/i \<<"COMMENT" \ /END/a\COMMENT\n' - you only change two lines. And to see what you commented add cat to the beginning.

  • periket2000

    periket2000 Correct answer

    7 years ago


    You can do it with the following commands:

    for commenting:


    for uncommenting:


    Obviously, here we're commenting lines from 66 to 70 (inclusive).

    Are you sure you don't need insert an 's' after the range?

    Maybe on your vim version but I'm sure of it, "s" stands from "substitution" /^ begining of the line by /# (hash). No mistery here.

    if you want to comment `//` instead of `#` need to escape them with \. `:66,70s/^/\/\/`

    one can refer this link multiple methods are mentioned here by author for comment

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM