Basic Select Commands in Vim
In this guide you'll learn the various ways that you can select items inside of Vim, specifically walking through the options provided by Vim's Visual mode.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide, we're gonna start taking a deeper look at how we can select items. Now, technically we've already kind of gone through each one of these in some form or another but we haven't actually dedicated a specific guide just to selecting items, and that's what I wanna do now.

So I'm gonna open up the example file, let me make sure I'm in the right directory. Oh no, I'm in that Rails directory we created in the last guide. So I'm gonna now open up in Vim the example text file, and let's review some of the selection items that are available. All of these are in the cheat sheet, they're under the subsection of selecting.

I'm gonna go over and I'm actually gonna type em out right now. So to select just a single word, you're gonna type in the letter V, so this is going to be a single word. The way we could do this is I have the, I press down on V, it switches down into visual mode on the bottom left-hand side, and then I can use the L or the right arrow to move to the right, and then I can move H to move to the left, and then I also could use J or K if I wanted to go down and select items one at a time, and that's how you can use V just by itself.

Now, if you wanna select an entire line, so say I wanted to select everything on this line, even if I was in the middle of it, I could do Shift+V, or capital V will select the entire line, then I could so something like press the D character, and I have deleted it. So that is a capital V that selects the entire line.

Now let's talk about some other options we can do. So we've selected a single word and we've also selected an entire line. But we also have the ability, and this is a pretty cool one, to select just the first characters of multiple lines. So let's see exactly how this would work in a real kind of scenario.

I'm gonna create a simple method here. We're just gonna call it Method Simple. And it's just going to put, hey there, and that's it. Now this is one that comes in very handy whenever you're wanting to do something like update comments or add comments into multiple lines of text.

So the way you can do this is by typing Control+V. So Control+V is going to put you into what's called Visual Block mode. And you can see there, on the bottom left-hand this side of the screen, we have Visual Block mode. And now if I type J, this is going to select only the very beginning of those lines.

And so now I could do something like shift into the insert mode. So Shift+I, followed by a #, followed by the +, followed by Escape and then it has updated all of those.

large

And like I said earlier, I actually am gonna dedicate an entire guide just to how you can add comments and how you can delete them, just because it is kind of a non-intuitive process, if you've never done it before in Vim. But that's what I use Visual Block for, pretty much daily. And so what that is, is Control + V. And that is going to put you in Visual Block mode, and that's just a really nice and easy way of selecting just the beginning of lines.

Don't worry, I know I went through the process and the keystrokes on how you can do comments quickly. One, I have the entire set of the sequence of keystrokes in my cheat sheet, but additionally I'm gonna dedicate a whole guide to it, so if you felt like I went through it quickly, don't worry, it's because I'm wanting to cover a lot of the select items in this guide, and we'll take other guides that will specifically target some of those other items.

Now that we have covered a single word, an entire line and also the beginning of lines with Visual Block mode, let's talk about how we can select all. That seems like a very common thing you'd want to do. Let's say that you got into, and let me close out of this and let's go into our Rails application, that Vim demo application. And let's see, oh yeah, I guess I actually have to change into the directory, that helps. Okay, there we go.

Let's say that I wanna update the Seeds file in the Rails application. As you can see right here, it starts off with a bunch of comments. Now, yes, technically I could just go Control+V, or Shift+V, come here and delete all of those. However, whenever you have a file and you want to delete everything, there are a couple ways you can do it. One, you could cheat, you could make it real easy and just type in D followed by a capital G, so that is D + G. That would do the exact same thing for you, but I also want to show you how you can select all for the times where you wanna do that.

So if you want to select everything, you type g + g + V + G. Let me, and then you could delete it or do anything you wanted. So let me actually type this and this is in the cheat sheet, but this is going to be g, g, V, G, exactly like this, so type it in exactly like that and that will select all. And that is really all you have to do whenever you want to select those items.

So let me switch back, and switch back to the example, just in case you're taking notes, you can follow along here. So g, g, V, G, this is going to select all. And then you can perform any kind of task, such as deleting, copying it, which would also be yanking it, anything like that. And those are, when it comes to selecting items, these are the four common ways that I use these. And I use these pretty much on a daily basis, in some form or another.

ggVG, not quite as much, mainly because usually whenever I'm wanting to delete everything, I will use that one shortcut I showed you, which is lower case D, capital G, and that accomplishes the same thing, in terms of either deleting everything or yanking everything. So that's kind of the common one that I'll use, but say that I wanted to only copy and I didn't want to cut, then this would be probably the best way of doing it, is selecting everything, yanking it, and then going and putting it in a different file. So those are the four most common ways that you can use the select process in Vim.

Resources

Code
v - a single word
V - selects entire line
control + v - visual block mode
ggVG - select's all