1 00:00:00,000 --> 00:00:11,010 [Intro music] 2 00:00:11,010 --> 00:00:14,490 Hello everyone. In this lesson we will cover the 3 00:00:14,490 --> 00:00:18,240 Linux file editor. An editor is a program which 4 00:00:18,270 --> 00:00:22,650 enables you to create and manipulate data or text 5 00:00:22,860 --> 00:00:26,250 in a Linux file. There are several standard text 6 00:00:26,490 --> 00:00:29,970 editors available on most Linux systems. You could 7 00:00:29,970 --> 00:00:35,820 use vi, ed, ex, emacs, pico, vim, which is, 8 00:00:35,820 --> 00:00:40,020 vim is the advanced version of vi. In our 9 00:00:40,020 --> 00:00:46,110 lesson, we will focus on vi. Why? Because vi is 10 00:00:46,170 --> 00:00:49,470 available in almost every Linux distribution, 11 00:00:49,590 --> 00:00:52,050 regardless of which Linux system you log in. 12 00:00:52,050 --> 00:00:55,770 Whether it's Ubuntu, SUSE, Red Hat, CentOS, you're 13 00:00:55,770 --> 00:01:00,450 gonna find vi everywhere. Plus, it's very easy to 14 00:01:00,450 --> 00:01:05,849 learn. The vi editor actually supplies commands 15 00:01:05,849 --> 00:01:09,930 for inserting or deleting text, replacing text, 16 00:01:10,170 --> 00:01:13,200 moving around the file, finding substitute 17 00:01:13,200 --> 00:01:16,230 strings, and cutting and pasting text inside. So 18 00:01:16,230 --> 00:01:18,900 that's what vi does when you have to write to a 19 00:01:18,900 --> 00:01:23,340 file. The most common keys are i which should 20 00:01:23,340 --> 00:01:27,480 insert, escape, replace for r, d for delete, 21 00:01:27,900 --> 00:01:31,020 and quit without saving, and quit write with save. 22 00:01:31,020 --> 00:01:34,440 So let's look at our Linux machine. And at this 23 00:01:34,440 --> 00:01:36,570 time, we will log into our Linux machine, the 24 00:01:36,600 --> 00:01:40,140 Linux virtual machine through our PuTTY session. 25 00:01:40,176 --> 00:01:45,569 [No audio] 26 00:01:45,569 --> 00:01:51,983 Okay, let's cd, whoami, and which directory we are in. 27 00:01:51,983 --> 00:01:53,868 [No audio] 28 00:01:53,868 --> 00:01:56,489 In this directory, I want you to create 29 00:01:56,489 --> 00:02:00,329 your first file, so call it vi, which is a 30 00:02:00,329 --> 00:02:07,567 command, space myfirsteditor, 31 00:02:09,251 --> 00:02:14,489 or just say myfirstfile, how's that? Much easier, hit enter. 32 00:02:15,479 --> 00:02:19,739 Now, when you hit enter, you are still in command 33 00:02:19,739 --> 00:02:24,779 mode, you are not in insert mode. So there are two 34 00:02:24,779 --> 00:02:28,349 different modes once you are in the vi. This first 35 00:02:28,349 --> 00:02:31,049 mode is to when you run the command, second mode is 36 00:02:31,049 --> 00:02:34,319 when you're inside of the vi and you start typing it. 37 00:02:34,949 --> 00:02:37,229 If you want to get out of that typing mode, you 38 00:02:37,229 --> 00:02:40,349 have to hit escape. So now let's start, let's say 39 00:02:40,349 --> 00:02:44,399 if I wanted to type into this file, you hit i for 40 00:02:44,429 --> 00:02:48,629 insert, lowercase i. As soon as you hit it, you will 41 00:02:48,629 --> 00:02:52,649 see at the bottom, it says INSERT. It means we are 42 00:02:52,679 --> 00:02:55,889 in insert mode. Now our regular Linux commands 43 00:02:55,889 --> 00:02:58,919 won't work because we are in insert mode. 44 00:02:58,919 --> 00:03:03,869 So let me type something like Hello World. 45 00:03:05,219 --> 00:03:14,759 This is my first lesson to learn vi. And you could 46 00:03:14,759 --> 00:03:22,259 hit enter again, I am so excited about it. So it's 47 00:03:22,259 --> 00:03:25,739 just simple as if you're typing a word document. 48 00:03:25,769 --> 00:03:29,729 Now if you wanted to exit out of it, if you want 49 00:03:29,729 --> 00:03:32,879 to save this file, all you have to do is hit 50 00:03:32,909 --> 00:03:36,689 escape right here. And once you hit escape, you'll 51 00:03:36,689 --> 00:03:40,439 see the insert mode is gone. Now how do you save 52 00:03:40,499 --> 00:03:43,559 this file? You could do through two different 53 00:03:43,559 --> 00:03:46,628 ways. One, you hit shift zz, 54 00:03:48,263 --> 00:03:50,279 the zz twice, and it 55 00:03:50,279 --> 00:03:55,589 will save the file. Let's see, if you do cat again, 56 00:03:55,649 --> 00:03:59,279 and do myfirstfile, you'll see it's there. 57 00:03:59,849 --> 00:04:02,279 Another way to save a file, let's go back in the 58 00:04:02,279 --> 00:04:07,979 file, myfirstfile, and let's say you want to type 59 00:04:07,979 --> 00:04:16,918 something, hit insert again, and then hi space. 60 00:04:17,789 --> 00:04:21,689 And now you want to exit out and save, hit escape, 61 00:04:22,319 --> 00:04:26,459 and this time instead of shift zz, you type shift, 62 00:04:26,459 --> 00:04:31,319 colon, write, quit, and exclamation point, which is 63 00:04:31,319 --> 00:04:35,879 also referred to as bang, you hit enter. How do you 64 00:04:35,879 --> 00:04:40,049 verify? You read the file myfirstfile, and there 65 00:04:40,049 --> 00:04:42,899 you go, the file is there. So these are the 66 00:04:42,899 --> 00:04:45,839 two modes, is once you get out and once you get in. 67 00:04:46,319 --> 00:04:50,729 Now I want you to go back into your file and hit 68 00:04:50,729 --> 00:04:52,564 vi and myfirstfile, 69 00:04:53,283 --> 00:04:55,252 and I want you to replace 70 00:04:55,252 --> 00:04:57,738 [No audio] 71 00:04:57,738 --> 00:05:02,219 hi, no I want you to delete hi and leave that as is. 72 00:05:02,789 --> 00:05:07,499 Now how do you go move around inside of a file 73 00:05:07,499 --> 00:05:10,319 when you are already in your editor. You could use 74 00:05:10,379 --> 00:05:15,903 up, bottom, left, right keys on your keyboard to move around. 75 00:05:15,903 --> 00:05:17,955 [No audio] 76 00:05:17,955 --> 00:05:20,699 But remember, you cannot use these 77 00:05:20,699 --> 00:05:24,929 keys if you are in insert mode. So let's say if 78 00:05:24,929 --> 00:05:29,489 you hit i, and you now in insert mode, and now 79 00:05:29,489 --> 00:05:34,109 you do a left, right key, it can work, but most 80 00:05:34,109 --> 00:05:38,099 likely it will break your commands. So it's better 81 00:05:38,099 --> 00:05:45,903 you hit escape, and go back, and then you do d on your keyboard, d. 82 00:05:45,903 --> 00:05:49,824 [No audio] 83 00:05:49,824 --> 00:05:53,489 This will delete the entire line. 84 00:05:54,059 --> 00:05:58,799 I wanted to delete only the word hi. So how do I 85 00:05:58,799 --> 00:06:02,639 get back my last line that I deleted. So you hit 86 00:06:03,239 --> 00:06:07,349 u, just simple u, and it brings back your last 87 00:06:07,349 --> 00:06:12,239 line. Now, if you wanted to remove the hi, just 88 00:06:12,239 --> 00:06:16,769 type x, once, which will remove one character, type 89 00:06:16,769 --> 00:06:19,619 x again, which will remove another character, now 90 00:06:19,619 --> 00:06:23,189 you have a space, hit x again. Now you don't have a 91 00:06:23,189 --> 00:06:27,479 space. Now what if you want to save now, again do 92 00:06:27,479 --> 00:06:32,969 shift zz, twice, and it saves it. How do you verify 93 00:06:32,969 --> 00:06:37,889 it? You do cat myfirstfile, and it is there. 94 00:06:39,120 --> 00:06:43,711 Now let's go back in again, and let's practice the replace command. 95 00:06:43,711 --> 00:06:48,300 vi myfirstfile. I want it to 96 00:06:48,300 --> 00:06:51,900 replace, I am so excited about it.,I want it to 97 00:06:51,900 --> 00:06:55,980 replace the word excited with the word happy. So 98 00:06:55,980 --> 00:07:00,330 how do we do it? We cannot hit i right now 99 00:07:00,330 --> 00:07:02,760 because we don't want it to get into the insert 100 00:07:02,760 --> 00:07:06,900 mode. So we will use our arrow keys to go down to 101 00:07:06,900 --> 00:07:12,022 the word excited, so I could do r. 102 00:07:13,590 --> 00:07:14,940 Once I hit r, I 103 00:07:14,940 --> 00:07:20,640 could do h, it only replaced one letter. If I 104 00:07:20,670 --> 00:07:23,910 wanted to do again the second letter, you move 105 00:07:23,910 --> 00:07:30,360 your cursor to the right, hit r, and then do a, and so 106 00:07:30,360 --> 00:07:34,320 forth. But I would prefer that you do escape out 107 00:07:34,320 --> 00:07:41,400 of it. And then do x x x x x, and then you hit i, 108 00:07:42,030 --> 00:07:48,690 and then ppy. You hit escape again. so you can 109 00:07:48,690 --> 00:07:54,000 move around. You could go up, you could go down if 110 00:07:54,000 --> 00:07:59,220 you wanted to move around. Now what if you 111 00:07:59,220 --> 00:08:02,130 want to go to the end of this file, and now you 112 00:08:02,130 --> 00:08:07,590 want to create a new line right below it, before I 113 00:08:07,590 --> 00:08:12,930 am so happy about it, you hit o. This will create 114 00:08:12,960 --> 00:08:16,830 a new line for you, and also it will automatically 115 00:08:16,830 --> 00:08:20,370 put you into the insert mode. So you could type 116 00:08:20,400 --> 00:08:27,390 anything like blah blah blah. But you just 117 00:08:27,390 --> 00:08:32,010 noticed that you just type j by mistake. Now how 118 00:08:32,010 --> 00:08:36,270 do you remove j, you type x. Now remember if you're 119 00:08:36,270 --> 00:08:39,360 going to type x, it will write x instead of deleting 120 00:08:39,360 --> 00:08:44,100 it. Look, see it is typing the x. So why to delete that 121 00:08:44,100 --> 00:08:46,289 character? How do I delete that character, it is 122 00:08:46,289 --> 00:08:50,490 because you cannot delete. Why? Because you are in 123 00:08:50,520 --> 00:08:54,179 insert mode. How do I get out of insert mode? You 124 00:08:54,179 --> 00:08:59,670 hit your life saving key which is escape. 125 00:09:00,000 --> 00:09:02,640 As soon as you hit escape, you see the insert mode 126 00:09:02,640 --> 00:09:09,540 is gone. Now when you hit x x, it deletes those 127 00:09:09,570 --> 00:09:13,500 two characters that you added by mistake, it was a 128 00:09:13,500 --> 00:09:18,060 typo that you added. Same way if you want to go up, 129 00:09:19,200 --> 00:09:22,140 you could, if you want to write something right 130 00:09:22,140 --> 00:09:26,730 here, and if you type i, you notice it start 131 00:09:26,730 --> 00:09:30,990 writing right in the middle. Hellohi, see without 132 00:09:30,990 --> 00:09:36,420 giving the space. So let's do escape again. Hit x, 133 00:09:37,350 --> 00:09:41,970 move the cursor to the left, and x, okay. And now 134 00:09:41,970 --> 00:09:45,810 you have that space in the middle. Now how do I 135 00:09:45,810 --> 00:09:51,960 write by putting a space in the middle? I do a, So 136 00:09:51,990 --> 00:09:56,220 a will automatically advance to the next space. So 137 00:09:56,220 --> 00:10:03,240 Hello hi bye space world. Okay, now you're 138 00:10:03,240 --> 00:10:05,760 still in insert more, what if you want to save it? 139 00:10:07,350 --> 00:10:10,770 Before you save it again, get out of insert mode 140 00:10:10,770 --> 00:10:15,780 by hitting escape, and do shift zz. This saved 141 00:10:15,780 --> 00:10:22,740 your file, cat, myfirstfile, and it is saving 142 00:10:22,770 --> 00:10:25,560 your first file. Now what if I wanted to delete 143 00:10:25,560 --> 00:10:29,190 the line, blah blah blah, the third line. So 144 00:10:29,190 --> 00:10:35,070 I'll go back into my file vi myfirstfile, move 145 00:10:35,070 --> 00:10:40,080 your cursor down to blah blah blah, and hit d d 146 00:10:40,080 --> 00:10:45,150 twice, one, two. There you go, it removes the line 147 00:10:45,240 --> 00:10:49,080 that you added wherever you want to go. If 148 00:10:49,080 --> 00:10:52,170 you want to, oh, you removed it by mistake, no 149 00:10:52,170 --> 00:10:57,210 problem, u will bring it back. Again, you 150 00:10:57,210 --> 00:11:00,450 could save it through shift zz, or you could save 151 00:11:00,450 --> 00:11:04,410 it through colon write quite, 152 00:11:05,850 --> 00:11:11,640 and bang, you could hit enter. And that's how you 153 00:11:11,640 --> 00:11:14,640 save the file. Now let's go back into the file one 154 00:11:14,640 --> 00:11:20,280 more time. vi myfirstfile, and I want to let's 155 00:11:20,280 --> 00:11:25,290 say, I have a file, which is huge, and I wanted to 156 00:11:25,320 --> 00:11:29,280 search for a specific word in a file. Let's say I 157 00:11:29,280 --> 00:11:34,170 wanted to search for lesson, I wanted to search 158 00:11:34,170 --> 00:11:40,110 for l. So you could do search for it two ways. I 159 00:11:40,110 --> 00:11:43,560 hope you remember the command grep. And then you 160 00:11:43,560 --> 00:11:47,550 search for lesson, from where, myfirstfile. And it 161 00:11:47,550 --> 00:11:50,610 will only bring that one line that has the word 162 00:11:50,610 --> 00:11:54,960 lesson. But if you are in vi mode, you could also 163 00:11:54,960 --> 00:11:59,880 do that same search. And now you could just simply 164 00:12:00,300 --> 00:12:04,020 make sure you are not in insert mode, simply hit 165 00:12:04,350 --> 00:12:12,570 slash and type lesson, lesson, hit enter. And 166 00:12:12,570 --> 00:12:17,340 there, it will bring you to exactly the word that 167 00:12:17,340 --> 00:12:21,810 you want to change perhaps, or you are looking for it. So 168 00:12:21,810 --> 00:12:24,930 these are the few commands that we have about 169 00:12:26,220 --> 00:12:31,650 creating a file using vi. We do have many different 170 00:12:32,070 --> 00:12:36,480 commands or keys that we could use in vi. I have 171 00:12:36,480 --> 00:12:40,050 included in this lecture, I have included all those 172 00:12:40,050 --> 00:12:45,930 keys as a handout. So please 173 00:12:46,230 --> 00:12:50,010 look into those and try every single key, I really 174 00:12:50,010 --> 00:12:53,610 want you to be very familiar with this and become 175 00:12:53,610 --> 00:12:56,010 eventually become an expert. So when you're typing 176 00:12:56,010 --> 00:12:59,460 in something in your text file, you know exactly 177 00:12:59,460 --> 00:13:03,120 which key you're going to work into, or which 178 00:13:03,150 --> 00:13:06,570 exactly key you're going to use. One common 179 00:13:06,570 --> 00:13:10,380 mistake that everybody is going to make is, when 180 00:13:10,380 --> 00:13:13,050 you are in insert mode, you're probably going to 181 00:13:13,050 --> 00:13:17,880 have to get out of it, you can, your typing x, this 182 00:13:17,880 --> 00:13:23,310 and that, but you cannot. So your lifesaver is you 183 00:13:23,310 --> 00:13:29,910 do colon, no first, sorry, first is escape colon 184 00:13:30,030 --> 00:13:35,220 Q. Now this time don't write W. The W will write 185 00:13:35,220 --> 00:13:37,350 your changes the file, you don't want to make 186 00:13:37,350 --> 00:13:41,700 changes. So just to quit, colon quit, bang, hit 187 00:13:41,730 --> 00:13:46,290 enter, verify it. There you go. See now you don't 188 00:13:46,290 --> 00:13:49,050 have that garbage that you were going to write 189 00:13:49,050 --> 00:13:52,290 your file into. So this is another way you get out 190 00:13:52,560 --> 00:13:57,510 and change your file. So please practice, man also 191 00:13:57,510 --> 00:14:00,270 give you different options to use it. There's 192 00:14:00,270 --> 00:14:03,180 again, there's another editor if you like to use 193 00:14:03,180 --> 00:14:09,210 which is vim, which is an advanced version of vi or 194 00:14:09,210 --> 00:14:11,910 you could use that too, which is also available 195 00:14:11,910 --> 00:14:15,180 most of the Linux distribution, whichever you 196 00:14:15,180 --> 00:14:18,930 like, but please do practice. Practice as much as 197 00:14:18,930 --> 00:14:22,560 you can. And I will recommend that you create 198 00:14:22,560 --> 00:14:25,470 files many files in your home directories or 199 00:14:25,470 --> 00:14:28,140 within your subdirectories create your files and 200 00:14:28,140 --> 00:14:31,260 write wherever you want to. Write ABC, or write a 201 00:14:31,260 --> 00:14:34,320 story or whatever and change your text. This is 202 00:14:34,350 --> 00:14:39,210 this is going to allow you to practice vi editor. 203 00:14:39,210 --> 00:14:42,027 [No audio]