tags:

views:

1011

answers:

10

I have this Ruby code, where some lines are commented out...

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 (-#).

+1  A: 

If you already know the line numbers, then n,ms/# // would work.

uckelman
+1  A: 

Here is a section of my .vimrc:

"insert and remove comments in visual and normal mode
vmap ,ic :s/^/#/g<CR>:let @/ = ""<CR>
map  ,ic :s/^/#/g<CR>:let @/ = ""<CR>
vmap ,rc :s/^#//g<CR>:let @/ = ""<CR>
map  ,rc :s/^#//g<CR>:let @/ = ""<CR>

In normal and in visual mode, this lets me press ,ic to insert comments and,rc to remove comments.

innaM
+6  A: 

Use Control-V to select rectangles of text: go to the first # character, type Ctrl+V, move right once, and then down, up to the end of the comments. Now type x: you're deleting all the # characters followed by one space.

Arthur Reutenauer
Damnit, same answer as mine, but way more up votes. Lucky :)
Marcin
Ha ha ha ha :-)
Arthur Reutenauer
+4  A: 

Here is how I do it - very quick and painless:

  1. Go to first character on the first line you want to comment out.

  2. Hit Ctrl+q in GVIM or Ctrl+v in VIM, then go down to select first character on the lines to comment out.

  3. Then press c, and add the comment character.

Uncommenting works the same way, just type a space instead of the comment character.

Marcin
+4  A: 

I have the following in my .vimrc:

" Commenting blocks of code.
autocmd FileType c,cpp,java,scala let b:comment_leader = '// '
autocmd FileType sh,ruby,python   let b:comment_leader = '# '
autocmd FileType conf,fstab       let b:comment_leader = '# '
autocmd FileType tex              let b:comment_leader = '% '
autocmd FileType mail             let b:comment_leader = '> '
autocmd FileType vim              let b:comment_leader = '" '
noremap <silent> ,cc :<C-B>silent <C-E>s/^/<C-R>=escape(b:comment_leader,'\/')<CR>/<CR>:nohlsearch<CR>
noremap <silent> ,cu :<C-B>silent <C-E>s/^\V<C-R>=escape(b:comment_leader,'\/')<CR>//e<CR>:nohlsearch<CR>

Works both in normal and visual mode.

(I stole it from some website many years ago so I can't completely explain how it works anymore :).)

jqno
+3  A: 

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

Put your cursor on the first # character, press Ctrl-q, 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, go to the first line you want to comment, press Ctrlq, then select until the last line, and press I#Esc, that will insert a # character on all selected lines.

CMS
By default it's CTRL+V. The windows version of gvim uses Ctrl+Q because Ctrl+V is already used for paste.
Martinho Fernandes
+1  A: 

I like to use the tcomment plugin: http://www.vim.org/scripts/script.php?script%5Fid=1173

I have mapped gc and gcc to comment a line or a highlighted block of code. It detects the file type and works really well.

Rick
+8  A: 

I use the NERD Commenter script. It lets you easily comment, uncomment or toggle comments in your code.

Manuel Ceron
+4  A: 

I use EnhancedCommentify. It comments everything I needed (programming languages, scripts, config files). I use it with visual-mode bindings. Simply select text you want to comment and press co/cc/cd.

vmap co :call EnhancedCommentify('','guess')<CR>
vmap cc :call EnhancedCommentify('','comment')<CR>
vmap cd :call EnhancedCommentify('','decomment')<CR>
qba
+1  A: 

I mark the first and last lines (ma and mb), and then do :'a,'bs/^# //

SDGator