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 (-#
).
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.
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.
Comment and uncomment blocks of code Press Ctrl+Shift+/ .
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:
VISUAL BLOCK
mode.INSERT
mode and then press #. This will add a hash to the first line. #
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:
Uncomment:
%
If you want to select multiple characters, use one or combine these methods:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With