From the course: LPI Linux Essentials (010-160) Cert Prep
Using vi and nano - Linux Tutorial
From the course: LPI Linux Essentials (010-160) Cert Prep
Using vi and nano
- [Male] There are many text-based editors, within Linux, and these are helpful when you need to edit a configuration file, or just access a quick file, and make some changes. Now, the first one I'm going to show you is vi, and the second one is nano. Now, I'm going to warn you, vi is a little bit more complicated to use than nano, but stick with me, and I'm going to show it to you. The reason is, a lot of system administrators really swear by vi, they love it, it's a relic of the old 1970s, and 1980s, but some of the old timers just really, really love vi. It's kind of the sign of being a true Linux guy, is that you love vi. Personally, I'm not really a big fan of vi, I much prefer pico, or nano, but I'm going to show it to you because the exam wants you to know it, and a lot of people that you're going to experience in the field, will say that if you don't know vi, you just don't know Linux, so we're going to go into it. All right, so the first one we're going to use is vi, and if we're going to use vi, we need to have a file to use, so I'm just going to call this test1, and by doing this, I'm going to open up vi, and open up a file, now known as test1, and this file is going to be blank. So when I do this, you're going to see that we have all these blue tildes going down the left side of the screen. This signifies that those lines have nothing on them, in fact, the whole file is just one line, with one character, this blank blinking cursor because it was an empty file, nothing has been done to it yet, so when we open up a file, by default, we are in edit mode. For us to be able to type in information, we have to go into insert mode, and we do that by pressing the I key. So in this case, I'm going to press I, and you see, no I came on the screen, that's because it just put me into insert mode, and now I can start typing, this is a test file, this is only one line. That's fine, you can enter whatever you want here. Now if I wanted to enter more data, I can hit enter and I can say, this is the second line, and I'll say this is the third line. That's enough data for us to play with right now. Now if I wanted to go up to the second line, and be able to get to it, I actually can't use my arrow keys, that won't work. Instead, I have to hit the Escape key, which goes back to edit mode. By doing that, I can now arrow my way around, to where I want to be able to touch things, and so if I wanted to be able to change where it says second, and maybe put that as a two, nd, instead, I can then go ahead, and press the X key to delete, and so as I hit X, it's deleting them one at a time. Now, if I wanted to type in the two, nd, I have to go back to insert mode, so I press the I key, and now I can put two, nd, and I'm back there. Now again, if I wanted to move around again, I'd have to hit the Escape key, and now I can move around with my cursor, or I can even use some shortcuts. There's a couple of shortcuts, for instance, if I use the Carrot key, which is the Shift, and the six, that will bring me to the beginning of a line. Now, if I wanted to move forward a certain number of words, I can actually do that, by typing in the number and the W. So if I wanted to go to the third word of this line, I would type 3W, and you can see, I went past the three words, and now I'm at the fourth word, ready to start typing because I moved forward three words, this is the, those were the three words. Now if I wanted to move back, I can do that by typing a number and B. So, let's say I wanted to go back two words, I would do 2B, and that takes me back from the, and back to is, and so that's why a lot of system administrators like it because they can move around pretty quickly this way. For example, if you wanted to go to the first line of the file, you could just type 1G, if you wanted to go to the third line, you type 3G, if you wanted to go to the 50th line, you'd do 50G, and so they can jump around really quickly. Another thing you can do when you're in this edit mode, is you can issue commands. So, for instance, if I wanted to show line numbers, if this was a configuration file, for instance, I can do that by typing in a colon, which gives me the command here at the bottom, and then set nu, which stands for set numbers. If I hit enter, you'll now see that I can see what line numbers there are, and be able to identify things a little bit easier. Again, if you want to know more about vi, you can use the man pages, or there's a lot of great cheat sheets for vi, that are one page PDFs you can download online, just search vi cheat sheet, and you'll find them, they're everywhere. Now that's the idea of using vi. We can take it a little bit further if we wanted to, and undo actions, which would be something like a U, if you type in U, that will undo an action, which in this case took away my 2nd that I added in. If I want to insert that back, I hit the I, and type in my 2nd, and other things of that nature, but again, I'm going to leave that to you, to play with a little bit more, because again vi is pretty complicated because there's no mouse, and it has all these weird commands you have to use, so instead I really want to focus on a program called nano instead. But before we do that, we have to save this file. Now, how do we do that? Well if we hit escape, we are back into our edit mode, we can then hit the colon key, which brings up our command issuing, and here I'm going to do W, and hit enter, that stands for write, which will save my changes, but it won't exit the file, so there we go I wrote that three lines, and those 90 characters to disk. Now, if I wanted to quit and save, I can do that by hitting the colon, then WQ, which stands for write, and quit, or if I wanted to quit without saving my changes, I can do, Q!, which stands for discard all my changes, since I saved last, and then exit. In my case, I do want to save, so I'm going to go ahead and backspace out here, and do WQ, so I'm going to write, and quit, and that'll bring me back to the command line. Now, if I want to see the contents of that file, we can again use cat, we can type in test1, and you'll see, I have those three lines to the screen. All right, so you can see how complicated vi was. Let me show you a much easier way to edit your configuration files, and this is using a program called nano. So you just type in nano, and the name of the file you want to open, and up it shows up. Now, you can see already, this looks more like a command program. You can see that there's some information here, for instance, the name of the program in the top left, this is nano 2.9.3. The name of the file in the top center, test1. The contents of the file, and then at the bottom, it shows me some information, and it even shows me some commands, all of those things like the Carrot + X, Carrot + G, Carrot + O, that you see at the bottom, that means Control, so if I wanted to exit out of here, I can just hit Control + X. If I want to get help, I hit Control + G, and there is some help, just like a man file, and I can then go next page, Control + V, and read through as I need to, to find the information. If I want to close that, again Control + X. Now you're already in edit mode here, you can already go in and change files, and delete files. You can arrow around, just like a regular word processor, so instead of a, maybe I want to backspace that, and hit this is the test file, right, and you can do a lot of this information here, just like in nano, you can go to a certain line number. So if you do the Control, and then the underscore character, it'll say, enter the line number, or the column number, so the line is how many rows up and down, maybe I want to be on the third row, and the column is how many characters over, from left to right, and it goes from zero, over to 80. So you could do that by doing, maybe 3, 5, which should get me there to about the I in is, on the third line, and you can see right there, it put me into the space, right, 'cause this, which is the fifth character is that space, right before the I in is. So this is the idea of being able to move around here, and again this is much easier to use, than something like vi because you can simply go around, and be able to access things. The other thing is you can actual cut text, and un-cut text, which means you can actually bring things around, and copy and paste as you go. There's a lot of nice features like that inside of nano. For example, nano even has a spell checker there, as you can see with Control + T, and for example, if you want to take away your justification, and make it all so that there's no single lines, everything goes in one big line together, well you can do that by hitting Control + J, if you want to undo that, you hit Control + U, and it goes back to normal. So you can see there's a lot more features here, inside of nano, than using something like vi. So the reason I want to bring this up, is because as you're trying to make test files, so you can play with things, as you want to edit configuration files, nano is a great tool for that. Now, when you're done using this file, and you want to save your changes, for example, let's go up here, and just change the 2nd, back to a written out second, we can save that by using the write out command, that's Control + O. It'll ask you where do you want to save this file, if you want to use the same name, just hit enter, and then it's going to write those three lines back to disk for you, and at that point, you can hit Control + X, exit out of here, get back to your command line, and go back to doing whatever else it is, you need to do on your Linux system.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
(Locked)
Text files and text editors3m 55s
-
Using vi and nano8m 57s
-
(Locked)
Beginning a shell script2m 34s
-
(Locked)
Commands4m 38s
-
(Locked)
Arguments2m 35s
-
(Locked)
Variables3m 24s
-
(Locked)
Conditional expressions4m 39s
-
(Locked)
Loops and functions3m 48s
-
(Locked)
Exit value2m 17s
-
(Locked)
Turning commands into a script11m 39s
-
(Locked)
-
-
-
-
-