views:

923

answers:

7

In VIM, at the moment when I need to comment out a section of Ruby code:

  • I navigate to the first column in the row I want to comment out
  • I press CTRL-v to enter visual block mode
  • I navigate down till the place I want to end the operation
  • I type r<space> if I want to uncomment the code or r# if I want to comment it out.

This workflow seems ok to me, are there any ways of improving this process? Are there any other tricks for commenting or uncommenting ruby code sections in vim?

+4  A: 

Have you tried out EnhCommentify.vim or tComment.vim?

Swaroop C H
+1, these scripts works with almost all filetypes, just hit <m-x> and it will toggle the comments on the (selected) lines.
Luc Hermitte
looks good, will try it out
Sam Saffron
+1  A: 

I like using the following:

  • put cursor on the first line you want to comment out
  • enter ma (no colon) to put a marker on that line
  • go to the last line of the block you want to comment out
  • enter :'a,.s/^/#/ and then enter

That says, from the line containing marker "a", up to the current line, substitute a hash for the beginning of the line.

HTH

cheers,

Rob

Rob Wells
It's good to know how to do it that way with markers for creating macros but for manually selecting a region like that visual mode seems so much easier to me.
dreeves
+3  A: 

Some people seem to like Nerd Commenter

projecktzero
+2  A: 

After visually selecting, in block mode, the text you want to comment out, hit I (that is a capital i), type # and finally hit the escape key. It's basically the same procedure you are using currently, but using insert instead of replace.

davitenio
Wow, that's nice! But why does that only work with visual block mode, not normal visual mode?
dreeves
If you take a look at vim's visual operators (http://www.vim.org/htmldoc/visual.html#visual-operators) you will see that there is no visual-characterwise insert operator nor a visual-linewise insert operator, but only a visual-block insert operator.
davitenio
+3  A: 

For each language (ftplugin), I write mappings which will add or remove the comment leader and move the cursor down one line. For example, in my python ftplugin, I have this:

noremap   <buffer> K      :s,^\(\s*\)[^# \t]\@=,\1#,e<CR>:nohls<CR>zvj
noremap   <buffer> <C-K>  :s,^\(\s*\)#\s\@!,\1,e<CR>:nohls<CR>zvj

I find this to be an extremely flexible setup:

  • Hit K to comment the current line.
  • Hit K repeatedly to comment lots of lines.
  • 6K to comment 6 lines.
  • K in visual mode comments the whole selection.
  • Everything can be uncommented in the same way using CTRL-K
  • If lines are already commented, they won't have an additional # added to the start.
  • If a # is followed by a space, it is considered a text comment and doesn't get touched.

I adapt this slightly for each language. It doesn't work as well for Old C comments (/*...*/) but I prefer not to use those anyway.

too much php
+2  A: 

I do almost the same thing as you.

commenting:

  • visual block select with CTRL-V then I# (insert # in the begining)

uncommenting:

  • visual block select with CTRL-V then X (delete the first symbol on the line)

Please note uppercase I and X.

jetxee
A: 

You can also do this:

  • Move to the first line to comment out
  • Hit Ctrl + q to enter Visual Block mode
  • Move done to the last line to comment out
  • Hit I, comment out by pressing #
  • Hit ESC

And to uncomment:

  • Move to the first #
  • Hit Ctrl + q to enter Visual Block mode
  • Move done to the last line to comment out
  • Hit d to remove the comment characters
rubiii