 I love Vim. Vim the text editor, Vim the set of key bindings, the Vim workflow. I love everything about Vim and people often ask me, why do I like Vim so much? Does Vim really do anything that all of these other text editors, these plain text editors like notepad and nano and g-edit, does Vim really offer anything more than those or is it just all about looking elite doing things in a terminal, you know, memorizing key bindings? What is it that Vim really offers? So today, I wanted to share some tips and tricks with you that I've learned over the last few years. Really, I'm going to show you some of my favorite Vim key bindings that, you know, once I discovered these, they really kind of changed the way I used them. So I made a copy of my Bash RC and I've opened this here inside Vim just as a test file to play around with some stuff. And one of the things I really love on a Linux system, you often are playing around in config files and you often have to uncomment lines. And uncommenting lines in Vim is really easy. For example, here in Bash Scripting, a commented line begins with the pound sign, the pound symbol, the hashtag, whatever you want to call it, and just put your cursor exactly on the character that needs to be deleted and just hit X. And now, you know, I've uncommented that line. I could colon WQ to write and quit and then I'd get out of this config file. It makes really, if you're doing a really simple edit, which you're often doing this on Linux systems, maybe you got to uncomment a line in your sudoers file or your pacman.com for whatever it happens to be. If you open the file in Vim, you get to the line that needs to be uncommented, put your cursor exactly where it needs to be, hit X and then colon WQ, write and quit, and you're out of it. And it just makes things really, really simple and easy. And it really is all of the delete commands that really impressed me when I was learning with Vim, you know, what really blew my mind is, you know, like this line on mine, Export Editor, what if I wanted to swap this line, Export Editor, with the line below it, Export Visual? Well, just do DDP, right? And those are actually two separate commands, DD, delete the line, P, paste the line, right? Just think of it as basically a cut and paste is what we did. Just always remember, DDP will take the line that you're on and it will swap it with the line that was right below it. Another common thing I do on my Linux systems, and you probably do too, is you're often changing values of variables. This variable, for example, here in this bash script, in my bash config, Visual, and you see Visual equals and then you have some value. And Vim makes changing really anything inside a pair of quotes or a pair of parentheses or a pair of braces or a pair of brackets, really, really simple. If I just want to delete everything inside these parentheses, I could delete inside DI, so DI, and then do a quote. So DI, quote, deletes inside the quotes, right? That's really easy to remember exactly what that does, you to undo to get that back. If I just wanted to copy everything inside the parentheses, a copy inside Vim is yank. So yank inside, quote, I just copied everything inside the quotes. If I go down the line and P to paste, you can see that we, in fact, did copy that. I can U to undo. And that was really one of the game changers for me when I was learning Vim, is once I figured out that I could delete and yank inside quotes and inside parentheses. Man, like nothing else. And like, you could, is there a plain text editor that has that kind of functionality that makes it easy that you can just do that with a key money? If there is, I'm sure there probably are some. I don't know of any. And because of that, I could never, for example, go back to using something like gEdit. Some other really cool delete commands that I do all the time. I often have to delete an entire document, or maybe I want to delete this document and paste it into another document. Well, it's really easy to delete an entire document here inside Vim, because all I would do is D, capital G, D for delete, G all the way to the end of the document. It was already on line one, so that's why that worked. I could U to undo. What if you were at the end of the document? Well, let me do capital G to go the end of the document. And in this case, what I would do is D, G, G. And it deletes everything from the last line to the first line, because G, G takes you to the beginning of the document. So I really love those two commands, D, capital G and D, G, G, for deleting the entire document, depending on whether you're on the first line or the last line when you're making that deletion. So these deletion key bindings, X to delete a character and D to delete multiple characters, lines, multiple lines, blocks of text, X and D are great, but they work in normal mode. And sometimes you might want to delete things while you're in insert mode. And of course, while you're in insert mode, if I hit I to get into insert mode, I could just do backspace or the delete key, just like you would on a normal text editor, and that would just keep deleting. But is there a better way? Well, I mean, you just want to delete a single character. Yeah, I mean, that works. But what if you want to delete an entire word? Can you do that while you're in insert mode? Or do you have to escape and get back into normal mode? No, you actually can delete entire words inside insert mode in VIM. Let me move to the end of the line. I'm going to move right here to the ending double quotes here. If I do control W, did you notice what happened? It deleted the word Emacs. It deleted the entire word. It deletes the entire previous word wherever your cursor is. It deletes the previous word. Let me undo that. And if I move to a different line, how about this commented line here? I know that's going to be kind of hard to read. But if I do control, well, let me get into insert mode first. Control W, it deletes the previous word. Control W again, deletes the previous word, and et cetera, et cetera. Let me escape and U three times to undo that. Now, if you wanted to just delete a single character while you're in insert mode, you could do control H. Control H just keeps deleting a single character. But obviously just hitting the backspace of the delete key would do the same. It's really control W. That's the powerful command there to know in insert mode is deleting that previous word. There is also a command you can do inside insert mode to delete everything up to the beginning of the line. So if I do control U, it deleted everything that was in front of my cursor back to the beginning of the line. Let me U to undo that. So those are some pretty neat deletion commands that actually work while you're in insert mode. So you don't have to escape and get into normal mode and then do some combination of D and another command to delete words or characters or whatever it happens to be. Another thing you can do while you're in insert mode is you can always enter a normal mode command as long as you first do control O. So control O while I'm in insert mode now would let me do something like, for example, DI quotation marks. And again, I'm still in insert mode, but it allowed me to run that normal mode command while I was in insert mode. Now let me escape and undo that. So anytime you're in insert mode, just do control O. And then if you want to then run your normal mode command, DW, for example, to delete a word, which it deleted the Emacs client from the E where the cursor was until the space that was there. So let me U to undo all of that. So that's really powerful because how often are you constantly switching back and forth between normal mode and insert mode sometimes for really just you only need to enter normal mode for one minor tweak. Well, just control O and then run that normal mode command, and then you're still in insert mode. So you don't have to worry about doing escape or I or any of that to switch back and forth. One really neat thing about Vim is how it handles indents. So it will actually do indentation for you if I was doing some if statement. So if this, then, you know, and then the next line of the if statement is already indented to the correct spot. But what if it wasn't? What if instead I hit enter and I ended up at the beginning of the line? Could I force the indentation? And the answer to that is yes, just inside normal mode do capital S and it puts the cursor to the correctly indented spot based on what Vim interprets the indentation to be in this if statement. Yeah, it would need to be spaced over four times, for example. Now, what if you needed to indent an entire document and this is really where Vim becomes extremely powerful. So if you want to indent an entire document, first go to the top of the document. Remember, gg goes to the first line and then hit equals equals is indent and then go to the end of the document. The last line remember that is capital G. So gg equals capital G and you didn't see it because nothing needed indenting here. But if I scroll down, you know, this was all indented before. I think it unindented like it did the opposite. And yeah, this actually wasn't indented before. And now it is, let me undo so you can actually see the differences. So you undo, you can see these if statements. Now they're back to normal where they were indented. And if I go to this statement here, that's a if then else statement. Actually got a case statement inside the if statement. Now let me do gg equals capital G and now let me go back to that case statement. Let me find that. Remember, it wasn't indented, but now we just indented the entire document and that's that same function, right? So and it looks perfect, right? It put the indentation exactly where you would expect. So that is one of the really neat features that I don't think a lot of people know Vim can do. So if you ever got any kind of bash script or Python script or whatever it happens to be, sometimes you get these weird things where you copied, especially something from GitLab or GitHub and it's not spaced, right? It's not indented, right? Vim can actually fix that document for you. Another thing I don't think a lot of people know about is that you can actually tell Vim exactly where you want the cursor to be on the screen. And you can think of this as lmh for low, middle, high and do shift L. And it puts the cursor in the low spot. So here in my case, it's like the bottom third of the screen is where it puts the cursor. If I do capital M, shift M for mid, it puts the cursor in the middle of the screen and then shift H for high. It puts it in this case, it puts it at the very first line. Let me go to the last line and do shift H and you can see it actually will put it about the top third of the screen. So that is shift L, shift M and shift H for low, middle, high, whatever it is you want the cursor on the screen. And you can also do control U and control D to move the cursor. What it will do is it will jump the cursor up half a screen. So if I do control U for up, it moves the cursor up half a screen. If I move down control D for down, it moves the cursor down half a screen. Now of course VIM is nice for programming and scripting and doing nerdy things with Linux config files and things like that. But a lot of people actually use VIM for proper writing. There are a lot of like professional writers, novelists and things like that that actually use VIM because VIM is perfect for that sort of work as well. And a lot of people are probably going to be surprised by this, but did you know that VIM actually has some built-in spell checking? All you need to do is in command mode, so do colon to get into command mode, colon set and then do spell. And you can see now it has these underlines under certain words. That's because the spell checker is running. Now everything is going to be misspelled here because it's a bash script, right? This is not proper English, most of these words. So that is colon set spell to turn on the spell check. And for those of you that are kind of new to VIM, anytime you set something like you set the spell checker in this case or you set expanding tabs or whatever happens to be, sometimes you want to turn it off. And how do you do that? Just know anything that you set. You can unset it, but not with the command unset. You would actually run set again and this time do no and then name of the command. So no spell. And now the spell check you can see was turned off. So colon set no spell or I could have also done colon set spell ampersign would have also worked. And if I actually do colon and up arrow, colon set spell and then the exclamation I believe would also turn that off. So there's actually three ways to unset a command. Another thing that I don't think a lot of VIM users know about. They don't spend much time in even content creators. Well, I'll speak for myself. I've made a lot of content about VIM and I really don't tell you guys about the help functionality inside VIM. There is actually a colon help for, you know, get help on a certain key binding. So if I do help and then space J, for example, I will get this horizontal split here at the bottom and it will actually tell me what the J key does. And you can see J goes down essentially. Now, if I want to close this horizontal split on me and I can do control WC for control W window C to close. Right. And so you can think of it that way. Now, if I do colon help, you don't have to type the whole word help colon H is colon help. Essentially it's just a short form of it. If I do colon H and then D for the D key, what does D do? Well, we already know it deletes and it gives us examples of various commands that we could use with the D key control WC to close that split again. And if you want to look up what a certain key does in a certain mode, you can do that with the help command. So if I do colon H and then space and then do V underscore D, it will tell me what the D key does inside visual mode. And if I hit enter, you know, it just gives me the same documentation that we were looking at before, but it skips to the part where D is actually in visual mode and tells me what the delete key does in visual mode. And of course, that would also work if you wanted something for example, if I up arrow, if I want to see what D does inside insert mode, well, it doesn't do anything inside insert mode. D is just a D inside insert mode, right? It just writes a D. So a better example of looking up something in insert mode, really the only thing that has a special function inside insert mode that doesn't just print a character is the escape key. Escape and inside insert mode. What does it do? It takes you to normal mode. Well, let's see if that's what the documentation says. And you can see escape go back to normal mode. Control WC to close that split. So that's just, you know, some of the stuff that I really find useful. Some tips and tricks that I know for me kind of changed the way I use them. And these were some of the things that really impressed me, especially the deleting and yanking inside parentheses and quotations. Like once you figure all that stuff out, where you can quickly delete exactly what you want in a split second and then paste it exactly where you want. That really is what transforms Vim into something much more than just your standard text editor. And for you guys that are already using Vim, you've been using Vim for a length of time, maybe months, maybe several years. What are some of the tips and tricks that when you discovered, you found mind-blowing? Share down in the comments below. Now before I go, I need to thank a few special people. I need to thank the producers of this episode. And of course, I'm talking about Gabe James Maxim, Homie Stubald, Matt Mimit, Mitchell Paul, Royal West, Armored Dragon Bash, Potato Chuck, Commander Henry, George Lee, Mars Drone, Methos, Nate Erion, Paul Peace, Archimdor, Polytech, Realiteats for Les Red Prophet, Rowland Tools, Neville Williams, and a bit of these guys. They're my high-steered patrons over on Patreon without these guys. This episode you just watched would not have been possible. The show is also brought to you by each and every one of these fine ladies and gentlemen. All these names you're seeing on the screen right now. These are all my supporters over on Patreon. I don't have any corporate sponsors. I depend on you guys. If you like my work and want to see more videos about Linux for an open-source software and Vim, subscribe to DistroTube over on Patreon. All right, guys. Peace. Ah, those poor nano users, they don't have any of this stuff.