Editing with Vi/Vim editor

Video loading...

  • 0:02
    More Editing with Vi/vim Editor with Addison Berry
  • 0:09
    Command Line video.
  • 0:09
    We're going to be looking at editing with the
  • 0:12
    Vi or Vim editor.
  • 0:13
    In the last video, we looked at just moving around.
  • 0:16
    In this one, we're actually going to go in and make some
  • 0:18
    edits to our file and talk about different ways
  • 0:21
    of saving the file once we've made our changes.
  • 0:23
    We're going to start off by looking at the file
  • 0:27
    we were looking at last time, to make some edits.
  • 0:29
    I'm going to move in to the articles directory.
  • 0:33
    And in here, you'll see we have a file.
  • 0:37
    vi is the command I'm going to use to open it,
  • 0:39
    and context-panels.txt.
  • 0:42
    Open that up.
  • 0:43
    Here's my document.
  • 0:44
    And remember, when we first enter Vi,
  • 0:48
    you're always in command mode, so I can use my letter keys
  • 0:51
    and I'm moving around.
  • 0:52
    But what want to actually do is insert,
  • 0:55
    because I want to actually edit text, rather than moving around.
  • 0:57
    So, I'm going to move over to a word,
  • 1:01
    and you'll see that this word is misspelled.
  • 1:02
    It's missing a letter.
  • 1:05
    And so, I'm going to move over and place my cursor exactly
  • 1:08
    where I want to do my insert
  • 1:09
    and I hit i. And you notice the insert appears at the bottom.
  • 1:13
    And now I can type text, and it'll actually be put
  • 1:16
    in to the document.
  • 1:17
    I'll type the a to replace that.
  • 1:19
    I could type more letters if I wanted to.
  • 1:20
    Once I'm done doing the actual text, I hit escape.
  • 1:23
    That puts me back to command, so I can move around.
  • 1:26
    Now I can type letters and I'm not actually inserting
  • 1:30
    that text now.
  • 1:32
    I'm just moving around.
  • 1:35
    Another thing that I can do for insert, so there's i
  • 1:37
    for insert, where the cursor is.
  • 1:38
    If I do shift-a, so a capital A,
  • 1:41
    that will take me to the end of the line,
  • 1:44
    and put me into insert mode right away,
  • 1:46
    so I can just begin typing.
  • 1:47
    You'll see the insert is down here again.
  • 1:51
    And so, I can just begin typing.
  • 1:52
    And it just places my cursor at the end of the line for me.
  • 1:56
    Make my changes, hit escape.
  • 1:58
    I can go up here and I can do the same thing.
  • 2:00
    To go to the beginning of line, you would type a shift-i,
  • 2:03
    or capital I.
  • 2:04
    It does the same thing.
  • 2:06
    It moves me to the beginning of the line
  • 2:07
    and puts me in insert mode.
  • 2:09
    And then I escape to get back to command.
  • 2:12
    You probably want to just play with that a little bit,
  • 2:14
    with those commands, inserting some text,
  • 2:16
    and then escaping and getting familiar with
  • 2:18
    command and insert.
  • 2:20
    But those are the basic ways of getting text into your document.
  • 2:24
    Now I just want to look at deleting text.
  • 2:27
    If I type the letter x, then I will delete text
  • 2:30
    in front of, or to the right of my cursor.
  • 2:32
    If you use a capital X, shift-x,
  • 2:34
    you'll delete the characters to the left of the cursor.
  • 2:37
    You can just use x to delete whatever little bits of
  • 2:40
    text you need here and there.
  • 2:41
    For bigger chunks, you can use the letter d with a direction
  • 2:46
    letter that we learned in our movement video last.
  • 2:47
    So dw will delete a word in front,
  • 2:51
    and db will delete a word backwards,
  • 2:54
    just like as if we were moving,
  • 2:55
    except it's deleting when it moves.
  • 2:57
    A special one is dd, which will delete an entire line.
  • 3:02
    If you just want to drop an entire line, just type dd.
  • 3:04
    The whole thing will disappear.
  • 3:07
    This is all good and well, but what if I want to fix mistakes?
  • 3:10
    I've made too many changes.
  • 3:12
    If I type the letter u, it will undo my last change.
  • 3:15
    And you can keep doing this and keep going backwards.
  • 3:18
    u is really handy to know.
  • 3:20
    Now, sometimes you mess things up so badly you just
  • 3:22
    want to go back to the beginning.
  • 3:24
    And you can do this with an x command,
  • 3:26
    which is when we start with a colon.
  • 3:28
    It's :e, and then I use an exclamation point
  • 3:31
    to say discard all of the changes I've done.
  • 3:33
    I want to edit and discard all changes.
  • 3:36
    When I do that, I go all the way back to the last saved
  • 3:39
    state of this document, which is when I opened it.
  • 3:42
    And you'll notice that the word very is gone,
  • 3:44
    and the a in example is gone.
  • 3:46
    I went all the way back to the beginning of the document.
  • 3:50
    Let's look at how we can actually save our document,
  • 3:53
    so that we don't have to revert all the way back to
  • 3:55
    the very beginning every time.
  • 3:57
    I'm going to go make this change to the word example again.
  • 4:00
    Insert, put the letter a, and escape.
  • 4:03
    And then I want to save the document at this state.
  • 4:06
    We're going to do that with an x command,
  • 4:08
    which means a colon, and then the letter w for write.
  • 4:11
    So, :w saves the document in its current state.
  • 4:18
    Then, this is how you save your document as you go.
  • 4:20
    And it's something you should really get into the habit of,
  • 4:22
    just like when you're writing any other document.
  • 4:25
    :w keeps you going.
  • 4:27
    Let's go ahead and make some more changes
  • 4:30
    in this document here.
  • 4:30
    I'm going to destroy some stuff and mess up my beautiful
  • 4:34
    document again.
  • 4:35
    Now, if I come down and I've messed everything up
  • 4:40
    and I want to revert, and now I'm going to do. Again,
  • 4:43
    to revert back to the last saved state, :e!.
  • 4:50
    And everything comes back.
  • 4:51
    But you'll notice that the word example has
  • 4:53
    now stayed the same, because I saved the document.
  • 4:57
    Alright, now I'm just going to make some changes here,
  • 4:59
    making some edits.
  • 5:00
    And we're going to be done with the document.
  • 5:03
    I've made the changes I need to change.
  • 5:04
    Now, remember quitting, you can do :q.
  • 5:07
    When I try and do that, it says, "Hey, you've made changes,
  • 5:10
    so you can't just quit.
  • 5:12
    You have to tell me what to do with the changes."
  • 5:13
    Now, before we said I could do q!, which means quit and don't
  • 5:19
    save any of my changes.
  • 5:20
    If I want to save my changes, though, I could do a :wq,
  • 5:24
    write it, and then quit.
  • 5:25
    So, save and quit.
  • 5:27
    You can do :wq, works fine.
  • 5:31
    Or you can just do ZZ.
  • 5:33
    So, shift-z-z does exactly the same thing as
  • 5:37
    :wq, and goes ahead and saves your changes.
  • 5:41
    That's some basics of making some edits, getting in,
  • 5:44
    and how to get out of that, and save your changes.
  • 5:48
    In the next video, we'll look more at copy-paste
  • 5:50
    and some other fun stuff.
Loading ...

Editing with Vi/vim Editor

Loading...

In this second Vi/Vim video we move on to doing some basic editing (see the Intro to Vi/Vim video for an overview). We cover inserting text in a few different ways, how to delete text, and then how to revert or save your changes. Note: this video was originally released August 9, 2010 on Lullabot.com.

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 10: Editing with Vi/Vim editor (youtube.com)

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