In VIM, at the moment when I need to comment out a section of Ruby code:
gc
ir
(inside Ruby block) and ar
(around Ruby block) for Ruby do ... blocks. im
/am
for inside/around Ruby method, and iM
/aM
for inside/around Ruby class. gcir
/ gcar
comment inside/around Ruby do/end block. gcim
/ gcam
comment inside/around Ruby method.gciM
/ gcaM
comment inside/around Ruby class.gcc
to comment a line, or 5gcc
to comment 5 lines.All in all it's very Vim-like and natural.
Hope that helps.
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.
I like using the following:
That says, from the line containing marker "a", up to the current line, substitute a hash for the beginning of the line.
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:
K
to comment the current line.K
repeatedly to comment lots of lines.6K
to comment 6 lines.K
in visual mode comments the whole selection.CTRL-K
#
added to the start.#
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.