Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a quick way to comment/uncomment lines in Vim?

Tags:

comments

vim

I have a Ruby code file open in vi, there are lines commented out with #:

class Search < ActiveRecord::Migration   def self.up     # create_table :searches do |t|     #   t.integer :user_id     #   t.string :name     #   t.string :all_of     #   t.string :any_of     #   t.string :none_of     #   t.string :exact_phrase     #      #   t.timestamps     # end   end    def self.down     # drop_table :searches   end end 

Say I want to uncomment all the lines in the first def ... end section. What's an efficient way to do that in Vim?

In general, I'm looking for an easy and fluid way to comment and uncomment lines. Here I'm dealing with Ruby code, but it could be JavaScript (//) or Haml (-#).

like image 429
Ethan Avatar asked Nov 04 '09 21:11

Ethan


People also ask

How do you uncomment multiple lines in vim?

Use the up and down arrow keys to highlight the lines you wish to uncomment. Once selected, press x to remove the comments. Once you press x, it automatically uncomments the lines.

How do you uncomment multiple lines at once?

Ctrl + K , Ctrl + U . There is also a button for it on the Standard toolbar. Show activity on this post. ctrl + / can be used for adding and removing comments.

What is the shortcut to uncomment multiple lines?

Comment and uncomment blocks of code Press Ctrl+Shift+/ .


2 Answers

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

Put your cursor on the first # character, press CtrlV (or CtrlQ 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:

  1. First, go to the first line you want to comment, press CtrlV. This will put the editor in the VISUAL BLOCK mode.
  2. Then using the arrow key and select until the last line
  3. Now press ShiftI, which will put the editor in INSERT mode and then press #. This will add a hash to the first line.
  4. Then press Esc (give it a second), and it will insert a # character on all other selected lines.

For the stripped-down version of vim shipped with debian/ubuntu by default, type : s/^/# in the third step instead (any remaining highlighting of the first character of each line can be removed with :nohl).

Here are two small screen recordings for visual reference.

Comment: Comment

Uncomment: Uncomment

like image 73
Christian C. Salvadó Avatar answered Sep 28 '22 10:09

Christian C. Salvadó


To comment out blocks in vim:

  • press Esc (to leave editing or other mode)
  • hit ctrl+v (visual block mode)
  • use the / arrow keys to select lines you want (it won't highlight everything - it's OK!)
  • Shift+i (capital I)
  • insert the text you want, e.g. %
  • press EscEsc

To uncomment blocks in vim:

  • press Esc (to leave editing or other mode)
  • hit ctrl+v (visual block mode)
  • use the / arrow keys to select the lines to uncomment.

    If you want to select multiple characters, use one or combine these methods:

    • use the left/right arrow keys to select more text
    • to select chunks of text use shift + / arrow key
    • you can repeatedly push the delete keys below, like a regular delete button

  • press d or x to delete characters, repeatedly if necessary
like image 24
amelia Avatar answered Sep 28 '22 09:09

amelia