Commenting and Uncommenting Ruby Code in Vim
In this guide you'll learn how to command and uncomment Ruby code from within a vim file, including a comparison between Vim and Sublime Text.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Since this section is dedicated specifically to working with Ruby development in Vim, I thought it would be smart to actually work with a Ruby file instead of just a regular text file. We've already gone through how you can comment and uncomment in Vim, but I promised you that I'd dedicate an entire guide to it, and so I want to actually live up to that promise.

So, here we have a regular very plain vanilla method called my_method, and then we're calling it right here. Let's imagine that we want to actually comment all of this out for some reason or another. I can do this very easily by running control + V, which is gonna take me into visual block mode coming down to wherever I want to end the comment string.

So what this is gonna do is it's gonna select the beginning of each one of these lines. From there I hit shift + I, which is gonna take me into insert mode, then I type in the # character followed by a space, and then I hit escape.

And then what this does, now this may seem a little bit weird, if you've never done it before, because what visual block does is it selects each of these items, and then when we run insert it may seem like we were only running insert on this first line. But because we used visual block followed by the capital I, it actually allowed us to make any change that we made to this first line, would kinda have a domino effect, and come to each one of these other lines.

So what exactly were those commands? I have 'em in the cheat sheet just in case you're wondering. But the way that I did it was, I typed in control + V, then I selected the item, so then I'll put it in kind of a one, two, three kind of list, so then I selected items or selected lines, and then from there I hit the shift + I, which is really capital I, and then I typed in # + space, and then I hit the escape character, and that is it.

control + v
selected lines
shift + I
# + space
esc

So that is, and this may seem like little bit of kind of a weird way of doing it, but this is a pretty common pattern when it comes to commenting in Vim. Now, there are plug-ins that allow for commenting, I personally have used them before, and I have just followed this process so many times that I've just never ended up really using those other plug-ins.

This may seem like a lot of steps, but actually the more you do it, it becomes kind of almost second nature. And I know that if you were using something like Sublime, which I'll open up Sublime right here. So say that you had Sublime, and you had some kind of Ruby file, and you have the same kind of method. So sample method, with something right here, and it may seem like it would be easier just to come down here, select it all, and then run command followed by a / and that accomplishes the same thing, and if you hit it again that toggles it.

So, that may seem intuitive, and it is. I mean, that's a great way of doing it, and whenever I use Sublime that's exactly the way I do it. And so following these steps may seem like it's a little bit slower, and technically it is a little bit slower, but, if you do it enough times, all the sudden this is gonna seem like second nature, and you're not even really gonna think twice about it. So, that is how you can add comments to a set of lines in Vim.

Now, if you want to remove these the way you would do that is by typing the same command. So you're gonna start off with control + V, select the items there, and then you're gonna give it a shift + X, so a capital X, and that is going to remove all the items, and then press the period key, and remember the period key is something that repeats what ever the last action was. So in order to uncomment items, you're gonna type in the same thing, you're gonna follow the same process of control + V, then you're going to select the items, then you're going to press shift + X, so a capital X, and then that is going to remove all of the first items you selected, and then you can type in a period.

And the cool thing about this is let's say that for some reason you have some method, like this, that actually had a few lines of indentation then you could do the same process with this period right here, and you could bring 'em back the way that we did just like this. So these are the two ways, and I'll run through 'em one more time, just because I remember when I was first learning how to do this, this was something that I found a little bit kind of annoying but also I didn't like all the different commands I had to type in, so the more you practice it, the easier it'll get.

So remember this is just control + V, select items, shift + I, # sign, space, escape, and then it's gonna apply each one of those. And then to remove each one of them, control + V, select it all the way down, capital X, followed by period, and you are back to where you started, so that is how you can comment and uncomment items in Ruby with Vim.