More Editing with Vi/Vim editor

Video loading...

  • 0:02
    Handy Command Line Shortcuts with Addison Berry
  • 0:07
    In this Command Line video, we're going to be looking
  • 0:10
    at some more options for editing in the Vi or Vim editor.
  • 0:14
    In the last video, we've looked at basic deleting
  • 0:17
    and inserting of text, and in this one,
  • 0:19
    we'll look at a few more options with that,
  • 0:21
    as well as how to copy-paste our content.
  • 0:24
    So, just like in our other Vi videos,
  • 0:27
    I'm going to go ahead and open up a document that
  • 0:29
    we've been working on here, which is context-panels.
  • 0:31
    So, vi is the command to go into the editor mode,
  • 0:35
    and I'm opening my document up.
  • 0:38
    Now, last video we looked at, we showed how you could do
  • 0:45
    some editing by deleting texts, and then inserting.
  • 0:48
    So, if I want to change this word accomplish to
  • 0:51
    something else, I could use dw to delete that word,
  • 0:53
    i insert, and then I can type in my new text and that works
  • 0:58
    perfectly well, and is awesome. But a shortcut way to do that,
  • 1:01
    I'm going to undo this, back to the original word here,
  • 1:05
    a shortcut way to do this is to use cw to change the word.
  • 1:09
    It deletes the word and automatically puts
  • 1:11
    me into insert mode.
  • 1:13
    So I don't have to do 2 different commands,
  • 1:14
    and then I can just type my text,
  • 1:17
    and then I'll hit escape there to get out of that.
  • 1:20
    So, cw is just a shortcut for doing dw and i.
  • 1:25
    There's also a shortcut for doing dd
  • 1:28
    which deletes a line, and then i and that
  • 1:30
    is cc, will let us change a line.
  • 1:34
    So it'll delete a line and then put us into insert mode,
  • 1:36
    so that I can just get rid of a line and then,
  • 1:38
    and then type what I want in there.
  • 1:40
    So, cw and cc are really handy little shortcuts.
  • 1:44
    I'm going to go ahead and undo this.
  • 1:46
    Then we have one other thing that I wanted to show you
  • 1:49
    in terms of dealing with text and replacing it.
  • 1:53
    I'm going to go over here to another word,
  • 1:54
    and if instead of deleting and re-writing something,
  • 1:57
    I just want to write over what exists,
  • 2:00
    I do a capital r and you'll see that we now have replace,
  • 2:03
    rather than insert, and when I begin typing,
  • 2:06
    I'm just going to type over top of the existing letters there.
  • 2:10
    That's cool, but you'll notice, you know,
  • 2:12
    that word was the same number when I type a longer
  • 2:14
    word and then I start to change things.
  • 2:16
    So it works for little bits, but it's not something
  • 2:18
    you generally are going to use for extensive stuff,
  • 2:20
    but capital r to replace texts,
  • 2:22
    is also a handy shortcut to just sort of jump in
  • 2:25
    and change what you need.
  • 2:28
    Another common editing task is copy and paste, and the command,
  • 2:32
    the letter command for this within Vi is y for yank,
  • 2:38
    and we can yank a word, yw. So, I yanked example,
  • 2:42
    and then I'll go up here and I'll just show you,
  • 2:44
    p will put or paste that word wherever my cursor is.
  • 2:48
    So, I can undo that, and I'll show this again.
  • 2:52
    You can also do this with multiple words at the same time.
  • 2:55
    So you'll see on this line, so let's say I wanted
  • 2:58
    to grab these.
  • 2:59
    This is 4 or 5 words.
  • 3:01
    So, I'll go back and I can put the number of
  • 3:04
    words I want to yank.
  • 3:05
    So 5 yank w, 5yw. That just copied all
  • 3:11
    5 of those words from my cursor,
  • 3:12
    and if I come back up here, and move to, say,
  • 3:16
    the end of this line, because I want to paste this at the end,
  • 3:21
    I will do p, and that puts the text, but notice,
  • 3:25
    it didn't put any space because it does it directly
  • 3:27
    after the cursor.
  • 3:28
    So I can just go back here and insert a space that
  • 3:31
    I might need, or something like that. But y with a word,
  • 3:37
    with a number will do multiple and p is always
  • 3:40
    going to put it directly after wherever
  • 3:42
    your cursor happens to be.
  • 3:44
    Following a pattern that we've seen before,
  • 3:47
    I can also yank or copy an entire line by doing yy.
  • 3:53
    So, whenever you do, like, a double letter it
  • 3:55
    affects the whole line.
  • 3:56
    So if I do yy, that copies the entire line,
  • 3:58
    p, and then I paste that entire line in.
  • 4:01
    So, similar operation.
  • 4:04
    Just remember y is for copy and then p is for paste.
  • 4:07
    And alright, so I'll escape out of there and come back down.
  • 4:11
    I also want to show, you can do that with several lines, right?
  • 4:15
    Instead of just one line at a time.
  • 4:16
    So if I go down and count the number of lines here,
  • 4:18
    that I, and see that I have 7.
  • 4:21
    I can do 7yy so the number of lines I want to copy, yank them.
  • 4:26
    It tells me I've yanked 7 lines, down here in the bottom.
  • 4:29
    So I know what I've got and then again, I'll just go up here,
  • 4:34
    and again, p, paste it, and it just goes ahead
  • 4:36
    and pastes those lines in for me.
  • 4:39
    So the whole number with command thing, as you can see,
  • 4:42
    works with words and with lines.
  • 4:44
    And then, another thing that I would like to show you is because
  • 4:49
    counting all these lines can get really kind of annoying,
  • 4:53
    and sometimes it's hard when you have a whole bunch of stuff.
  • 4:56
    And Vim, not Vi, but Vim has a visual mode, and so,
  • 5:01
    if you have Vim, which most of the time you will,
  • 5:05
    you can actually use a visual mode to select what it is that
  • 5:07
    you want to work with, rather than having to count things
  • 5:10
    out and type the numbers in, and such like that.
  • 5:13
    So let's look at that.
  • 5:13
    I'm going to go down here, and I'm going to type the letter
  • 5:16
    v to enter visual mode in Vim.
  • 5:19
    And then once I do that, I'll be able to highlight
  • 5:22
    text with my cursor.
  • 5:24
    So, I'm in visual mode now, notice,
  • 5:26
    it tells me I'm in visual and now if I say,
  • 5:29
    line down with using j, make my cursor go down,
  • 5:33
    you'll see I'm highlighting a whole bunch of text.
  • 5:36
    So now that I've highlighted that text,
  • 5:38
    I can just use y and it will yank all of that,
  • 5:42
    and it tells me 10 lines, because I selected a bit more
  • 5:44
    than I did last time.
  • 5:45
    And then, when I go up and paste, or p,
  • 5:49
    it goes ahead and copies all that stuff.
  • 5:51
    So this is a little bit easier way to sort of work with it.
  • 5:54
    You can use the visual mode, not just with, you know,
  • 5:57
    lines with j and k, moving, it's just wherever you move
  • 6:00
    your cursor, you're going to be visually selecting.
  • 6:02
    So if I go back into v mode, then we'll see that
  • 6:10
    I can use all kinds of letter commands with these, right.
  • 6:13
    So to select up, I can go to the right and just select words.
  • 6:16
    I don't have to select entire lines,
  • 6:19
    and then you can use all the different letter commands.
  • 6:22
    So d deletes, and you can use all the different commands.
  • 6:25
    So it's not just for copying, to go into visual mode.
  • 6:28
    Once you're in there, you can use any of the letter commands
  • 6:30
    that you've learned.
  • 6:31
    So, if I go in here and, let's see, well, let me go up.
  • 6:36
    Yeah, let's select that and then I hit c to change, and again,
  • 6:40
    it puts me into insert mode.
  • 6:42
    Deleted that, puts me into insert mode.
  • 6:45
    So, visual is really great, but just remember,
  • 6:47
    it's only available with Vim and not with plain Vi.
  • 6:52
    Though also keep in mind that many commands with Vi,
  • 6:54
    many servers, that actually does map to Vim.
  • 6:57
    So most of the time, you'll be able to use that,
  • 6:59
    which is really handy.
  • 7:04
    OK, so I'm going to go ahead and save out of this document
  • 7:07
    here, by doing capital zz, which will write
  • 7:10
    my changes and then quit out of the document,
  • 7:13
    and that's a little intro to some more things that you
  • 7:16
    can do with editing.
  • 7:17
    We looked at changing things, yanking and pasting,
  • 7:22
    and then the really awesome visual mode that lets you
  • 7:25
    kind of really see what it is that you're doing.
  • 7:28
    And that's it for this video, see you in the next one.
Loading ...

More Editing with Vi/vim Editor


This video picks up where we left off in the Editing with Vi/Vim video. This time we take a look at some shortcuts for replacing text, how to copy/paste, and the cool visual mode feature you get with Vim Note: this video was originally released August 31, 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 11: More Editing with Vi/Vim editor (

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