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

前端 未结 10 778
悲哀的现实
悲哀的现实 2021-01-30 13:23

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 t
10条回答
  •  长情又很酷
    2021-01-30 14:00

    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.

提交回复
热议问题