问题
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 orr#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?
回答1:
I do almost the same thing as you.
commenting:
- visual block select with
CTRL-VthenI#(insert#in the begining)
uncommenting:
- visual block select with
CTRL-VthenX(delete the first symbol on the line)
Please note uppercase I and X.
回答2:
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
Kto comment the current line. - Hit
Krepeatedly to comment lots of lines. 6Kto comment 6 lines.Kin 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.
回答3:
Some people seem to like Nerd Commenter
回答4:
I use these plugins:
- vim-commentary by Tim Pope, which defines a comment operator
gc - vim-textobj-rubyblock, which defines
ir(inside Ruby block) andar(around Ruby block) for Ruby do ... blocks. - vim-ruby which defines
im/amfor inside/around Ruby method, andiM/aMfor inside/around Ruby class.
Using this combination, I can easily comment/uncomment Ruby-specific code in Normal mode, e.g.:
gcir/gcarcomment inside/around Ruby do/end block.gcim/gcamcomment inside/around Ruby method.gciM/gcaMcomment inside/around Ruby class.- Plus normal Commentary maps like
gccto comment a line, or5gccto comment 5 lines.
All in all it's very Vim-like and natural.
Hope that helps.
回答5:
Have you tried out EnhCommentify.vim or tComment.vim?
回答6:
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.
回答7:
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.
回答8:
You can also do this:
- Move to the first line to comment out
- press 'ESC' then
- Hit
Ctrl + qto 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 + qto enter Visual Block mode - Move done to the last line to comment out
- Hit
dto remove the comment characters
回答9:
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.
回答10:
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:
- Navigate to the first row that you want to start your comment.
- Type
12@con command mode to comment 12 lines
To uncomment 12 lines:
- Navigate to the first row that you want to uncomment.
- Type
12@uon 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
来源:https://stackoverflow.com/questions/460496/whats-the-most-elegant-way-of-commenting-uncommenting-blocks-of-ruby-code-in