What's the most elegant way of commenting / uncommenting blocks of ruby code in Vim?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 08:24:25

问题


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?


回答1:


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.




回答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 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.




回答3:


Some people seem to like Nerd Commenter




回答4:


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.




回答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 + 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



回答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:

  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



来源:https://stackoverflow.com/questions/460496/whats-the-most-elegant-way-of-commenting-uncommenting-blocks-of-ruby-code-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!