1 00:00:06,639 --> 00:00:11,869 Okay, for the many reasons that we discussed in the previous video, we will now talk about vim. 2 00:00:12,689 --> 00:00:17,589 So what is there to know about vim? Well, after starting vim, it opens in Command mode, 3 00:00:17,589 --> 00:00:22,489 and command mode is called Command mode because you can only type commands in that mode. 4 00:00:23,570 --> 00:00:29,295 So if you want to enter text, the first thing you need to do is press either I or O to 5 00:00:29,295 --> 00:00:35,020 get into insert or input mode. And once you're done typing text, you press ESC to get back to Command mode. 6 00:00:35,740 --> 00:00:40,000 Now, in Command mode, there are a couple of important commands that you need to know about. 7 00:00:40,560 --> 00:00:45,960 Well, in fact, there are so many commands in VIM that it easily will give you a headache. 8 00:00:46,640 --> 00:00:51,085 If I may give you an advice, don't try to memorize all of these commands because 9 00:00:51,085 --> 00:00:55,530 you will notice that sometimes there are five different commands to do the exact same thing. 10 00:00:56,250 --> 00:01:00,670 Focus on a handful of commands, learn how to work with VIM in a comfortable 11 00:01:00,670 --> 00:01:05,090 way based on that handful of commands, and then you can learn about other commands. 12 00:01:06,170 --> 00:01:07,799 So the first relevant command is wq. 13 00:01:11,469 --> 00:01:14,049 The command starts with a colon, because it does. 14 00:01:14,790 --> 00:01:19,420 Believe me, you don't want me to tell you about X mode, which was relevant in 1984 when it was first invented. 15 00:01:22,650 --> 00:01:28,370 W is for write, Q is for quit, and exclamation mark is for don't complain about anything, just do it. 16 00:01:29,890 --> 00:01:35,174 If ever you deleted something by accident, you are going to like U. U for undo, 17 00:01:35,174 --> 00:01:40,459 undo the last change and DD is what allows you to delete the current line. 18 00:01:41,239 --> 00:01:46,144 You will notice that in Linux, editing text files is very often line oriented. So 19 00:01:46,144 --> 00:01:51,049 it's convenient if you can easily delete an entire line if you don't need it. 20 00:01:52,319 --> 00:01:56,400 And one that I really like is the column percent s, which is substitute. 21 00:01:57,459 --> 00:02:02,079 Old is the text that it is looking for, new is the text that it's substituting for. 22 00:02:02,750 --> 00:02:08,034 And the G makes it a global substitute without the G, it would only substitute the first 23 00:02:08,034 --> 00:02:13,319 time it finds the text on the line and it would leave the other times alone. 24 00:02:13,860 --> 00:02:17,460 And by making it a global substitute, you replace it all the time. 25 00:02:18,400 --> 00:02:23,344 Now what I would recommend is you use Vim Tutor. Vim Tutor provides an easy to 26 00:02:23,344 --> 00:02:28,289 use on system Tutor that guides you through the essentials of using VI and Vim. 27 00:02:28,990 --> 00:02:33,704 It will take you about 20 minutes and I would say go for it, go use it, because 28 00:02:33,704 --> 00:02:38,419 you'll learn a lot from it, even these keys that you probably never want to work with. 29 00:02:39,659 --> 00:02:45,340 So let me use VIM regex. I'm going to need this later. 30 00:02:46,020 --> 00:02:52,020 And now I'm in A new file with the name regex. You can see that in the lower left corner, it indicates new. 31 00:02:52,539 --> 00:02:58,259 And I'm going to use I for input. And here you can see that I'm in insert or input mode. 32 00:02:58,939 --> 00:03:05,192 And I'm going to use B N n and B O N 33 00:03:05,192 --> 00:03:11,444 N E n and B O o n and B o o 34 00:03:11,444 --> 00:03:17,697 N E n and BT and bot and boot and boat and 35 00:03:17,697 --> 00:03:23,949 B O, A, B, C, D, F, G, A T. 36 00:03:25,189 --> 00:03:29,879 That sounds like a weird file, but hey, I need it for a later demo and I kind of like it. 37 00:03:30,400 --> 00:03:31,280 So I'm done now. 38 00:03:31,800 --> 00:03:37,509 Now what do I need to do first? Well, we need this command to write and quit. 39 00:03:38,229 --> 00:03:43,629 First I'm using escape to get back to command mode, and now 40 00:03:43,629 --> 00:03:49,030 I'm using WQ to write quit and don't ask any smart questions. 41 00:03:52,229 --> 00:03:55,849 Now that we know about the basics of working with vim, 42 00:03:55,849 --> 00:03:59,469 let's talk about a couple of more useful VIM commands. 43 00:04:00,590 --> 00:04:07,110 I tried to make some reference slides, and on the reference slides I'm trying to go beyond the useful commands. 44 00:04:08,069 --> 00:04:09,370 What do we see on this slide? 45 00:04:10,009 --> 00:04:10,030 Q. 46 00:04:12,090 --> 00:04:14,710 That's your desperate mode. Quit without saving. 47 00:04:15,810 --> 00:04:18,720 U is for undo and Control R is for redo. 48 00:04:19,259 --> 00:04:22,985 You know, sometimes you want to undo something and oops, one undo too 49 00:04:22,985 --> 00:04:26,710 many. Then you can use Control R to undo the last undo. 50 00:04:27,769 --> 00:04:32,134 V is for visual mode. It uses arrow keys to select a block, and 51 00:04:32,134 --> 00:04:36,500 D is to delete the current selection, which is useful after selecting a block. 52 00:04:37,439 --> 00:04:40,829 Then you can use Y to copy or yank the current selection. 53 00:04:41,410 --> 00:04:44,819 And a command like 3dd which deletes 3 lines. 54 00:04:45,899 --> 00:04:47,199 Text will search for text. 55 00:04:47,720 --> 00:04:53,090 GG will move the cursor to the top of the document, and uppercase G will move the cursor 56 00:04:53,090 --> 00:04:58,460 to the end of the document before going to the next slide. Let's do a quick demo. 57 00:05:00,720 --> 00:05:05,519 I'm copying regex to vim trial and I'm 58 00:05:05,519 --> 00:05:10,319 going to use vim on vim trial. 59 00:05:12,240 --> 00:05:16,939 Let's imagine that I'm using this V for visual, uppercase, D to 60 00:05:16,939 --> 00:05:21,639 go to the bottom, and D for delete. Oh my goodness. What 61 00:05:21,639 --> 00:05:26,339 happened? Well, I just accidentally deleted all of my contents. 62 00:05:27,100 --> 00:05:33,460 You know what I can do? I can use Escape, I'm making sure that 63 00:05:33,460 --> 00:05:39,820 I'm in the command mode, and colon Q which is quit without writing anything. 64 00:05:40,660 --> 00:05:41,839 That's doing so much better. 65 00:05:43,019 --> 00:05:46,754 What else can I show you? Well, let's get back 66 00:05:46,754 --> 00:05:50,490 to this VIM trial and let me use 3dd. 67 00:05:51,069 --> 00:05:55,379 3dd will delete three lines. Oh, I regret. So I'm using u for undo. 68 00:05:56,370 --> 00:06:01,555 Now, uppercase g is convenient, lowercase g two times is convenient. Did you 69 00:06:01,555 --> 00:06:06,740 see that? That moves the cursor up and down in the current configuration. 70 00:06:07,319 --> 00:06:11,904 And if ever you want to search for text, then you use slash followed by 71 00:06:11,904 --> 00:06:16,490 the text that you are looking for. And there you can see it's automatically highlighted. 72 00:06:17,449 --> 00:06:23,384 And that allows you to do whatever you want to do with it. Well, what I 73 00:06:23,384 --> 00:06:29,319 want to do with it right now I want to Q to quit without saving anything. 74 00:06:30,079 --> 00:06:32,439 I have a couple more useful VIM commands for you. 75 00:06:33,339 --> 00:06:34,420 Let's talk about them. 76 00:06:35,660 --> 00:06:41,620 So we have W which allows you to go to the next word, or B which allows you to go to the previous word. 77 00:06:42,540 --> 00:06:47,180 Caret moves to the start of the current line, and dollar moves to the end of the current line. 78 00:06:47,759 --> 00:06:50,139 And dw is for deleting the current word. 79 00:06:50,689 --> 00:06:54,220 Uppercase o opens a line above the current position and 80 00:06:54,220 --> 00:06:57,750 lowercase o opens a line after the cursor position. 81 00:06:58,550 --> 00:07:02,709 And I like senumber, that will show line numbers. Very convenient in programming. 82 00:07:03,319 --> 00:07:09,615 And R which will replace only one single character. Using VIM While working with vi, 83 00:07:09,615 --> 00:07:15,910 it's very important that you realize that there's command mode and there's input mode. 84 00:07:17,000 --> 00:07:20,709 So command mode, that's what you start in. It's for entering commands. 85 00:07:21,629 --> 00:07:25,620 And input mode, also known as insert mode, is where you type your text. 86 00:07:26,639 --> 00:07:33,076 The first challenge that you will have while working with vi is how do you get into input mode? Well, 87 00:07:33,076 --> 00:07:39,513 different keys are available. The I, lowercase o, uppercase o, and even more, I'm not going to list all of 88 00:07:39,513 --> 00:07:45,949 them because when you want to work with vi, definitely don't try to memorize all of these different commands. 89 00:07:46,470 --> 00:07:51,125 Now, once you're in input mode, you can type your text and at a certain moment you're done. And 90 00:07:51,125 --> 00:07:55,779 then in order to save your text, you need to press escape to get back to command mode. 91 00:07:56,819 --> 00:08:03,414 When you're back in command mode, you can do whatever you want to do with your document. Some of the 92 00:08:03,414 --> 00:08:10,009 most obvious commands are colon w, q exclamation mark for instance for write, quit and don't ask any questions. 93 00:08:10,629 --> 00:08:16,430 Or q exclamation mark if you want to make sure that nothing is changed, or U if you want to 94 00:08:16,430 --> 00:08:22,230 undo, or dd if you want to delete a line. And many more commands are available in command mode. 95 00:08:22,829 --> 00:08:28,470 I'm not going to write them all here on the lightboard. I've listed the most important commands on the next couple of slides. 96 00:08:29,009 --> 00:08:34,046 Try to get used to them because really these are the most essential commands and don't 97 00:08:34,046 --> 00:08:39,083 let these VI cheat sheets drive yourself crazy. Because it's way too many commands. You don't 98 00:08:39,083 --> 00:08:44,120 need to memorize all of them in order to be successful with the VI Editor.