Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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?

like image 423
Sam Saffron Avatar asked Jan 20 '09 08:01

Sam Saffron


People also ask

How do I comment out large sections of code in Vim?

Using the up and down arrow key, highlight the lines you wish to comment out. Once you have the lines selected, press the SHIFT + I keys to enter insert mode. Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.

How do you comment out a block of code in Ruby?

Single line comments in a Ruby script are defined with the '#' character. For example, to add a single line comment to a simple script: # This is a comment line - it explains that the next line of code displays a welcome message print "Welcome to Ruby!"

How do I comment a block in Vim?

To comment out blocks in vim:use the ↑ / ↓ arrow keys to select lines you want (it won't highlight everything - it's OK!) Shift + i (capital I) insert the text you want, e.g. % press Esc Esc.

How do you comment text in Vim?

How to Comment a Single Line in Vim. To comment out a single line in Vim, enter Visual Mode by pressing Ctrl + V . Next, navigate to the line you wish to comment out and press the C key. Depending on your Vim configuration, this should comment out the selected line.


1 Answers

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.

like image 163
sastanin Avatar answered Sep 29 '22 10:09

sastanin