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

前端 未结 10 680
悲哀的现实
悲哀的现实 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 13:52

    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.

    0 讨论(0)
  • 2021-01-30 13:52

    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.

    0 讨论(0)
  • 2021-01-30 13:55

    Some people seem to like Nerd Commenter

    0 讨论(0)
  • 2021-01-30 13:56

    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

    0 讨论(0)
  • 2021-01-30 13:57

    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
    0 讨论(0)
  • 2021-01-30 13:58

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

    0 讨论(0)
提交回复
热议问题