 Do you spend a lot of time in a text editor, or are you a writer, or a scripter, or a programmer, or whatever it happens to be? If you spend a lot of time at a text editor typing, you owe it to yourself to learn VIM. And VIM is not that hard. Today I'm going to cover the absolute basics as far as the VIM key bindings and the basic VIM motions. So to get started learning VIM, the very first thing you need to do is make sure VIM is installed on your system. So I'm on an arch-based Linux distribution. So I could install VIM using Pacman, sudo pacman dash capital S VIM. Or if you wanted to, you could install Neo VIM, which is a more modern version of VIM. Or if you happen to be an Emacs user, you could just use Emacs and just enable Evil mode inside Emacs. And it's a VIM emulation that is very good as far as like 99.9% of everything that is in VIM is also in Emacs Evil mode. So you could also do this in Emacs as well. Once you have VIM or Neo VIM installed, you can open a terminal and run the command VIM tutor. And this is a little help guide. This is basically your very first VIM lesson. The VIM tutor is this document that you could go through in about 20 to 30 minutes and it's going to cover all of your basic VIM motion commands, basic key bindings, basic editing commands. So it's a pretty straightforward document to go through. You can just read it line by line. It's going to tell you some commands to enter, just hit those commands. But honestly, this VIM tutor document, it's kind of plain looking for one thing. It's not a programming document. It's not written in a programming language. There's no syntax highlighting. So for me, I think I'm going to play in a different file. I think what I'm going to do for purposes of this video, we're just going to play with my bash RC. So I'm going to type VIM dot bash RC here in my home directory. And this is my bash config. Now the very first thing you need to learn when you're using VIM is how to open a document with VIM and how to quit out of VIM. Well, opening a document in VIM is easy. When you're in the terminal, type VIM and then the path to some document that you want to edit. Or if you're creating a new document, the path to a new document you want to create, right? That's easy. But now that you've got this document open, how do you quit? Well, you type colon q to quit. So colon is the command mode within VIM. Anytime you type colon, it expects you to then give it a command such as q for quit. You could actually type the full word quit, but q is a shorthand for quit. If I do that, it quits me out of VIM. If I up arrow here in the terminal and go back to VIM dot bash RC, I'm back in this document. Now if you want to save changes to a document, say I've edited a few lines of this document. Well, to save those changes, you want to do colon w for right. Again, you could do colon in the full word right, but colon w is the shorthand for right. If you want to save and quit, you could do a colon wq for right and then quit. So if I do colon wq, you know, I had made no changes, but had I made some changes, that would have worked. Now say I make some changes and then try to do a colon q without saving. Well, I'm going to get a warning that I haven't saved the document. But if you want to quit without saving the document, say you made some changes, but then you changed your mind. It's like, ah, I don't want those changes saved. Let me quit without saving. Well you can do a colon q exclamation point and that will quit without saving any changes. So let me get back into my bash RC. Let's go over some of the basics with VIM. The very first thing you need to know is that VIM is a modal text editor. By modal, I mean it has modes. By default, when you open a document in VIM, it is in normal mode. And the reason it's called normal mode is this is the mode you're going to be in most of the time. Normal mode allows you to move around the document with your motion keys. The basic motion keys are H, J, K, L. So H is lift, J is down. So if I start typing J, I go down, K is up, L is right. And just to verify, I'll also do H to go back, lift. So H, J, K, L, again that is lift down, upright. Now your arrow keys do also work when you're in normal mode. So you could use the arrow keys to just, you know, move up and down and left and right. That works just fine. Too many VIM purists will tell you not to use your arrow keys when you're using VIM because it kind of, you know, confuses you a little bit, which it does on a standard keyboard because H, J, K, L are those four arrow motion keys, right? But they're in a straight line across. On your standard 110 key keyboard, your arrow keys are typically in like a T-shaped or upside down T, right? So you have the up arrow key above the down arrow key and then left and right out to the side. You know, it's a very different kind of layout on the keyboard. Your arrow keys compared to H, J, K, L and mentally it will confuse you if you try to go between the two. Luckily, I don't have this problem because I'm using these split keyboards from ZSA for the most part on all of my machines and I have these set up to where my arrow keys are set up with left, down, up, right, all in a straight line. So it's very easy for me to either use H, J, K, L or I just move my hand down two lines and the arrow keys are also set up in the same order as H, J, K, L. So mentally it's, I don't have to learn two different sets of arrow motions, right? They're very similar. But if you're learning them on a standard keyboard, try to avoid using the arrow keys for a while because, again, you really need to get used to H, J, K, L. So as I said, normal mode, you're typically in normal mode. When you're in normal mode, you're not editing text. Now if you want to get into insert mode, which is the mode where you can then start typing on a keyboard and add text, hit I for insert mode and now you could start typing something. And to get back into normal mode, all you have to do is hit escape on the keyboard and now you're back in normal mode. So H, J, K, L will work as motion keys, right? Where if you were in insert mode, H, J, K, L would actually type the characters H, J, K, L on the point of the cursor. Now I didn't actually want to add that new line to my config because this is my actual bash RC, my bash config file. So I'm going to type you on the keyboard to undo. So anytime you're in normal mode, you to undo. If I do control R, that is a redo and you will use these constantly when you're making changes in a document using them. So remember, you to undo control R to redo. So let me you a couple of times to make sure that I haven't made any changes here. I'm back at the oldest change. You can see here at the bottom echo area already at the oldest change. So we have normal mode, which you begin in. You have I for insert mode, escape gets you back into normal mode. There's also visual mode. And there's a couple of different visual modes you need to know. You need to know shift V for visual line mode. Now that I'm in visual line mode when I go down with a J, I'm selecting the entire line. You can see I'm highlighting these entire lines to copy or delete or whatever it is I'm doing to these lines, manipulating the text of these lines in some way. Let me escape to get out of that. So that was visual line mode. There's also control V, which is visual block mode. Now this, instead of selecting the whole line, it just selects whatever I select with the cursor point. So if I go over a little bit. So if I actually use L to go over right, you can see I'm selecting a block of text. I'm not selecting the whole lines. I'm just selecting that highlighted area. So those are the four basic modes. You need to know normal mode, insert mode. And then you have the visual line mode, visual block mode. And then, of course, colon is its own mode as well as command mode, where you enter commands such as colon, q to quit colon, w to write colon, w, q for write and quit. While HJKL are nice for moving around by character, most of the time you're wanting to move around in much bigger chunks than single characters. Well, there's a couple of things you can do to quickly move around a document. The first thing you can do is you can always do gg to get to the top of the document, right? You could also do capital G, shift G, to get to the bottom of the document, the last line of the document. You see, I'm on line 256. If I want to go back to line one, gg, if I want to go back to the last line 256, capital G, and if I have a line number in mind, an exact line number I want to go to, for example, 10 capital G, that takes me to line 10. If I wanted to go to line 50, capital G takes me to line 50. You can also use the HJKL motions in conjunction with numbers to move, for example, up and down by a specific number. So for example, maybe I want to go up 10 lines. I could do 10k and I just moved up 10 lines, right? If I do 10j, I move back down 10 lines to where I was previously. And of course, I could also do something like 20l to move over 20 characters to the right. 20h would move me back to the left 20 characters. So let me gg to get to the top of the document. Let's move down to this line here. Now there's a couple of other very nifty motions you'll use all the time. You'll use the zero and the dollar sign on the keyboard in normal mode. What dollar sign is, dollar sign takes you to the end of the line, right? Dollar sign, end of the line, zero takes you to the beginning of the line. Dollar sign takes you to the end, zero takes you back to the beginning. Now zero and dollar sign are useful for the most part, but sometimes you'll have weird lines. For example, I'm going to add some blank characters to the beginning and the end of this line. Now if I do zero to go to the beginning of the line, that's great. But now I'm on that blank space and maybe every line starts with, I don't know, two spaces, four spaces. You'll often encounter this in certain programming documents and you don't want to go to that space. You really wanted to go to where the actual line really starts, the first non-blank character. Well, you can do this as well. What you can do instead of doing zero to go to that first character, use the carrot symbol to go to the first non-blank character. And similarly, instead of doing the dollar sign to go to the very end of the line, if it's a blank character, what you could do is you could do g underscore to go to the last non-blank character. So in summary, remember, zero is the beginning of the line, dollar signs, the end of the line. Carrot symbol is the first non-blank character. G underscore is the last non-blank character. Give me you to undo those changes. Another thing you can do to move around quickly in Vim is you can use the braces. You can use the opening brace and the closing brace. The closing brace moves you to the next paragraph. The opening brace moves you to the previous paragraph. So if I do the closing brace, we will go down by paragraph. If I do the opening brace, we will go back up by paragraph. If I want to move down by a half page, I can do control D to move down by a half page. You can see my cursor is always in the same spot. I'm moving down by a half page worth of lines there. If I want to go back up by a half page, I can do control U. So control D for down, control U for up. That'll... and if I wanted to go all the way to the top gg or all the way to the bottom, remember capital G, shift G, right? Gg, first line, capital G, last line. Sometimes you want to search for a specific string of characters in a document. You can do that with the slash. Just hit slash and now type the string you want to search for. I want to search for export. And you can see it finds export in several places here. And if I hit enter, it goes to the first instance. And then if I hit in for next, it will go to the next instance of export. In again, will take me to the next export. If I do capital N, it will take me backwards to the previous export. Capital N to the previous export. Now if you know you're going to search for a string backwards, instead of doing the slash name of string, right? You could also do a dollar sign name of string. So let's do dollar sign export. And now what this does when I type in for next, it just begins us going backwards. And if I do capital N, it actually goes reverse. So you have two different ways to search. The slash is the normal way. The dollar sign name of string is the backwards way. Another thing you can do is you can move forwards and backwards by word. You will often do this instead of just using hjkl. Remember h and l move left and right by one character. But if you just want to move forward and backwards by word, you can do w for word. So if I just type w, I move forward to the first character of each word. Now w also includes special characters such as the period and the semicolon, you know, all of those kind of punctuation marks. If I do b for back, I move back by word. I go to the first character of the word, except we're going in reverse. And again, it's going to include special characters such as that semicolon and the period. So w forward, b for back. If you want to go forward and backwards by word, but instead of the first character of the word, you want to be on the last character of the word, you can use e for end of word. So e will go forward by end of word. And g e will go backwards by the last character of each word. And just like w and b included the special characters, e and g e also include the special characters such as the punctuation marks. There are versions of these same commands that do ignore the punctuation marks. So instead of using w, b, e, g e, what you could do is you could use a capitalized version of those. For example, capital W, shift w, will take me to the next word. But when I get to config period here, and I do capital W, it skips the period, right? We're going to ignore those special punctuation marks. So no longer would I stop on semicolon here with the next w, it is skipped to the beginning of the next word just in this case. And capital B does the same thing in reverse. It's going to ignore that semicolon, right? We're going to ignore the period with it as well. Capital E does the same thing. We go to the end of the word, the end of the word is the period on config period, right? So before it would, the regular E, if I hit it on config, it goes to G. And then the next E goes to the period, right? So G, it sees G and period as the end of two separate words where capital E, I hit zero on the keyboard and do capital E, you know, it's going to see the period as the end of the word config period. And to do G, E in reverse and have it also act the same way what you could do, let me go forward a word with capital E. And now let me do G, capital E to go back. And you see when it goes back, it treats the period as the end of this word. If I do G, capital E again, the next time it jumps back to the last character of this word here, bash. So W, B, E, G, E, and then you have capital W, capital B, capital E, G, capital E, depending on whether you want to ignore the special punctuation marks or not. For those of you that are programmers, one of the really cool things Vim does is it really helps you manage keeping track of your parentheses, braces, and brackets. Because if you want to quickly navigate in between a pair of braces or a pair of brackets or a pair of parentheses, all you need to do is I'm on the opening bracket, in this case on this line right here. If I'm in normal mode, all I have to do is hit the percent key and I go to the closing bracket. So basically parentheses will always take you to the matching pair for that parentheses, brace, or bracket. If I hit parentheses again, I go back to the opening bracket. So I can quickly switch between the opening and closing of that pair. And just to verify that this actually works with parentheses, braces, and brackets, I'm going to get into insert mode. And what I'm going to do, let's do opening braces, parentheses, brackets, and then some stuff here. And then let's do closing brackets, parentheses, braces. And then I'll escape to get into normal mode, zero to get at the beginning of the line. If I hit parentheses while I'm on the brace, it will take me to the last brace. But let me do H to move back to a character to the closing parentheses. And now when I do parentheses, it takes me to the opening parentheses. If I hit L on the keyboard to move over right a character, so I'm on the opening brace, I hit percent sign. You can see it takes me to that closing brace. Now let me, you to undo all of that. Let's talk about deleting and copying and pasting, because those are actions you're going to do all the time in normal mode in Vim. Let's first talk about deleting an entire line. You will often delete entire lines and then go paste them somewhere else. For example, maybe I want to move this line one line down. Well, I would do DD for delete the entire line. DD, delete the entire line. P, paste the line, right? So that's how I move that line, basically one spot down. Really DD and then P. What it does, it swaps the two lines, right? Because if I undo, you can see undo twice. We have this line here that begins with the hashtag. This is a commented line. Then I have an empty line, right? And if I do DDP, I just swap those places. Is all that is. Let me, you to undo. So DD deletes an entire line. YY copies an entire line. Vim doesn't call copy, copy. It calls a copy a yank. So YY just yanked that line. It copied that entire line. So now if I do P, it pasted that line on the next line. You to undo. Now let's imagine that you want to delete everything in this document or copy the entire document. Well, if you want to delete the entire document, let me GG to go to the very first line. We already know DD is delete, right? And we also know capital G takes us to the end of the document, takes us to the last line. Well, what if I do D capital G for delete and then G go to the end of the document? I just deleted this entire document. You to undo. What if I wanted to copy the entire document? You've probably already guessed. Y for yank capital G. And you can see 256 lines yanked. I just copied this entire document. If I hit P right now, I just pasted this entire document. So if I scroll down, you will see eventually I'm going to begin again. Here's the top of my bash RC again. So now instead of 256 lines, I have 512 lines because I yanked the whole thing, I copied the whole thing, right? And then pasted it again. So let me do to undo. So D capital G delete the whole document. Y capital G copy the whole document. And of course, those only work if you start on the first line of the document. What if I was 10 lines down in the document? Well, first you would need to GG before you do your D capital G or Y capital G, right? So if I wanted to yank the entire document but I wasn't on the first line, I would simply do a GGY capital G, right? So you can see how very quickly you'll get the hang of these commands, these normal mode commands. And it's so powerful, especially once you get the hang of using them all the time. For example, swapping lines. I do that DDP thing so many times where I have two lines and I want to swap them. I want to reverse the order of the lines. For example, maybe these two lines here, DDP, right? I just put those lines in different places. You to undo. And while I'm mostly sticking with the absolute basics of the VIM motions and the VIM key bindings, I'm not going to really dive too deeply into command mode other than colon W for right, colon Q to quit, colon WQ for right and quit. I will show you how to yank an entire document. Instead of having to do GGY capital G to yank the entire document, you could actually do this quite a bit easier in command mode because you could do colon and then you could do percent. Percent in command mode means the entire document and then give it a Y right behind it for yank. And you can see that achieves the same effect, 256 lines yanked. And as you probably guess, if I did a colon and then percent D for delete, that would also achieve the same effect for deleting the entire document. Now let's talk a little bit more about pasting a line. Let me copy this line here. So YY yanks that line, right? It copies that line. I'm going to go up a couple of lines. Now, if I do a P, it's going to paste that copied line on the next line. But what if I want to paste that line on the previous line instead of the next line? Well, it may you undo. And instead of P, if I do shift P, it actually paste on the previous line. So that is how you paste. So typically you're pasting to the next line, but if you ever want to paste on the previous line, capital P paste to the previous line. Also, earlier I mentioned insert mode. I gets me into insert mode. So I could start typing something, right? Well, what happens if you want to get into insert mode but not on the line you're currently on? What if you want to get into insert mode on the next line or the previous line? Well, on the next line, you just hit O. O takes you to the next line and automatically gets you into insert mode. And if let me escape to get back into normal mode and if I do capital O, that gets me into insert mode on the previous line that I was on. So now I could do this is another line. Of course, I misspelled all that. Let me U to undo. So those are very useful insert mode commands. Remember, I always get you into insert mode on that line. O always gets you into insert mode on the next line. Capital O always gets you into insert mode on the previous line. And one other thing, sometimes you want to get into insert mode, but you want to get into insert mode at the very end of the line that you're currently on. Capital A gets you into insert mode at the very end of the line. Escape, let me undo all of that. Now rarely are you wanting to delete or copy an entire document. More often you're deleting or copying entire lines, but much more often you're deleting or copying just words or sometimes even single characters. Let's talk about deleting single characters. X on the keyboard deletes the character you're currently on. So if I hit X right now, I just uncommented this line, right? The hashtag that was under the cursor point is deleted. If I hit X again, that space is gone. If I hit X again, the capital M is gone, right? Let me three U to undo three times because I knew I'd hit X three times, right? Remember, you can always type a number before a command to run that command multiple times. Let's talk about replacing a character. So imagine that instead of my bash config right here, I wanted that word to actually be cache. I wanted this to say my cache config. Well, the cursor is currently on the B. I can do R for replace. If I do R and then do C, it just replaced the B with a C. So R, name of character, is a replace. So if I wanted instead of cache, I wanted this to be my cast config. So I could do RT and it just replaced that H, which was under the cursor with a T. Now let me U to undo a couple of times. If you want to delete a word, type D for delete and W for word. So DW deletes word, right? DW again, deletes word. Now let me two U to undo that. And just like deleting a word is DW, yanking a word, copying a word is YW for yank word, right? If I go back a character and hit P, you can see I did copy that word. So let me undo that. So just a quick refresher. DD deletes the entire line. U to undo. DW deletes a word. U to undo. What if I wanted to delete from this character that I'm currently on to the end of the line? You've probably already guessed D for delete, of course. And then remember dollar sign for end of line. So D dollar sign deletes everything to the end of the line. U to undo. What about delete everything from the cursor point to the beginning of the line? Remember zero is the beginning of the line. D zero, right? Deletes everything from the cursor point to the beginning of the line. U to undo. And of course that works with yank as well. If I go to the middle of the line here and I do a Y dollar sign, I just copied everything from the cursor point to the end of the line. If instead I did Y zero, I copied everything from the cursor point to the beginning of the line. One of the things you often want to do when you delete a word, you want to delete a word and replace it with another word. So instead of doing DW to delete it and then I to get into insert mode and then type a new word, just do all of that in one command because there is a command in VM for change. What change does, it deletes whatever characters or section of text you tell it to delete and then it automatically puts you into insert mode so you can type the replacement. So for words, you can do CW for change word. It just deleted that word and now I could do something like capital B-A-S-H for my bash config and then escape to get back into normal mode and just like DD deletes an entire line, YY yanks an entire line, CC changes an entire line, it deletes it, puts you into insert mode so you can then write a new line. Now for me, I'm going to escape to get back into normal mode, you to undo a couple of times and just like we could do D dollar sign, D zero, Y dollar sign, Y zero. You could also do this with change as well. If I go to the first letter of config here and I do a C dollar sign for change everything in this line from the cursor point to the end of the line, you know, it puts me into insert mode and then I could write the rest of the end of this line. Let me U to undo that. If I want to change from the cursor point to the beginning of the line, I would do C zero and it deletes everything from the cursor point to the beginning of the line, puts me into insert mode so I can type a new beginning to this line. Let me U to undo. Now DW, CW, YW, all of those word commands, for example, DW, deletes a word, right? DW would delete that word. Let me U to undo. But sometimes you're not exactly on the first character of the word. If you're not on the first character of the word, say I'm on the second character of the word, DW, deletes the word, but it left the first character because the cursor point was after it. So what you want to do, if you're in the middle of a word and you want to delete the word, you have to DIW for delete inside the word. So basically, if I'm in the middle of a word, DIW is the command to delete the word. So, and if I, same thing with Yank, if I'm in the middle of a word, YIW to Yank that word because if I'm in the middle of the word and I just do YW, it's only going to Yank where the cursor point is to the end of that word. Sometimes you want to delete or you want to Yank a word and you want to include the spaces around it. So in that case, what you would do instead of DIW to delete inside word, you would DIW to delete around the word. So it deletes the word and the spaces around it. You to undo that, you could also do that with Yank. Y-A-W Yanks around the word. And similarly, if I was in the middle of this word, not and I did a C-I-W for change inside the word, I'm changing the word. Let me escape. You to undo. If I was in the middle of the word and I did C-A-W for change around the word, it also will delete the word and the spaces around it and put me into insert mode so I can make the necessary change. For those of you programmers, if you want a really neat thing you can do again with braces, brackets and parentheses, what you could do is you could actually delete everything that is inside a pair of braces or brackets or parentheses. For example, I'm in the middle of these brackets here. This is a line and you can see it's wrapped in brackets. Well, if I delete inside and then the opening bracket, I just deleted everything that was in that pair of brackets. You to undo. If I wanted to delete everything that's in a pair of parentheses, I could again just go anywhere inside this and do D-I and then the opening parentheses and I deleted that text. Also, I deleted the brackets because the brackets were inside the parentheses. You to undo. And one more time, let's try this with the braces. So I'll do D-I and then the opening brace and I deleted everything that was inside the brackets and the parentheses including the brackets and the parentheses themselves because I deleted everything inside the braces. I hope that makes sense, but if you're a scripter or a programmer, obviously these kinds of commands, the delete inside, braces, brackets, parentheses, you're going to use these all the time. The last basic Vim motions I want to show you are F and T. So F is go forward till the next character. It's basically find the next character. So if I do FB, it goes forward to the next instance of that character, B. If I do FB again, there's not another B in this line, but what about, how about F? F, F goes to the next F. F, F goes to the next F. F, F goes to that last F and then FF won't do anything from that point. Now let me hit zero on the key. Very similar to F is the T command. So F, what F did, remember, if I do FG, it takes me to the next instance of that character, G. What T does, it takes you to the next instance of G, except instead of putting the cursor point on the G, it puts the cursor point on the character in front of the G. So think of F as find, find the G and take the cursor point to it. Think of T as until with a capital T, until, right? So go till the G, but stop the cursor point right in front of it. So if I do TG, you can see, I actually go to the place right before G. TG again, well, there's not another G. How about TF? Goes to the U and stuff, and that's as far as it'll go. Now also in conjunction with the F and T commands, you can use semicolon and comma to move forward and back by the next instance of those characters. So remember, FF takes me to the F in config, but there's also two other Fs in this line. And what I could do, instead of doing FF again, I could just do the semicolon and it will take me to the next F. Semicolon, again, will take me to the next F. Comma would take me back to the previous F. Comma again would take me back to the previous F. And again, that works with F. It also works with T if I go to the beginning of this line and do TF and then the semicolon, again, will take me to the next F. But remember, T always takes you to the cursor point on the character right before that particular character you were searching for. And the comma, once again, will take me in reverse. That's when you do it in reverse with the T. It's actually the character right after. Because again, it goes in reverse all the way to F, but it's not going to stop on F. It's going to go a character right after it, in this case, because we're going in reverse. So I think we covered really all of the basics as far as your basic motion keys, how to move around a document in them, the basic editing as far as deleting, copying, which is yanking, pasting. We even covered a little bit of command mode, but really, the VIM motion keys, they're not that hard to learn. Once you know the absolute basic motions, like I showed you today, I mean, really, what I covered on camera on this one video, that's like 95% of everything you'll do in VIM. There's really not much more to learn. There's still some stuff to learn, but you can get by in VIM just fine if you know exactly what I showed you today on camera. Now, before I go, I need to thank the producers of this episode, Gabe James, Matt Paul, Steve West, Arkotic, Armoredragon, Commander, Angry, Darloff, George Lee, Matthew, Methos, Nate, Erion, Paul, Peace, Archon, Adore, Reality, Four Less, Very Profit, Role, and Soul, As, Three, Ten, Ren, Tools, Delaware, Ward, Gentoo, and Ubuntu, and Willie. These guys, they're my highest-tiered patrons over on Patreon. Without these guys, this quick look at the absolute basics of VIM would not have been possible. The show's also brought to you by each and every one of these fine ladies and gentlemen, all these names you're seeing on the screen. These are all my supporters over on Patreon. I don't have any corporate sponsors. I'm sponsored by you guys. The community, if you like my work, support me on Patreon. Subscribe to DistroTube over on Patreon. Peace, guys.