[ View menu ]
Main

Vi and vim video tutorial for absolute beginners

Filed in Encyclopedia ,Programs ,Tools
Subscribe to Decision Science News by Email (one email per week, easy unsubscribe)

LEARN VI AND VIM BY WATCHING AND TYPING ALONG

For this week, Decision Science News has created a vi and vim video tutorial. (The tutorial is best viewed in your browser’s full-screen mode, try pressing F11 in Windows). Vim is a free and open source editor. DSN highly recommends vim.

This tutorial is for absolute beginners and probably should not even be looked at by people who already know vi or vim.

After finishing the tutorial, try this …

ADDITIONAL EXERCISE:
Find 4 ways to delete the last sentence typed (“End of the line”).

ANSWERS
(Try each of these, typing “u” after each to undo the deletion so that you can try the others.)

1) Move into the sentence with the movement keys and type “das” (delete a sentence).
2) Type dollar sign “$” to go to the end of the line and then “4db” to delete backwards 4 words, since there are 4 words in the last sentence.
3) Go to the end of the sentence as above, then hit “d(” to delete the sentence to the left.
3) Move to the beginning of the sentence with “(” then type “d)” to delete the sentence to the right.

BONUS: HOW TO EXIT AND SAVE FROM VI / VIM
From edit mode “:w filename.txt” will save your file. To quit vim, type :q. To quit without saving type :q!

We have received many questions about why one would want to learn vim in this day and age. A good answer is here.

For reference, the text of the tutorial follows:

Welcome to the Decision Science News vi and vim video tutorial.

To begin the first thing we will do is type “i”. This puts us in insert mode, which means the mode in VIM in which you can enter text. So type something, such as:

“I would like three eggs over easy.

Oh, and some coffee, too.”

Now that you have entered this text, you can move around a bit with the arrow keys and do some crude editing. For example, I can backspace over words and type them again, and the delete key also works.

However, this is generally *not* the way you edit text in VIM. Generally, you switch to “edit mode”. Do this by hitting ESCAPE or Control-OpenBracket Ctrl-[. I will do that now. Notice how the word “INSERT” has disappeared from the bottom of my screen. Now I’m in edit mode. If I hit “i” I’ll go back to insert mode, and ESCAPE takes me to edit mode: i ESC i ESC i ESC

The thing that really confuses people about edit mode is that you can’t just enter text in it. You can only enter special vim commands. We’ll go over some of the most common vim in this tutorial.

First, movement commands. To move down in edit mode, hit “j”. Notice how “j” looks like a down arrow. To move up, hit “k”, it makes sense that up would be right next to down. Since j and k are home keys, you can move up and down quickly.

To move left, hit “h”, which makes sense because it’s to the left of the up and down keys.

To move right, hit “l”. l, h, j, k. That is space by space movement.

Now, let’s give ourselves some more text to work with. Move up to your first line of text and hit “yy”. The “yy” command yanks a line of text, kind of like copying it. Now press “p”. “p” is like “paste” and you’ll see that it pastes the copied line, so we now have our first line twice.

To do a command many times in vi/vim, just type a number and then that command. For example, type 5p and you’ll see that it pastes 5 times.

Similarly, if we type “99p” it will paste 99 times. Try it “99p” I’ve just put 99 lines there. Yikes, how to undo it? Two ways. First type “u” and you’ll undo the last action. If you type “u” the 99 lines are gone. Another way would be to use the delete lines command, which is “dd”. I’ll type “99p” to paste back in the 99 lines, and now what I’ll do is type “99dd” and I just executed the “dd” command (delete line) 99 times. “dd” again is delete line.

How do you join lines? Simple, you use capital J. Up at the top, I can use captial J to put all these sentences into one line: J J J J J. Now you have this long line. I’ll do yy to yank this long line and I’ll hit “p” to paste it. Now we have some text to edit.

We’ve looked at moving left and right character by character, but there is a better way. If we type “w” we skip to the next word or punctuation mark (w w w w) and if we type “b” we move to the previous one: b b b w w w b b b. To jump to the next sentence we hit Close-paren ) Notice how close-paren ) looks like a right arrow. Similarly, Open-paren ( looks like a left arrow and jumps to the previous sentence.

To go to the beginning of the current line, type 0. To go to the end type dollar sign $: 0 $ 0 $ 0 $

Now lets delete some words. Hit “5b” to move back 5 words. Now to delete one word to the right type “dw”. To delete a word to the left you would type “db” (remember that b refers to the word to the left). To delete a sentence, move into the sentence and type “das”. Sentence is gone! Another way to delete a sentence, say the sentence to the left is to type d open-paren “d(” deleted the sentence to the left. I’m going to move back a sentence now to show that if you want to delete a sentence to the right use d close-paren: “d)”

To delete just one character, simply type x. You can use the delete key.

Ok, so we’ve done a bunch of stuff in edit mode, but you probably want to get back to actually typing something. How do you return to insert mode?

There are a few ways. We’ve already talked about i, this will let me insert before the cursor. So we see here the cursor is on the l in “like”, if I type “i” and start typing, I’m inserting before the cursor. I hit ESC to go back to command mode. If I hit “a”, I can start typing after the cursor. And I’ll hit ESC again to go back to command mode.

Now, let’s say I want to insert something on the line below the current line. If I just type “o” I can start inserting on the line below. Now I can type “Hi Dad” and hit ESC to go back to edit mode. To start typing on the line above, I type capital O and hit ESC to go back to command mode. If you have trouble remembering which is above and which is below, think of the Os as filled with helium. The big O will hold more helium and go above the lower case o.

To start inserting at the beginning of the line, type capital letter “I”. I’ll type “Start of line” and type ESC to go back to edit mode. To start inserting at the end of the line and type capital A (think A for “After” the line). I’ll type “End of the line” and then ESC to go to edit mode.

4 Comments

  1. Kaye Richmond says:

    Thank You so much as I am an absolute beginners this was very helpful in preparing for my first quiz!

    September 26, 2009 @ 5:11 am

  2. John Salerno says:

    Great tutorial! It would also be nice to see a video of someone using Vim normally, to get a sense of how quickly you can write and edit a text file with it.

    June 19, 2011 @ 1:57 am

  3. Josie Belliard says:

    Hi
    I absolutely loved your tutorial. I now realize why so many people say VI is hard. Of course… it’s a boring something to learn… but you did such a great job. I think every teacher should learn to speak like you “do”. I think you use a lot of psychology to speak each word to share your knowledge. You voice is very calming… I honestly mean what I just said.
    bellir

    July 25, 2011 @ 3:19 pm

  4. Rajkumar Killedar says:

    Very nice helpful

    February 18, 2014 @ 12:52 am

RSS feed Comments

Write Comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>