38

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?

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506

10 Answers10

41

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.

sastanin
  • 40,473
  • 13
  • 103
  • 130
  • Yes, I didn't notice the *uppercase*, which leads me to your answer. :-) +1 – Arup Rakshit May 28 '14 at 06:56
  • 4
    any ideas of why this might not work in some cases? I try and it only insert on the first line I blocked. I have +visualextra installed. – will Aug 31 '14 at 03:44
  • I realize this is very old, but @will I found that the insert did not apply until I left visual block mode by hiting (in other words `CTRL-V` + down arrow to select first column + `I#` + `` ) – GSP Jan 15 '23 at 20:33
14

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
  • 88,666
  • 34
  • 128
  • 138
  • Can you explain what does `\@=` and `\@!` in you search strings mean? – Mikhail V May 04 '15 at 16:37
  • I have tweaked the script slighthly to enter the C, PHP like single line commenting with the same *Shift+K* combination as given above. `noremap K :s,^\(\s*\)[^\/\/ \t]\@=,\1\/\/,e:nohlszvj` – Samundra Jun 30 '16 at 09:39
6

I use these plugins:

  1. vim-commentary by Tim Pope, which defines a comment operator gc
  2. vim-textobj-rubyblock, which defines ir (inside Ruby block) and ar (around Ruby block) for Ruby do ... blocks.
  3. vim-ruby which defines im/am for inside/around Ruby method, and iM/aM for inside/around Ruby class.

Using this combination, I can easily comment/uncomment Ruby-specific code in Normal mode, e.g.:

  1. gcir / gcar comment inside/around Ruby do/end block.
  2. gcim / gcam comment inside/around Ruby method.
  3. gciM / gcaM comment inside/around Ruby class.
  4. Plus normal Commentary maps like gcc to comment a line, or 5gcc to comment 5 lines.

All in all it's very Vim-like and natural.

Hope that helps.

Doug
  • 3,119
  • 3
  • 27
  • 30
6

Some people seem to like Nerd Commenter

projecktzero
  • 1,192
  • 1
  • 9
  • 11
5

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

Swaroop C H
  • 16,902
  • 10
  • 43
  • 50
5

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.

Sagar Jain
  • 7,475
  • 12
  • 47
  • 83
Rob Wells
  • 36,220
  • 13
  • 81
  • 146
  • 1
    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 Jan 23 '09 at 05:12
  • 1
    I agree, this is one of the better answers here. But I personally would have this mapped to a keystroke or something helpful let alone the limitation is that '#' is not used for all scripts to comment. – Dwight Spencer May 12 '13 at 14:31
  • i agree @dreeves and i am trying get away from a line centric approach to a block centric approach. but it's difficult after using vi for over thirty years! (-: – Rob Wells May 13 '13 at 13:43
4

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
  • 2,361
  • 2
  • 22
  • 14
  • Wow, that's nice! But why does that only work with visual block mode, not normal visual mode? – dreeves Jan 23 '09 at 05:12
  • 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 Jan 23 '09 at 09:56
  • This is the correct answers for me - the final ```Esc``` was necessary. – Deebster Apr 29 '15 at 09:02
2

You can also do this:

  • Move to the first line to comment out
  • press 'ESC' then
  • 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 # of comment
  • Hit Ctrl + q to enter Visual Block mode
  • Move done to the last line to comment out
  • Hit d to remove the comment characters
pmg
  • 106,608
  • 13
  • 126
  • 198
rubiii
  • 6,903
  • 2
  • 38
  • 51
1

I will recommend you an alternative way to comment using by Macro

The first, just put this in to your .vimrc

let @c="I#\ej"
let @u="^xj"

For the example

To comment 12 lines:

  1. Navigate to the first row that you want to start your comment.
  2. Type 12@c on command mode to comment 12 lines

To uncomment 12 lines:

  1. Navigate to the first row that you want to uncomment.
  2. Type 12@u on command mode to uncomment 12 lines

The conclusion

Commenting:

[quantifier]@c 

Uncommenting:

[quantifier]@u

note: These commands will start commenting/uncommenting from your current line.

Additional:

To improve you nevigation number.vim can help you a lot about quantifier number.

https://github.com/myusuf3/numbers.vim

fronthem
  • 4,011
  • 8
  • 34
  • 55
  • Awesome tip and I think that it should add space before exit insert mode `let @c="I# \ej"` or something like that, Thanks – 6LYTH3 Jul 04 '14 at 05:00
1

Try T-comment with Ruby block.

I have T-comment mapped to //.

" Easy commenting
nnoremap // :TComment<CR>
vnoremap // :TComment<CR>

This allows, from anywhere in a Ruby block, to comment it out with:

var  (visual around Ruby)
//   (toggle comments)

Note that Ruby blocks has a couple of plugin dependencies that need installing, see my Vimfiles for an example.

Kris
  • 19,188
  • 9
  • 91
  • 111