Check out a free preview of the full Complete Intro to Linux and the Command-Line course:
The "VIM: Basic Commands" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian reviews basic VIM commands, including starting VIM, editing a file, and saving a file. Also covered is how to quit VIM, which is the only command worth remembering, according to Brian.

Get Unlimited Access Now

Transcript from the "VIM: Basic Commands" Lesson

[00:00:00]
>> So let's pop into vim and learn how to quit cuz I feel like this is the most important thing that I might type teach you all day today. So, let's clear here. So again, you can type vi or vim, I'm gonna type vim just because I'm sure someone's gonna yell at me that vi and vim are not the same thing.

[00:00:20] And I'm gonna say vim txt file which is the file that I was just editing with nano, right? As you can see, this is the same thing here, just in vim instead of in nano. Now the hard thing about vim is it doesn't really give you glow or like visual cues of what you can do and where you are.

[00:00:44] And that can be really tough, whereas nano's pretty good about showing you the shortcuts at the bottom. So the first thing I want to teach you is how to quit vim. So the first thing is that vim has multiple different modes. So you can be in interactive mode, you can be in command mode, and umpteen million more different kinds of modes we saw in x mode as well.

[00:01:07] So the first thing if you hit escape, and you hit it a couple times, you'll probably get back to command mode, which is where you wanna be. Right now we're already in command mode so we don't need to hit escape. And if you hit colon q, that's how you quit vim.

[00:01:25] So that's just important to know upfront. I feel like if you're gonna go in a door, you wanna know how to get back out the door and if you don't know, it's gonna be like a little bit panic inducing. So, you can go into vim and know that you can always get to the exit.

[00:01:38] I remember literally when I was working in my first job, and I did not know how to use vim, whenever it popped up for a get merge message or something like that, I would just close the terminal because I didn't know how to quit vim. [LAUGH] So, that's the first thing, colon q.

[00:01:56] That'll get you out of there. Sometimes it'll say you have unsaved changes and not let you quit. If you hit colon q, exclamation point, that'll let you quit no matter what. That's like, I don't care about save changes, just abandon ship, okay? So, that's the first thing, know how to quit vim.

[00:02:18] I feel like we're all better people already. So let's go back into it. Let's actually learn how to use vim a little bit because for the most part, if I have the choice between nano and vim, I know enough about vim that I feel okay to write a bit of vim.

[00:02:31] So right now I'm in command mode, command mode allows you to run various different commands. So one that I know all the time, if I hit colon d, that's actually just going to delete the line that I'm on. So there's a bunch of these kind of different commands.

[00:02:46] I'm not gonna show you very many of them because I don't know very many of them. But for example, if I wanted to delete all the lines up here, I could go to the top line and say d100, which would delete the next 100 lines, and it would delete everything in the file.

[00:03:00] So that's one of the commands. Again, there's a great many more, and I'm sure someone's says, well if you just hit colon q 15 times 7, that would have deleted all those particular lines and you wouldn't have had to move your cursor so much. I know, if you wanna learn vim, don't learn it from me.

[00:03:21] But okay, so now I wanna get into interactive mode or I want the ability to type now. So if I hit I, or insert mode, rather, not interactive, this will drop me into typing mode. Where I can say, this is brian, we're typing in vim now, this is so much fun.

[00:03:44] So that's insert mode and now I'm done typing, if I hit escape, this will drop me back into command mode. Now I can hit colon w and that will save it. So that's what colon w does. And then I can hit colon q and I'm done and I'm out.

[00:04:04] So get back in here one more time. Frequently what I'll do is I'll come in here, I'll write some more stuff. And if you wanna write and quit all at the same time, which is usually what I do, you can do colon wq. All done. And then that both wrote and quit all at the same time.

[00:04:33] So that's really about all I'm gonna show you about vim. I don't think we really need to get much more into it than that. There's plenty of good course material out there on vim if you wanna become like a vim expert. But for now, this should be enough of a tutorial that you're not afraid of vim anymore.

[00:04:55] The last thing I will show you, one, there's a website called- I think it's called vim adventures. Yeah, so if you go to vim adventures, you can just google that. And there's also a link in the course notes. That's a really fun way of learning vim. And then if you also do help tutor like this, this will drop you into a vim tutor that will actually walk you through of how to learn vim.

[00:05:22] So despite the fact that vim people love to tell you how much they love vim. They're also a very nice community and they want people to learn vim so there's a lot of good resources out there as well. Last thing I'm gonna show you. If you're still having trouble quitting vim after all this, because there's still some weird places you can get in.

[00:05:43] If you put colon qa exclamation point, this will always get you out 100% of the time. I don't even remember what the A does. I think it just says like, I don't care, just blow everything up. Just get me the hell out of here. It's like the nuclear bomb was like, I want out.

[00:06:02] So, that's a good one to keep in mind as well. The last one that I feel like I need to talk about is Emacs. You can't mention them without someone peeking in around the corner, is like, hey, what about Emacs? [LAUGH] The reason why I'm not showing you Emacs is, there's no reason that you'll ever be thrust into Emacs that you didn't mean to get into Emacs as far as I know.

[00:06:28] And it's not normally installed by default in most Linux distros. So, it's out there. It's another text editor like I know my coworker Nina Zakharchenko uses Emacs and swears by, it seems like it's a good text editor. I've just never been thrust into it and I showed you nano and vim because you will be thrust into those.

[00:06:46] And you do have to know how to use those at least a little bit. So, that's why we're not talking about Emacs today is because you don't have to learn Emacs. You can learn it if you want to. And so suffice to say, I don't know anything about Emacs cuz I've never had to learn anything about it.