00:00:00 - Welcome to Nugget 103.8 where we're learning
00:00:04 - basic file editing with the text editor "vi". This section,
00:00:10 - this nugget, we're going to learn about vi which is
00:00:15 - by far my absolute favorite of all text editors. I'm going to put a smiley face
00:00:20 - here, and by the end of this your geek cred is going to
00:00:23 - go way up because vi is by far the geekiest way. Well,
00:00:28 - one of the geekiest ways anyway to edit text. I'm so glad that this is
00:00:31 - one of the requirements for the CompTIA and LPIC exam because,
00:00:35 - yeah, you're going to be really happy that you learned vi.
00:00:38 - So let's get right down to it. Let's get to a command line.
00:00:43 - Okay, first things first, we are here at the command line and in
00:00:47 - the folder I have a file called file.txt. Now, it looks
00:00:51 - like this. So this is the file that we have. It's just a text file.
00:00:56 - Now, in order to open it with vi, the text editor, you type
00:01:00 - vi and then the name of the file. So vi file.txt
00:01:03 - and it opens it up. Now, vi, if you thought regular expressions
00:01:08 - were confusing, I have to confess. At first vi is going to
00:01:11 - drive you crazy. But it really is worth the effort to learn because
00:01:16 - it's extremely fast and your fingers don't have to go very far
00:01:19 - from the home row to do work. So it's worth the effort. Just
00:01:23 - trust me. So we start here. Vi has two basic modes. There's command
00:01:28 - mode which is what we're in right now. I can't actually edit
00:01:31 - the text here. I can't actually insert text or add text here. If
00:01:35 - I type, it's not going to work. This is the command mode. It means
00:01:38 - that we send commands to vi to manipulate the text. The other
00:01:42 - mode is called insert mode, and you can edit or enter insert
00:01:46 - mode by pressing one of a few keys. I will put us in insert
00:01:52 - mode, and then we can start typing things like The, The Today
00:01:57 - is the first day of the rest of your life, and we've inserted that.
00:02:00 - So we're in insert mode. And then to get back into the command
00:02:04 - mode, you press escape. And then I am in command mode. And I
00:02:09 - know because I can't type any of the, I just can't type words. If
00:02:13 - I start typing words, they're going to be commands. So those
00:02:16 - are the two things. There's command mode and there is insert mode. Now, you
00:02:21 - can also kind of say, there is another mode called execute mode
00:02:25 - which is where you tell it things, but I just consider that
00:02:27 - part of command mode where we are right now, alright.
00:02:30 - So you can't really tell a difference except that we're
00:02:34 - in insert mode now because I pressed I, and if I press escape,
00:02:38 - we are in command mode. Now, to get around the file, probably
00:02:44 - arrow keys will work. If you're in command mode, it's very
00:02:47 - likely that arrow keys will let you go up, down, left, right. But on the LPIC and the
00:02:53 - CompTIA requirements it specifically mentions the old school
00:02:58 - way of getting around vi, and those are using the keys
00:03:03 - h, j, k and l. Now, if that sounds confusing, you just have to trust me
00:03:08 - that it's not that bad. Now, if you look down at your keyboard, you'll
00:03:12 - see the h key
00:03:13 - of those four keys, the h key is on the left, and that's how
00:03:17 - you go left. So if I hit h, it goes to the left until it can no longer
00:03:22 - go left. And then on the other end of those four keys is l.
00:03:26 - Press l, tap, tap, tap, tap, tap, it goes right. Again, this is in
00:03:31 - command mode. It's going right and left using h to
00:03:35 - go left and l to go right.
00:03:39 - Now, the letters may not make sense, but at least if you look at those
00:03:42 - four keys, you can see that the left one goes left and the right
00:03:44 - one goes right. That's not too hard to remember.
00:03:47 - The other two, k and j, are a little bit tougher to remember.
00:03:51 - Now, j is to go down.
00:03:54 - So again, we're going down, and k is to go up.
00:03:58 - So up, j is down. K is up, j is down.
00:04:03 - I gotta be honest, there's not really great ways to remember that.
00:04:06 - I've heard some people say that um, you know,
00:04:09 - k you can remember a kite goes up in the air. So you can use that
00:04:13 - to remember that k means up. The one that means the most
00:04:17 - to me and I remember and it's silly but Jupiter has a lot of
00:04:21 - gravity and it would suck you down really fast. I'm kind of a space
00:04:25 - nut so, you know, my mnemonic probably won't work for you. But
00:04:28 - I remember that j for Jupiter, you would go down very fast if you were
00:04:32 - standing on Jupiter. So maybe you like that. Maybe you prefer to
00:04:36 - remember k is a kite that goes up in the sky. But j and k are
00:04:40 - down and up. And the other two, h and l,
00:04:43 - again they're right next to each other on the keyboard there,
00:04:46 - but they're pretty easy to remember because l is on the right
00:04:48 - and h is on the left. So if you're familiar with that, you know,
00:04:52 - play around with that a little bit. Test yourself if you need to
00:04:54 - so you know how to go up and down, left and right because that's
00:04:57 - how you get around a file as you want to edit it, right. So now
00:05:01 - you know a couple things about vi. You know how to get into
00:05:04 - insert mode which is to press i will start inserting characters
00:05:09 - at the place you're at, Thrafnursday, okay. Press
00:05:15 - escape to get back in the command mode. If you press A, that
00:05:20 - goes into append mode, and this is a subtle difference, right. If
00:05:23 - I pressed i, it started typing right at the, character that I'm in.
00:05:27 - If I press a,
00:05:29 - it goes to the character after. See how it did that? So it's flashing
00:05:33 - on the a in Thrafnursday.
00:05:37 - If you press a to go into
00:05:41 - insert mode, it appends it after. So then it starts typing after the a.
00:05:45 - Not much difference there, but that's, you know, that's the difference
00:05:48 - between using a and i. Then the other option you can use
00:05:51 - is to press o, and what o does;
00:05:55 - it goes into insert mode but on the next line. So it adds a
00:05:58 - line below, and you can start typing there.
00:06:02 - Hello! New line. And then again you press escape, and then you're
00:06:06 - back in command mode, right. So there's a couple of big things
00:06:10 - that we're doing there, and sorry if it's confusing but we're going to
00:06:13 - move on and go into some editing, not just adding some text
00:06:17 - here and there.
00:06:19 - I'm going to guess that you're pretty familiar with common
00:06:21 - editors like Microsoft Word or Notepad or or G-Editor, whatever
00:06:26 - it is that you're used to. Most have terms like copy, cut and paste. Well,
00:06:31 - vi has similar features, but they're called different things.
00:06:35 - I'm going to start with this so we can make our file grow a little
00:06:38 - bit more, alright. So let's use our, we're in the, press escape.
00:06:43 - Make sure, yes, we're in command mode. So let's go, let's go up, k
00:06:48 - for fly my kite, and let's put this whole line into the,
00:06:55 - the clipboard which is actually called the register, okay.
00:06:58 - And instead of copying it, it's called yanking it. So it sounds
00:07:03 - a little bit bizarre. But you yank a line of text into the
00:07:06 - register. So you press yy.
00:07:10 - You don't see any output, but pressing yy is going to put
00:07:15 - that into the register. And then let's say we press j to go
00:07:20 - down a line and we want to paste or put, it's actually
00:07:26 - put in vi terms, after this line. You press p and it puts
00:07:31 - it right below there. If you want to do it again, you keep pressing
00:07:34 - it. It puts it below the current line. Boom, boom, boom. So see that.
00:07:39 - We have, we, we put a whole bunch of things from the clipboard
00:07:42 - which isn't called a clipboard. Do you remember what it's called? The register,
00:07:45 - right, exactly, okay. So now we're still in command mode, right.
00:07:49 - So I can still press k and go up. It's not going to insert text because
00:07:52 - this is all done in the command, in the command mode, not in the insert mode.
00:07:57 - So here I am and let's say we want to cut out
00:08:03 - this line, okay. Now, we use the d command and you use dd,
00:08:10 - twice, and it cuts out or deletes that whole line. Now, just
00:08:15 - like with yanking, it's actually in the register now, okay.
00:08:18 - So let's go down here and I decided that I want to put that
00:08:25 - line in between, right, after this line. We're going to put it
00:08:28 - after that line, okay. Again, remember put puts it in the line
00:08:32 - after you, what you're currently on. It inserts it in a line there, right. So
00:08:35 - we'll put a couple of those in there. Remember we cut it or we deleted
00:08:40 - that whole line from the top. Now, you can delete more than or
00:08:43 - you can, you don't have to delete a whole line at a time. Let's
00:08:47 - say, let's go back up here, and I just want to get rid of this
00:08:51 - word. Then instead of dd, I would press dw for word, and
00:08:58 - it put that in, it cut out or deleted that word from that, from that
00:09:04 - line and now it's in the register. So if I want to,
00:09:08 - oh, I want to put it on a line of its own because that's what put
00:09:11 - does, I press p,
00:09:13 - and it put it there because it wasn't a new line. It put it after
00:09:17 - the character that I was at instead of after the line because it's,
00:09:21 - we'll do it a couple more times. See, it's right on that o character.
00:09:24 - It's going to put it at the next word over. Hello, hello, hello,
00:09:28 - hello. See how it's different? Now, let's say we wanted to
00:09:34 - put it somewhere else. Let's go down here
00:09:39 - and right there. So I want to put it right after that. If I put,
00:09:44 - it's going to put a hello right after that character. Now, if
00:09:47 - I delete this whole line, dd,
00:09:52 - and down here, if I put it, now I do, I deleted an entire
00:09:57 - line. So now it's going to put an entire line after this line.
00:10:02 - See. And really it did the same thing, but you have to remember
00:10:06 - when it cuts out a whole line, that includes the new line
00:10:10 - character. So it cut out the whole line. So it put a whole new line
00:10:13 - in. It cut out a whole word here. So put in a whole word right
00:10:16 - after it. So that's the difference with cutting a line and cutting
00:10:19 - a word. You can also do just one letter.
00:10:23 - Again, this is all still in the command mode. I can't just type
00:10:26 - words in here. I'm just using the keyboard shortcuts in the
00:10:30 - command mode. So let's go up. I just want to cut out this T.
00:10:35 - So d for delete and then l for just one letter.
00:10:40 - And I want to put that T somewhere else. I want to put it right
00:10:45 - after this T. So then I would put it and it would put that T right
00:10:50 - after the character I was at.
00:10:52 - See, so it puts it right after the p for put. So we have a lot, we're
00:10:57 - learning a lot here I realize that. But what we've, we've shown how
00:11:01 - to do yanking.
00:11:03 - So we come here. We can yank this whole line, yy.
00:11:08 - And it didn't delete it. It just put it in the register or
00:11:11 - like in the clipboard. And then let's go down a little bit
00:11:14 - and insert it right here. We're going to press p and it's going
00:11:18 - to put it in the line after this.
00:11:20 - See. Now, but you say it doesn't look different. Well, see, it's the same
00:11:24 - thing that we yanked before. So let's go back up just to be clear.
00:11:29 - We'll yank this line, yy, that line is yanked. We'll go down. Press p
00:11:35 - to put it in, in there, put it in the new line, alright.
00:11:38 - And let's say I just want to delete this exclamation.
00:11:42 - That's considered a word so dw. It deleted that whole word
00:11:46 - which is really just an exclamation point.
00:11:49 - And let's see, press l to go over this way, and after this
00:11:53 - character I want to put that exclamation. So I go to the character
00:11:56 - that I want to put it after and then press p to put it in there,
00:12:00 - put it in, alright. It's kind of confusing, but when you get used to it
00:12:05 - it's really fast. And it's important to know although you need to
00:12:09 - know the h, j, k, l keys, a lot of times the arrow keys work
00:12:13 - just fine in command mode, okay. So you can be lazy and do that,
00:12:17 - but on the test you're going to need to know the h, j, k,
00:12:20 - l for left, down, up, and right.
00:12:25 - Now that we've created this big messy file full of a whole bunch of
00:12:29 - similar things, it's a good time to show you how to search within a
00:12:34 - file. Now, we're in command mode again. So again, I'm not entering text.
00:12:39 - You'll notice that a lot of the stuff you do in vi is done
00:12:42 - where you don't actually type in text. It's a lot of yanking and putting
00:12:46 - and deleting and, and that kind of thing. So we're here, if
00:12:50 - you want to search for something and this is like in a configuration
00:12:53 - file or something, you may want to search for a specific term,
00:12:56 - you press the forward slash and you see how it, how it showed up down
00:13:00 - here? So you press forward slash and then what you want to search
00:13:04 - for. So I want to search for after. Now, there can be spaces in this
00:13:08 - too so after that.
00:13:11 - And I pressed enter and it found the first occurrence after
00:13:15 - where my cursor was. So it started at my cursor and then
00:13:17 - looked for the first occurrence of that. And now to repeat the
00:13:20 - search, you can either press n to just one keystroke, keep
00:13:26 - finding it.
00:13:29 - Or if you want to go backwards like you want to search up,
00:13:33 - capital N and it goes backwards.
00:13:39 - So I'm just going capital N and it repeats the search going
00:13:42 - backwards or up and lowercase n
00:13:45 - is the next occurrence or, you know, to search again for the same term.
00:13:50 - And now, since I'm talking about backwards, let's say you want
00:13:54 - to start a search backwards. Well, you press instead of a forward
00:13:58 - slash, you do a question mark. See, I typed question mark. It appears
00:14:03 - down here and now I want to search for the first occurrence
00:14:07 - of ello.
00:14:12 - Press enter. It found it right there. So now if I press n,
00:14:16 - it's going to find it right there because remember this is
00:14:18 - a backward search. So it's going backwards now when I press
00:14:22 - lowercase n.
00:14:23 - So it can be a little confusing after, remember, if you search
00:14:26 - forwards or backwards because n and capital N will go forwards
00:14:30 - and backwards according to whatever search you did, alright.
00:14:35 - And if you look, see, I'm pressing n to go backwards. If I press
00:14:38 - capital N, it kind of reminded me, okay, we'll do a forward search
00:14:42 - for ello if you do capital N, capital N, capital N. Lowercase
00:14:46 - n, lowercase n, lowercase n, it's the backward search
00:14:50 - for ello, alright. And then both of these searches, let's say
00:14:53 - I keep going, I keep going, oh, what do you think is going to happen
00:14:57 - next? Ah, it starts right at the bottom of the file and it continues
00:15:01 - searching, but it does tell you, see. Search hit the top so it
00:15:05 - continues at the bottom, and it just loops around like that, alright.
00:15:10 - So search n, n, n, go backwards. The same thing if we're going searching
00:15:14 - down, it will wrap and go to the top,
00:15:18 - alright. So that's searching. It's, it's pretty easy and it works
00:15:22 - really, really well. So now we've done all this beautiful work
00:15:26 - to this file, right. It looks so beautiful that we want to save it.
00:15:29 - Well, that's pretty easy to do. Again, we're in command mode now.
00:15:33 - So see, k goes up, j goes down. What you need to do when you're
00:15:37 - working with the files is you start with a colon. So press colon
00:15:42 - and some people call this execute mode, but this is just part
00:15:44 - of the command mode and this is a way that you save files or,
00:15:48 - or quit the program. And there's a couple ways to do that. Now,
00:15:53 - let's say that we wanted to start over editing this file from scratch.
00:15:58 - :e means start over like revert to save type thing.
00:16:02 - Now, if you've made changes and you tried to do just the :e,
00:16:06 - it will complain. It will say, whoa, if you do that, you're totally going
00:16:10 - to lose all the changes you made. And we all wanted to say Thrafnursday
00:16:13 - so it warned you. But let's say we really, really wanted
00:16:18 - to get rid of all of our changes, right. So you would type :e.
00:16:23 - An exclamation point means, yes, I know that I've made changes
00:16:28 - and I'll lose any changes I've made. It's like override for the
00:16:31 - safety precautions. So if we do that, it brings us back to the
00:16:35 - file that we started with, alright. So we're done with making
00:16:39 - all of our silly changes. All we're going to do now go down a couple
00:16:42 - things. I press o so that I can enter insert mode underneath here
00:16:47 - and that
00:16:49 - makes today June because, I don't know. That doesn't make any
00:16:54 - sense, but that's okay. Now, remember, I'm in insert mode now so I can't
00:16:58 - save the file. What do I do to get out? I press escape and that
00:17:01 - brings me back into the command mode. So then I press colon.
00:17:05 - W writes the file. Now, if you do colon w, it writes to the
00:17:10 - file that you started with. You know, I mean whatever it was.
00:17:13 - You typed vi file.txt. So if we type w, it's
00:17:18 - written to that file. If you wanted to name it something different,
00:17:21 - you could type :w and then newfile.txt. And
00:17:27 - then it writes it to a file called the newfile.txt, see,
00:17:31 - so that we can write it and, that's like save us in the world of
00:17:34 - Microsoft Word or something. If you want to quit, you type
00:17:38 - :q. But before we do that, let's say that we deleted
00:17:44 - this line and then we wanted wanted to quit.
00:17:48 - So :q, it's gonna say, whoa, again just like when we tried
00:17:52 - to revert to the original it says, you've made changes and you
00:17:55 - haven't saved them. But if we really don't want to save them like
00:17:58 - we x'ed, and we messed it up, if you type :q!
00:18:03 - the same thing happens. It goes right to, you know,
00:18:08 - it quits just right away. It doesn't save the file at all.
00:18:11 - So let's go back into the file. Now, this is the file that we had. And there's
00:18:16 - another way, instead of the colon, there, it's kind of a shortcut.
00:18:19 - Now, normally, what you do if you, if you make changes to a file
00:18:22 - that you like, I like that and I'll insert my name
00:18:27 - is Bob. (not really)
00:18:30 - And then I press escape to get back in the command mode. Normally.
00:18:34 - what you would type is :wq because you can string
00:18:37 - them together like that. So what this is going to do it's going
00:18:39 - to write to the file and then it's going to quit. So you press
00:18:43 - that, it writes to the file and and it quits, alright. So see now,
00:18:48 - ls, see now we have two files there, right.
00:18:53 - The first file that we have, we have the one that
00:18:56 - we, we just edited and then we have the one that we saved
00:19:02 - when we did that, right, and it said that there's a new file, right. So anyway,
00:19:08 - so we're back into our file here. So what we typed, that's normally
00:19:12 - you type again wq if you're going to save something, right,
00:19:16 - and then quit. There's a shortcut so you don't have to type colon at all.
00:19:20 - If you do capital Z, capital Z, it writes and quits. It's a shortcut.
00:19:25 - I never use that shortcut personally. I just like to, my
00:19:28 - fingers are just used to typing :wq. But ZZ
00:19:33 - is actually one of the items listed on the LPIC exam. So
00:19:37 - you should know that. Either capital Z, capital Z in command
00:19:40 - mode or :wq will write the file and quit right
00:19:45 - away. Now, one last thing I want to show you is how you can use,
00:19:51 - now again I'm in command mode, right. So kk goes up. How to change,
00:19:57 - use the c command. Now, it's a little bit tricky to get the
00:20:00 - hang of, but you can, let's say, for example, we want to c,
00:20:04 - change a word, so w. Now, see how this word now has that dollar sign
00:20:10 - after it. What it's going to do, anything I type now is going to
00:20:14 - replace the word today. So it could just be the word the,
00:20:19 - and if I press escape, see, it replaced the word today with the, alright.
00:20:24 - So, but let's say we wanted to do something like, go to the beginning here, change
00:20:29 - this word to, Today is really cool, and then press escape.
00:20:37 - It's replaced that, it's changed that word today to Today is really
00:20:40 - cool. So what it does basically is it takes that word. Now, you can
00:20:44 - do the same thing with just a letter. Let's say I want to change
00:20:48 - this letter
00:20:50 - with yyy, and then press escape, alright. So you can do it just to
00:20:55 - one letter or to the whole word and change is using c.
00:21:00 - I don't use it personally very often, but if you want to replace
00:21:04 - this weird word that I made now, we could say cw,
00:21:09 - Today is really cloud, cloudly, cloudy, press escape. And then
00:21:16 - it's replaced it with cloudy, alright. So that's how change
00:21:20 - works. Now, let's look over all of our different vi things,
00:21:23 - so maybe we can wrap our brains around it. Now, I know we went over a
00:21:27 - lot of stuff really quickly. So you may want to go over this
00:21:30 - video a couple times and practice playing with a few text files
00:21:33 - on your own. But basically, let's go through. The forward slash is
00:21:36 - how you search for something in the forward direction going
00:21:39 - from top to bottom of a file. If you want to search backwards, you use
00:21:42 - question mark. Again, this is in command mode. When you search
00:21:46 - the first time, a lowercase n will continue that search in
00:21:48 - the same direction and a capital N, again, in command mode, will
00:21:54 - continue the search in the reverse direction, so the reverse
00:21:57 - of how you started. These keys, h, j, k, and l are right next
00:22:02 - to each other on a keyboard and they correspond to left, down,
00:22:05 - up and right when you're in, I'll say it again, command mode, alright.
00:22:09 - Now, to enter insert mode, there's a few different ways that
00:22:12 - you can do that. If you press i, it's going to enter insert mode
00:22:16 - at the current character that you're at. If you press a when
00:22:20 - you're in command mode, you're going to enter insert mode at
00:22:23 - the character after the one that you're on right now. So like
00:22:27 - if you're at the last letter of a word and you press a, you're going to
00:22:30 - start inserting at the character after that. And then o will
00:22:34 - enter insert mode and it inserts a line underneath the current
00:22:37 - line that you're in and you start entering right there. So if you're
00:22:40 - in the middle of a file and you want to enter a new line right below it,
00:22:42 - you press o and then you're in insert mode on the line right
00:22:46 - below it. It turns into a blank line, alright. And then to get back
00:22:49 - into command mode, you press the escape key. So if you're in
00:22:52 - insert mode, you press escape to get back into command mode
00:22:56 - where all of these other things work, alright. So that's cheat sheet
00:23:00 - page one.
00:23:02 - And here is cheat sheet part 2 where we talk about how you can modify
00:23:07 - some things like change. We just learned that last, c and then you
00:23:11 - can either enter l
00:23:13 - in, in there for changing one letter or w, cw
00:23:18 - will change the current word like we just showed you, okay. The same thing
00:23:22 - with d. Now, d stands for delete. But if you're used to Windows
00:23:26 - or, you know, the standard word processing terminology, you might want
00:23:30 - to think of it as cutting because it puts whatever you delete
00:23:33 - or cut into the register so that you can put it somewhere else later.
00:23:37 - So you can either delete a word or you can delete a line,
00:23:41 - delete line, and you can do that or, or a letter, I'm sorry,
00:23:46 - l is for letter. Or you can delete the entire word. Now,
00:23:50 - with yank, you can do a letter at a time using l or you can do
00:23:55 - an entire word at a time. And again, yank is much like copy
00:23:59 - would be in a word processor. It leaves it there, but it puts
00:24:02 - it in the register.
00:24:04 - And then down here, yy is to yank an entire line, dd is
00:24:08 - to delete an entire line. Again, both of these put it in the register.
00:24:13 - And then to put things back or paste if you, if you prefer
00:24:16 - to think of this as paste, the p will put the contents of
00:24:20 - the register at your current place. If it's an entire line, it
00:24:23 - will put it at the line below you. If it's a letter, it will
00:24:26 - put it at the character next to you. If it's a word, it will put it at the
00:24:29 - character next to you. So depending on what it is you have in
00:24:32 - the register or in the clipboard, if you want to think of
00:24:34 - it that way, it depends on how put will put it on the screen, alright.
00:24:38 - And if you completely mess up your file with all the silliness
00:24:41 - that you've been doing :e! will revert to
00:24:46 - the original file, okay. Even though you've made changes, it will bring you right back
00:24:49 - to the original file that you started with. One more page and
00:24:52 - then we're done with our cheat sheet here. Now, let's say
00:24:55 - that you've made changes and you want to write and edit or
00:24:58 - write and exit, there's two ways you can do that. The more common
00:25:02 - way one or the one that I see most often is :w for write, q for
00:25:06 - quit. You can put them right together on the line like that or in
00:25:10 - the command mode you just the capital Z, capital Z will
00:25:13 - write and quit. Now, if you want to write and even if it's a read
00:25:18 - only file, if you put the exclamation after the w, it's
00:25:21 - going to write even if it's a read only file, alright. So that's kind
00:25:25 - of dangerous. You have to be careful if you're going to write to
00:25:27 - something like that. And then :q! is quit
00:25:31 - without saving which if you mess up a file and you just want to get out of
00:25:35 - there, you can quit, exclamation point, and that will get you
00:25:38 - out of it. Like I said, I know this nugget has been really, really
00:25:43 - chock-full of a lot of confusing information. The best way though
00:25:46 - is to just try playing with vi. I promise it's worth it, and in the
00:25:51 - end you'll have a nice smiley face and apparently red hair.
00:25:54 - I hope this has been informative, and I'd like to thank you for watching.