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.

Asked By: Michael Durrant


Visual Block Mode

  • First, move the cursor to the first char of the first line in block code you want to comment, then type Ctrl + v.

    Then vim will go into VISUAL BLOCK mode.

  • Use j to move the cursor down until you reach the last line of your code block. Then type: Shift + i

  • Now vim goes to INSERT mode and the cursor is at the first char of the first line. Finally, type # then ESC and the code block is now commented.

Note that at first sight, the editing does not seem to differ from changing a single line. The changes will only be applied to the whole block after hitting ESC.

To uncomment, do the same things but instead of type Shift + i, you just type x to remove all # after highlight them in VISUAL BLOCK mode.

Answered By: cuonglm


You can do it with the following commands:

for commenting:


for uncommenting:


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

Answered By: periket2000


The following steps are done in command mode:

  1. go to first line and set the marker with mt
  2. go to the line till which you want range
  3. issue the command :'t,.s/^/#/


  • m#, where # denotes any letter, puts a marker at your current location. The location can later be referred to using '#, where # is the same letter as before. In our case we used the letter t
  • The command :'t,.s/^/#/ reads as follows:
    • : the prefix for complex commands
    • 't,. defines the range at which the following command should apply. In our case, it is from marker 't till current line .
    • s/^/#/ is a substitution formula, replacing all beginning-of-line ^ with literal character #
Answered By: Chandra kant


There is a plugin, which offers an efficient way of commenting out based on the file system being used.

Also, this answer discusses how to use the NERD commenter for vim. Few more basic instructions on how to use the plugin are discussed here.

Answered By: Ramesh


For the sake of completeness here’s another way:

  1. Enter visual mode by pressing v
  2. select the lines you like to comment (up/down arrow or j/k)
  3. enter :s/^/# / which translates to: in selection (:) replace beginning of line (^) with `# ‘
Answered By: reto

Use either NERDCommenter as mentioned, but if that’s not available to me I use the visual modes. I go into Visual Line mode, make my selection, often its blocks of whitespace, so I go V{ then change to Visual Block mode with Ctrl+Q and then insert the comment symbol at the start of the lines with I, so to comment out a block of text, I’d do

Answered By: TankorSmash
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.