Guide to Vim Modes
This guide examines the various Vim modes, including: Command mode, Insert mode, Visual mode, and Replace mode. These modes each have a specific purpose and we walk through examples of how to use each mode to work with files in Vim.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now that you have a good idea on how to navigate inside of Vim, I think we're ready to walk through the various modes that Vim offers. And throughout this entire section, we're gonna be working with all of these modes, but I wanted to dedicate a single guide specifically to the modes, because you're gonna be switching in and out of these as you're working in the editor.

So the first one is one we've already talked about, which is called command mode, so this is also your navigation mode. So right now we're in this file, and if I navigate inside of this, I am inside of command mode, it's called command mode because I can enter commands. So right here, if I wanna save this file, I can hit colon, that pops up the command prompt in the bottom left-hand side followed by a W to save it, and there you go, I have saved it by passing in the write command. So that is the basic command mode.

Now also we have our insert mode. Insert mode is what we use whenever we wanna insert any text, so we can activate that mode a few different ways. We've already talked about 'em, but let's review. To start typing right in a spot where you're at, I can type in the letter I and type in any content I want just like that. If I hit Escape, I go back into command mode.

large

Now if you notice, whenever I switch, on the bottom left-hand side, it switches and shows me my mode right here in the editor. Now we've also talked about two other ways that you can enter insert mode. One is with the capital letter I, which is going to take me in insert mode and put me at the very beginning of the line, just like that.

large

Now also, if I want to start typing at the very end of the line, which in this case is gonna be the very end of this paragraph, then I can type in Shift + A or a capital A, and now I can start typing at the end of the line just like that.

Now there's also one other option, this is one that's not used quite as much. I don't really find a ton of practical time to use this, but if I type in capital R, so Shift + R, now if you notice on the bottom left-hand side, I'm no longer in insert mode, I'm in what's called replace mode. And replace mode is kind of self-explanatory.

large

If I start typing just like this, notice how it's actually overwriting all of the script or all of the text that I have right there.

large

Like I said, this is a mode that's available to you. I personally don't really use it, I don't find it the most intuitive. However, I do know developers that do use it and there are times where that can be handy. So that is the insert/replace mode, they're really very similar the only difference is replace overwrites everything where insert will move everything to the right, so that is that mode.

So we've covered command and also insert and replace. Now let's talk about a new one, and this is called visual mode. Visual is what you would use for copying and pasting and for selecting items and for making changes to multiple items of text, so let me show you how this could work. I want to come down here to a new line, and I'm gonna create a simple method, so I'm gonna say def some_method, and inside of this, I'll put hello world and then end.

def some_method
  puts "Hello world"
end

So let's say that I wanna do something to all three of these lines. Let's say, a good example would be adding a comment. I could add a comment like this, where I type in the letter I, type in a hash, and then come back down to another line, do the same thing, and do it one more time here. As you may have guessed, there is a much better way of doing this.

So I'm going to pull each one of those back and now I'm gonna show you how we could use the visual mode in order to accomplish this same thing all in a single action. Now I just typed in the letter V, and as you can see on the bottom left-hand side, it switched to visual,

large

and what that's gonna do is let me select items. So we're gonna have an entire subsection dedicated to using visual mode, but just a little quick demo on it. If I type in B, it's going to skip ahead to each word, and as you can see, that's letting me select items.

large

I could also hit J for down and select items that way, and K for up. I don't personally wanna do any of that for this example, I just wanted to show you that's how you can select items. Like I said, we're gonna go through an entire little subsection dedicated just to the visual mode. For right now, I just wanna show you some of its capabilities.

Right now, let me switch into what's called visual block mode. So for visual block, I can type in Control + V, bottom left-hand side will show visual block, and now I'm just gonna go down. So I'm gonna type in J twice just like this, and that is going to select only those first characters.

large

And now what I can do is type in, this part might seem a little tricky, and we're going to actually have an entire guide dedicated to visual mode and commenting and uncommenting, but for right now, I'm just gonna show you. I'm going to type in the capital letter I, followed by the pound sign, followed by a space, hit Escape, and then it's going to apply that one change that I made to all of the lines of code, just like that.

large

And I could do that for all of these. If I typed in this, I typed in Control + V and went down for each one of these in the entire system. Now I could do the same thing by hitting Shift + I, then pound, plus, and Escape, and since you're going through a Ruby course and it makes sense, this is a way you would comment items out in Ruby.

large

And then to uncomment them, so first let me just undo that and come back to this line of text right here. To uncomment these, it's pretty easy, as well. I'm going to type in Control + V, select those items again, then hit Shift + X, and then that deleted each one of them.

large

And if I type in the period, it repeats whatever the last action is. I know I just threw a lot of things at you, and I don't want you to think that you have to have every one of those mastered. We are gonna be going through each one of these items. One of the nice things about Vim is when you learn one component, then you're gonna be able to use it in a lot of different situations, so being able to do something like I just did with the period to repeat the last item, that's something we're gonna be doing throughout the course, and you're gonna be using that quite a bit.

But I wanted to just more than anything give you an example of a practical way that you can use visual mode. Visual mode's also great for doing things like deleting large amounts of text, so say that I wanted to come down and delete this entire method and the first two paragraphs. I could do Shift + V and that's going to select that entire line, hit J, come down here, and then I can delete all of that text.

large

I'm gonna walk through, we have another whole mini section dedicated just to being able to delete items, so we'll talk about that quite a bit more then, so let's not worry about it now.

But that is some of the common ways that you can use the visual mode. So in review, the three main modes that you're gonna use in Vim are going to be the command mode, which is the normal one. Every time you hit Escape, you enter back into command mode and this lets you write commands just like I'm doing here with writing. And then also you have insert mode and replace mode, which is where you can enter in any text, anything like that.

Replace mode lets you write over items, so with replace mode, I can write over anything I want just like that. And then we have visual mode, which lets us select text and lets us do things like commenting, uncommenting, copying and pasting, deleting, and different elements like that. So those are the main modes you're gonna be using as you work through Vim.