I was just thinking…

Vim Tip: Comment out multiple lines

June 13, 2007 · 18 Comments

Commenting out a bunch of lines without a vim plugin:

Select your lines with VISUAL BLOCK (CTRL-V), then press I to insert before all highlighted lines. Next type your comment character, # (for python, shell, etc). Last press ESC.

I forget not frequently used, but helpful VIM commands from time to time. Why not blog it?

You can alternatively select your lines with VISUAL LINE (SHIFT-V), then type : s/^/#
This tells the selected lines that you wish to substitute the start of the line with the # char.

Categories: Linux · Linux Programming · Open Source · Programming · Python · Tips

18 responses so far ↓

  • Jison // July 2, 2007 at 1:22 pm | Reply

    Nice tip.
    How do you uncomment them?

  • Mark // July 2, 2007 at 1:29 pm | Reply

    Same thing…but use:
    :s/#/
    Will substitute all lines beginning with the comment ‘#’ to nothing. This has the same effect and is clearer:
    :s/^#/
    The ‘^’ denotes ‘the beginning of the line’ . In the first example it is inferred. Meaning it will not remove extra #’s if they exist elsewhere in the line.
    Thanks for stopping by! :)

  • Robert // July 6, 2007 at 9:35 am | Reply

    Thanks, that was helpful!

  • Priscilla // July 22, 2007 at 1:38 am | Reply

    how do you get your font size to normal

  • Mark // July 22, 2007 at 5:14 pm | Reply

    Priscilla — I use the console based VIM. But I was able to find this: http://www.vim.org/tips/tip.php?tip_id=632

  • Cimo // June 12, 2008 at 7:33 am | Reply

    I googled your tip. CTRL-V and I is great. Thanks.
    You can use CTRL-V and d to uncommet, too.

  • James Hunt // June 27, 2008 at 12:55 pm | Reply

    Using vim mappings can make this process easier, provided your comment characters is always the same (for example, I primarily script in shell, perl and ruby, so my comment character is almost always ‘#’):

    :map <F4> I#<Esc><Esc>

    (You have to type ‘> E s c >’ as 5 characters, but you should press the F4 key to get the <F4>)

    Then, after you select your visual block to comment out, just press F4. Of course, you can substitute any key or key sequence (like Control+G) you like.

    I’m using Control+G to comment a selected block and Control+T to uncomment it:

    :map ^G I#<Esc><Esc>
    :map ^T x<Esc><Esc>

    (As above, the ^G and ^T is generated by holding down Control and typing G and T, respectively)

  • James Hunt // June 27, 2008 at 12:59 pm | Reply

    Oh yeah, and if you want to put the mappings for ^G and ^T in your .vimrc file, you can use the special <C-G> and <C-T> key representations.

    Happy hacking!

  • Philluminati // October 28, 2008 at 11:48 am | Reply

    Excellent tip…exactly what I was googling for.

  • anonymous // January 30, 2009 at 7:47 am | Reply

    Comment and uncomment with a single shortcut:

    map :s/^/\/\/:s/^\/\/\/\//

    (first we set // to the beginning of the line, and if it’s now 4 slashes there – remove them all)

    Anybody knows how to suppress error message?

  • hemantborole // February 5, 2009 at 12:42 pm | Reply

    anonymous: you can use a different character in your :s, like a # or ? so you dont have to escape those ‘\’.
    James Hunt, avoid mapping ctrl-T, because thats the key for tags.

  • anonymous // February 8, 2009 at 6:01 am | Reply

    Ok, here’s a new one).

    map :s@^@//@:s@^////@@e
    vmap :s@^@//@:’s@^////@@e

    Now it comments/uncomments single and multiple lines in normal and visual block selected modes. And it shows no errors.

  • anonymous // February 8, 2009 at 7:19 am | Reply

    It’s been reformatted.

    map :s@^@//@:s@^////@@e
    vmap :s@^@//@:’s@^////@@e

  • anonymous // February 8, 2009 at 7:25 am | Reply

    Ok, here’s a new one).

    http://pastebin.com/m3805915

    Now it comments/uncomments single and multiple lines in normal and visual block selected modes. And it shows no errors.

    PS Don’t know how to post here correctly. How to make it keep all the symbols? Please delete two previous posts.

  • bangitliketmac // February 19, 2009 at 12:40 am | Reply

    Just use the ToggleComment plugin for vim. Get it here: http://www.vim.org/scripts/script.php?script_id=955

    Put ToggleComment.vim in the $HOME/.vim/plugin/ directory.

    Put this in your .vimrc (just one example for Ruby commenting, lots of others are available in the README):
    map ,# :call CommentLineToEnd(’# ‘)+

    That lets you use ,# (that’s a comma followed by a hash symbol) to toggle comments on one or many lines – just use visual mode for multi-line (shift-v, then arrow keys).

  • anonymous // February 27, 2009 at 4:35 pm | Reply

    Thanks a lot! That’s exactly what I was searching but could not find.

  • James // April 8, 2009 at 2:07 pm | Reply

    I wanted to indent a lot of lines in an XML file. I did it by creating a macro and then running it multiple times based on your ^V I tip.

    Here is the commands I used (I prefixed the lines with line numbers so I can explain the steps afterwards):
    1) qa
    2) /^^I^I^I$
    3) mb
    4) /^^I^I^I$
    5) ^V
    6) `b
    7) I^I
    8) q
    9) 100@a

    Here is the explanation of each of the above lines:
    1) Start recording and store in register ‘a’.
    2) Search for the first line (using ^ and $ to ensure pre-indent match).
    3) Set mark ‘b’ (to be able to return here).
    4) Search for the last line.
    5) Enter visual block mode.
    6) Return to mark ‘b’ (backtick to move to column 0).
    7) Add a tab at the beginning of the line.
    8) Stop recording.
    9) Replay the recording stored in register ‘a’ 100 times (repeat as needed).

  • James // April 8, 2009 at 2:11 pm | Reply

    Oops,

    lines 2 and 4 had the XML tags removed after posting.

    2) /^^I^I^I<xsd:complexType>$
    4) /^^I^I^I<\/xsd:complexType>$

Leave a Comment