Introduction to Vi/Vim editor

Video loading...

  • 0:02
    Editing with Vi/vim Editor with Addison Berry
  • 0:09
    In this Command Line video, we're going to introduce
  • 0:11
    you to the Vi Editor which is, there's also a
  • 0:15
    version called Vim, and we'll talk about those differences.
  • 0:17
    We'll introduce just sort of the basic concepts
  • 0:20
    of using Vi and how to move around within a file.
  • 0:23
    In future videos, we'll get into more editing.
  • 0:27
    OK, so let's just start out real quickly
  • 0:28
    talking about Vi and Vim.
  • 0:30
    The command itself, when you want to work, edit a file,
  • 0:33
    say, is just type vi and then the file name.
  • 0:35
    And that's a visual editor for the command line.
  • 0:39
    It's pretty old school.
  • 0:40
    There's also Vim, and this is the more common editor
  • 0:45
    that people use today, between the 2, and basically,
  • 0:50
    it has some niceties that make it easier to work with.
  • 0:52
    The nice thing is that if you know Vi commands,
  • 0:55
    those will also work in Vim, but the reverse
  • 0:57
    is not always true.
  • 0:59
    Now, if I look and see where my VI command is coming from,
  • 1:03
    by using which here. And then I list, I'm going to list
  • 1:05
    all of my, all of my commands in usr/bin,
  • 1:08
    that begin with vi, so that we can sort of see
  • 1:10
    all of the stuff I have here.
  • 1:11
    You'll notice that Vi is actually assembling to Vim.
  • 1:16
    So when I type the command Vi, it's actually running Vim
  • 1:20
    in the background for me.
  • 1:21
    This is a very common setup and is what you'll
  • 1:23
    find on most systems, but sometimes you end up on
  • 1:28
    a system that only has Vi, and doesn't have Vim,
  • 1:30
    and that's when knowing the Vi shortcuts is important.
  • 1:35
    I also wanted to show you real quickly this Vim Tutor.
  • 1:37
    You just type in the command vim tutor and it brings you,
  • 1:42
    it opens up Vi, and gives you instructions and walks
  • 1:46
    you through a lot of things.
  • 1:47
    It's really handy, it doesn't take that long,
  • 1:50
    it does take time but you can always come back to it,
  • 1:52
    and kind of pick up or review things.
  • 1:54
    So, it's a really handy thing that you have locally.
  • 1:57
    I'm going to show you how to get out of here,
  • 1:59
    which is by doing a : q, which appears at
  • 2:02
    the bottom of the line here.
  • 2:03
    And we'll talk more about this when we go edit our file,
  • 2:07
    but : q is how you quit out of a file.
  • 2:11
    OK, so now let's actually go get something to play with.
  • 2:15
    We'll get our own file and start playing around.
  • 2:17
    I just copied a recent article that Dave did on,
  • 2:21
    put it in a text file, so we have something to work with.
  • 2:24
    So I'm going to go in there, and then I'm going to type,
  • 2:26
    vi and then the file name.
  • 2:28
    And that will open that file in the Vi editor.
  • 2:33
    So, vi, context-panels, boom!
  • 2:36
    And that's how I get into my file with the editor.
  • 2:41
    And you'll notice at the bottom when I open the file up,
  • 2:45
    it's giving me some information in this bottom line.
  • 2:47
    So, that's the name of the file, the number of lines,
  • 2:50
    the number of characters.
  • 2:54
    This stuff isn't necessarily going to stay here.
  • 2:56
    Then, over on the right-hand side,
  • 2:59
    you'll see these 2 numbers with a comma;
  • 3:01
    that is giving the line and position.
  • 3:03
    So, I'm in the first line at the first character right now.
  • 3:06
    And then all the way over, it's telling me that I'm at
  • 3:09
    the top of the file, and you'll see these
  • 3:12
    2 things will change.
  • 3:13
    And top will show the percentage of where
  • 3:15
    I am relatively in the file.
  • 3:18
    These 2 are going to stay over here,
  • 3:20
    and you'll see them change as we move around and do things.
  • 3:22
    And you'll just generally notice in Vi, this bottom line,
  • 3:25
    is sort of a console.
  • 3:27
    It's your, it can be a command line, as well as,
  • 3:30
    an information line, here, and that's sort
  • 3:32
    of always there for you.
  • 3:34
    Now, when moving around, sort of instinctually,
  • 3:38
    you'll want to hit the enter key and that lets me move down.
  • 3:42
    I can also use arrow keys on my keyboard here, to go up,
  • 3:47
    to go down and go sideways.
  • 3:49
    You really need to understand that this is a Vim feature,
  • 3:52
    and that you can't do this in Vi, if you have plain Vi.
  • 3:57
    The way you need to do it, if you're in Vi,
  • 3:59
    is you need to use letter keys.
  • 4:02
    These 3 letter keys on your keyboard,
  • 4:03
    H will make the cursor go left, J will make it go down,
  • 4:08
    K makes the cursor go up, and L will make the cursor go right.
  • 4:14
    And it's just a matter of taking the time to get
  • 4:17
    familiar with these keys.
  • 4:20
    It's really good to know because if you end up in
  • 4:22
    a Vi-only environment, this is how you move around.
  • 4:25
    You can't use your little arrow keys that
  • 4:28
    so many people get used to.
  • 4:29
    So, if I come back to my document here, now,
  • 4:33
    I'm just going to use the letter keys.
  • 4:35
    So, I'll use h to go left, j down,
  • 4:40
    k up. I'll move where I can go.
  • 4:43
    Right here.
  • 4:44
    Then, l will make me go right.
  • 4:47
    It's very important to know if you end up in a
  • 4:49
    Vi-only environment, because your arrow keys
  • 4:52
    are not going to work.
  • 4:54
    Another thing you can do with these,
  • 4:56
    is you can type a number before either of these.
  • 4:59
    So, if I want to go down 3 lines, I can just type,
  • 5:01
    3j and I go down.
  • 5:03
    Same thing with k. I could do 4k and I'd go up 4 lines.
  • 5:07
    So, that's also just a handy thing and you'll see
  • 5:10
    that pattern repeat itself in other commands.
  • 5:14
    Everything that we've been doing, I've been typing letters,
  • 5:16
    but I'm not actually editing any text.
  • 5:18
    I'm not typing text in, I've just been doing things.
  • 5:21
    I'm in command mode.
  • 5:23
    Vi is a modal editor.
  • 5:25
    If I hit i, you'll see this insert comes at the bottom.
  • 5:29
    Now, I can insert text.
  • 5:32
    So, previously I was in command mode, now, I'm in insert mode.
  • 5:37
    And now, when I type my hjkl letters,
  • 5:40
    instead of moving around the screen,
  • 5:42
    I'm actually typing text in.
  • 5:44
    This is an important thing to know, to move back and forth
  • 5:47
    between actually inserting, or just executing commands.
  • 5:53
    To delete this stuff, I can hit my back delete key here.
  • 5:56
    So, I'm doing that.
  • 5:58
    You can also, just like on a regular command line,
  • 6:01
    if I do CTRL+H, that will delete my text backwards as well.
  • 6:06
    We'll look more at editing in the next video,
  • 6:07
    but it's very important to understand
  • 6:09
    moving back and forth.
  • 6:10
    Now, to get back to command mode, I hit the escape key,
  • 6:14
    and you notice the insert went away.
  • 6:18
    It's important to know whether you're in insert
  • 6:19
    mode or command mode because your letter keys
  • 6:22
    are going to do different things.
  • 6:24
    When in doubt, always hit escape,
  • 6:26
    get into command mode and then you can start over
  • 6:29
    and figure out what's going on from there.
  • 6:31
    But now you can see I'm in command mode,
  • 6:32
    and I can just move around.
  • 6:34
    Insert, escape.
  • 6:37
    You need to play with that a little bit and get familiar
  • 6:39
    with it, but like I said, when in doubt, hit escape.
  • 6:42
    You'll go back to command mode.
  • 6:46
    So, I've been in command mode, just typing letters
  • 6:49
    on my keyboard.
  • 6:50
    I want to show you another one, another type of command.
  • 6:53
    If you type a colon when you're in command mode,
  • 6:56
    you type a colon. you're going to get this little, sort of,
  • 6:59
    command line at the bottom.
  • 7:01
    And for super uber geeks, this is called x command,
  • 7:05
    in Vi, if you want to impress the geekies.
  • 7:07
    So once I type a colon, there are different kinds
  • 7:10
    of commands I can do.
  • 7:11
    I can just type a number, and it will just take
  • 7:15
    me to that line number in this document.
  • 7:19
    So if I want to jump down to line 112,
  • 7:21
    I just do :112 and it takes me to that line in the document.
  • 7:25
    You can see that down here.
  • 7:27
    That I've moved to line 112 and I'm 72% through my document.
  • 7:31
    I can do :1, takes me back to the beginning of the document.
  • 7:36
    So, that's a really fast and easy way to move
  • 7:39
    to particular lines within a document.
  • 7:42
    Just : number, done.
  • 7:45
    Another way that you can do this,
  • 7:46
    going back to using letters, versus using the x command,
  • 7:50
    is to use the G. So if I do 3 and then shift-g
  • 7:55
    for G, that takes me to the third line.
  • 7:58
    Same thing, 112, shift-g, will take me, it does
  • 8:02
    exactly the same thing.
  • 8:04
    1G takes me to the top.
  • 8:06
    There's, t's really nice if you just do shift-g,
  • 8:09
    a G, you will go to the bottom of the file,
  • 8:12
    and you see I'm at the bottom. You just go directly to
  • 8:16
    the bottom of the file, without having to type line
  • 8:18
    numbers or know how many numbers, or any of that thing.
  • 8:20
    So, it's really nice.
  • 8:23
    So you can use either one to move around,
  • 8:24
    they both work perfectly fine.
  • 8:27
    OK, so we've been moving up and down lines,
  • 8:31
    but let's just take some quick looks like within a line,
  • 8:34
    how do I move back and forth.
  • 8:36
    To go forward, I can, by word, if you type w,
  • 8:40
    it will move you forward a word at a time, through a line.
  • 8:45
    To go back, you do, b. You'll go back a word at a time.
  • 8:48
    So, w, forward a word, b, back a word.
  • 8:54
    To move to the end of the line, you just do a $ sign.
  • 8:58
    Shoots you to the end.
  • 9:00
    A 0, the number zero, not the letter O,
  • 9:03
    the number zero moves you to the front.
  • 9:05
    So, $, end, 0, is the beginning of the line.
  • 9:09
    And then the last thing that we'll look at here,
  • 9:13
    in terms of just moving around is how to page
  • 9:15
    through a document.
  • 9:16
    CTRL+F, will take me down, you know, a page at a time,
  • 9:19
    a screen at a time. And the CTRL+B will take me back
  • 9:24
    a page at a time, all the way back up to the top.
  • 9:28
    So, CTRL+F goes down.
  • 9:29
    CTRL+B goes up, or forward-backwards.
  • 9:34
    So, that's the basics of moving around.
  • 9:36
    Now I want to show you, we're going to look at that
  • 9:39
    quitting again.
  • 9:40
    How to actually get out of my document.
  • 9:42
    I've looked things over, it's all good.
  • 9:43
    Remember the :, we're going to do an x command to quit,
  • 9:46
    and then you do q, and that would be to quit,
  • 9:49
    and when I do it this time, though,
  • 9:51
    it's giving me this little error.
  • 9:53
    It's not letting me quit.
  • 9:54
    It's because I've made changes, I did edits.
  • 9:56
    Remember I did that insert and I did some edits, and so it says,
  • 9:59
    "Hey, you didn't save your changes,
  • 10:01
    so I'm not just going to quit."
  • 10:03
    So I'm going to do, :q and then an !
  • 10:09
    and that says, "No, really quit.
  • 10:11
    I know I didn't save it, that's cool."
  • 10:12
    It's going to quit and not save any of the changes
  • 10:15
    that you made.
  • 10:16
    It's just going to get out of the document
  • 10:18
    and go back to what it was originally.
  • 10:20
    So, those are the basics of getting into a file,
  • 10:22
    moving around, and then getting back out again.
  • 10:24
    Next time, we'll look at editing.
Loading ...

Introduction to Vi/vim Editor


This video introduces you to the Vi (and Vim) editor. Vi is the most common text editor that you will have available to you on *nix systems so it pays to at least learn the basics in case you end up somewhere where that is all you have to use. Vim is also actually a very serviceable editor which many people (mostly hardcore geeks) use as their day to day editor. We'll talk briefly about Vi versus Vim, then open a file, move around, and close the the file. Our next video will dive more into editing files with Vi.

Note: There are a lot of editors out there on various systems, notably emacs, nano, and pico. Vi is considered the lowest common denominator (i.e. it is the most commonly available one), which is why it is the one being covered in the command line basics series. It is also the editor that I use personally, so is the one I am most familiar with. Please limit editor war discussions to other threads on the internet that are meant for them.

Note: this video was originally released July 27, 2010 on

Note: In some places the command line prompt is cut-off. The YouTube version of this video doesn't have the cut-off problem. We are working on getting this fixed, but in the meantime, check out the YouTube version instead.

Command Line Basics 9: Introduction to Vi/Vim editor (

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.