Lesson 2 of 26

Vim file open save, load, moving, changing and selecting text

Duration: 7:04 Watch on YouTube (external link, opens in new tab)

Markdown version

In this lesson

Starting Vim from the command line, Roy writes a hello world file in insert mode, saves it with a colon, w and a file name, and quits with a colon and q, or writes and quits with wq. In what he calls navigation mode he adds lines with o and Shift-O, moves with h, j, k and l and by words with b and w, and undoes with u. He changes text by selecting it with v or Shift-V and pressing c, or with cw and ciw for change word and change in word.

Transcript

1,154 words

[00:12] (external link, opens in new tab) Let's start Vim up. We just type vim in the command line. Press enter. And this is the main screen of Vim. And what we're going to do first, we're just going to write a simple hello world text file. And the way that you enter into writing mode in Vim is you type I for insert mode. And now you can see at the bottom here that we're in insert mode and I can type hello world. Now if you want to get out of insert mode and save stuff, you press escape. And now you can see we're no longer in insert mode.

[00:43] (external link, opens in new tab) If we actually want to save the file, we use a special command line mode which is the colon mode. So you type colon. You see at the bottom we can now type a command and there are many multiple commands. many multiple really for example if you want to write you just say w now if I just type w now I'm going to get an error it says oh no file name to save so let's type colon w hello hello world and I'm going to type enter

[01:15] (external link, opens in new tab) And now I can see that there's a file called hello world now let's say I want to quit vim and I want to see the file so how do you quit vim you type colon and Q for quitting. And now what we can see is that if you type ls here, we have a file called hello world. If I show the contents of hello world, you can see that's the content. So let's try to edit the file again. So I'm going to type vim hello world.

[01:47] (external link, opens in new tab) And you can see that I am in the file again. And I can change the text. But how do I change the text? Well, obviously I can press I and then I can move about with the arrows, but that's not the way Vim is really really designed. You see, the way Vim is designed is to kind of give you a surgical like precision attacking your code. So, let's say I want to add a new line. There's a special command for that. So, I'm not in insert mode. I'm not in command mode. I'm in what we call navigation mode or what I call navigation mode.

[02:20] (external link, opens in new tab) And navigation mode also allows for surgical strikes against your code. So if I just want to add a new line below hello world, all I have to do is to say O for a new line below. So O, I want a new line. And here I am in insert mode with a new line. This is the second line. And press escape again. And the way that things work in Vim usually is that you do something something surgical and then you press escape if you want to get back into navigation mode again.

[02:52] (external link, opens in new tab) Let's say I want to add a line just above this line. I don't want to I don't need to go up or down. All I have to do is to say shift O for a line above. Shift O and I'm in insert mode. This is now the now the second line. Now let's say I want to change second line in the third line to the word third. So how do I move down and up? So one way to move down and up is with the J K keys. So J goes down, K goes up.

[03:24] (external link, opens in new tab) J K J K. So I can just move down and up. And I can also move H and L for left and right. Le H and L left and right. J and K. Down and up. But I can also move between words in a in an easier way. So if I want to move a whole word back, I'll say B in navigation mode and I move a whole word back. If I want to move a word forward, I just say W. So I can move back and front.

[03:55] (external link, opens in new tab) Now let's say I want to change this word. I can do it in multiple ways. For example, I can go into what we call selection mode, which is I can press V and then I can move L to the right like this. And now I can press C to change the selection. So I'm going to press C. And now the word I can change it to third. Press escape and I'm done. Now let's say I want to undo that. So I can show you a different way to do it.

[04:26] (external link, opens in new tab) So to undo, you just press U. So I'm going to I just undid that. Let's say I want to change it. There's an easier way to change stuff in Vim. So I can say CW that is change word. So C W will change a word. So now I can say third and press escape again. Let's undo that. And you can see that there is a pattern. Usually the pattern that I use to change a specific word I just say CW. So CW third escape.

[04:58] (external link, opens in new tab) Now I can be in the middle of a word. So I can say change in word. C I W change in word. Third escape. Get undo. Doesn't matter if I'm the beginning or the end. I'm going to say undo and then say changing word third escape. Let's do that to change it to number two. changing word to escape.

[05:29] (external link, opens in new tab) I can select a whole line and change the whole line with not a regular V but a a shift V. So shift V moves into line selection mode. And now when I move up and down I can go up and down like this. And I can change by pressing C and then doing something else in this line. escape. Okay, so that was a kind of a flurry of commands. So, just to repeat, the commands we've learned so far are going up and down with J and K, up and down.

[06:06] (external link, opens in new tab) V for selecting, H and L to move left and right. B to go backwards whole words, W to go forward whole words. Change CW for changing a word from the beginning. here. Escape changing word c i w in the middle of a word and escape for closing. Let's say I want to save the file. I can do command w col w and then write and it will save the file because it already has a file name.

[06:42] (external link, opens in new tab) But let's say I want to quit as well. I can do col wq for write and then quit. And here I am back in the file. I can press cat hello world and this is a new file. So that's the most simple beginning. Next we're going to look at a bit more complicated stuff.