Check out a free preview of the full Introduction to Bash, VIM & Regex course

The "Using Vim" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

James shows how to use Vim to open, edit, and quit files.


Transcript from the "Using Vim" Lesson

>> James Halliday: Let's learn Vim. So if you don't already have Vim installed, the problem we have at least vi installed. If you don't have Vim installed, go ahead and install it. Also, on some systems, you'll have to install things like syntax highlighting and all of the fancy features. With a package, on a Linux system called vim-common, so it's good to have that.

If you're on a Mac, I think that you're probably already good to go. So why doesn't everybody type vim? Should see something like this.
>> James Halliday: I have version 7.4 on debian. So if you try to type, this is what happens. It might take a bit for it to start logging.

This is because when you go into vi, you actually start out in this thing called command mode. So to go into insert mode, which is a more familiar kind of editing, you can type i. So then at the bottom, it shows INSERT with dashes around it. And now you can type basically like normal.

You can even use the arrow keys and things like that. But I'll show you a better way of doing that in a moment. So you can say, hello there I am in Insert mode. So if we wanna save this file, we're gonna have to give it a file name, because we just started them without a file name.

So to do that, we have to go back into command mode, which you can do by hitting Escape. Or you can actually do it with Ctrl+C as well, which is useful if you don't wanna reach all the way up to Escape. Although, in my system, I have CapsLock mapped to Escape, because I use Vim all the time.

So it's very easy for me to hit Escape. Anyways, once you're in command mode, you do colon and then you can do w space the name of the file that you wanna save as.
>> James Halliday: So I'll save it as hello., or I'll save it as wow.txt. There we go.

And you should see a little message at the bottom similar to that. If you wanna quit now that we've saved our file, you can do, in command mode which you can always get to by hitting Escape. You can do :q and that exits. If you want to now open that file again, you can do vi or vim, the name of the file and you can make a change.

>> James Halliday: If you wanna do both saving and quitting in one operation you can kind of combine those commands by doing :wq,
>> James Halliday: Which saves and quits. If you want to do things like, maybe you made some changes. Maybe your cat walked on your keyboard and put a bunch of characters in your program.

I have a cat now that does that a lot. It loves to stand on my keyboard. If you just wanna quit without saving the changes, if you try to do :q, vim will tell you you haven't written your changes out. So you have to tell vim explicitly, :q!, which is, I just wanna quit.

I don't care about the changes, throw them away, which it does. Now, if I open it agin see that those changes are done. So to recap, insert mode you can go into by hitting i. There are different ways that I'll go into that you can go into insert mode with variations.

But i to go into insert mode, Escape to go from insert mode back into command mode. In command mode, :q quits, :w writes. Also, if you make a change, you can do :w. If you've already specified a file name, then you don't have to specify the file name again.

So saves to the same file. And :wq writes and quits, :q quits. :q!, quits discarding changes. Also if you've typed vim already and you wanna load a file that already exists, you can do :o and the name of that file.
>> James Halliday: There we are.
>> James Halliday: Yep.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now