Transcript for:
Comprehensive Guide to Mastering Vim

my name is the primagen and I've been using Vim for about 10 years and I wanted to take the time to build a series in which could lead you from someone who doesn't know anything about Vim to someone who could become an expert at using Vim now this is going to be a multi-part series in which each one's going to add to the next one and it will get you progressively faster as you go and I do want to make a separation right away there is vim the program and Vim the Motions now Vim motions I don't understand why anyone who is programming does not use those those are extremely efficient and they're very well done and they're available in intell Sublime even Atom vs code anything you want to use they have them but Vim the editor I understand why that may not be as appealing you have to kind of want to be able to configure and create your own environment to exactly your own needs for Vim to be super useful and so this few part series is going to be focused on making you awesome at Vim motions and navigating some things within Vim I'll try to separate them out so if you're just not interested in Vim itself that's okay but if you are interested more in using Vim I have a video on your Vim RC which you can go check out right now links down in the description and as you pass by that like button give it like a little bit of coconut oil appreciate it all right so right away I do not think that you should start out in Vim if you have no familiarity with Vim I really do think you should continue to use vs code if that's what you're using intellig if that's what you're using whatever editor you're currently using use the Vim mode or the Vim plug-in now I think one of the big mistakes when learning Vim is that you try to learn every single key possible all the different shortcuts and then you're just sit sitting there just like mentally grinding to try to find the most optimal one in every single moment that sounds exhausting so that's why I just took a much different approach myself to learning it and I'm going to try to give you that exact same approach that I took 10 years ago all right so one last kind of preface this will be the hardest of all the videos because the getting started is the hardest because you're going from a paradigm of using the mouse and being much more kind of click and control oriented into using something like Vim which is just much more home row motion Centric now I know that a lot of you are probably using vs code I hear if you hit the Subscribe button right now it actually makes learning easier go check the comments people will say that trust me it makes it easier all right so the first thing you need to know about Vim is that it's a Modell editor whether you're using it in vs code or Vim itself you are in various modes and there's four modes that you really need to be concerned about first off the mode that you're in right away is normal mode it just means that I can move my cursor around second mode is insert mode you can see it right down here says insert that just means I can type like a regular editor hit all the enters do everything that it will'll try to autoindent and do the best job it can third is visual mode you can see right down here again I'm in visual line mode as I move it's kind of like highlighting with your cursor and of course the fourth mode is command mode if I press colon you'll notice right here that a colon appears and my cursor actually appears on this line I can now type out some commands a command you should know is W col W enter will save the current file col WQ will attempt to quit now I know I ruined all the memes but guess what the memes are still funny I actually still like them 10 years later I still enjoy can't quit Vim memes all right so let's go over each of the modes in detail all right so normal mode the mode you can move your cursor around the most foundational movements you need to know is j and k k will obviously go upwards J will go downwards I have my keystrokes right here so you can watch them happen L is going to go this way and H is going to go this way now these are the most fundamental movements I do recommend getting pretty dang good with them now if you have neovim I actually built the game for this exact thing so if you do command mode Vim be good you will get a game if you navigate down with your J's all the way down to hjkl press DD to delete that line it will start a game for you where you can actually walk around to each one of these and then press X to delete it X on the X and this right here will just help you get good and just kind of build that muscle memory in super quick without making it feel so boring as trying to use the text editing program but instead you got a little quick game really just helps you make that connection with those keys if you don't know how to do a plugin again I have that vimrc video which I have linked down below but you'll quickly realize that HJ K&L is just not enough right that H&L is really inconvenient you just like hold it and slowly go over that would just melt my mind now I told you Vim is fast it is smooth it blows people way when you see someone who is really good at it use it so obviously L can't l and H can't be the last of your movements so the last two I really wanted to show you was W which hops over by word something equivalent of like holding option I think on Mac and pressing the arrow key and B the opposite walks backwards so those movements are called motion a motion is anything that moves the cursor all right so I wanted to go over the anatomy of a motion so right now all we've been focusing on is this part right here HJ k l WB those are motions they move the cursor but did you know that you can kind of augment them meaning that if we add a count to it it'll be performed multiple times so check this out so you can see about eight lines up there is a to-do line right up there which if I press 8K I will jump my cursor eight times up to that to-do line if I wanted to go back say all the way down to the return statement 16 lines down I can go 16 J you can see right here I did 16j and my cursor landed on the return statement right there yes you're probably impressed with my vano white level pointing skills I know you can't train it you're born with it again if you have vmb good uh I did do another one of these games right down here so if you go all the way down to relative you turn on relative line numbers inside your vimrc you can do you know six down you can just practice the deleting game so that you can get good at jumping around while deleting relative line numbers have helped me in enormous amount so I highly recommend you getting used to jumping around with relative line numbers obviously counts work with all the other motions so I can technically go like 10 L but that'd be weird or 2B also that would be weird I that sounds like way too much overhead to try to count how many jumps you have I would not use that as a way to move around in Vim all right so let's add a little bit more let's talk about commands you can do I want to focus right now just on the D command d stands for delete meaning that if I wanted to delete a line I can press DD that will just simply delete a singular line I press U to undo that delete I can press contrl R to redo my previous action so U is like contrl Z controlr is like control y D's can be used with motion just like I said we can do a command a count and a motion so jumping back here if I'm on this line right here and I want to delete all the way down to the return statement but not the return statement what I can do is I can go D3 J I have deleted my current line plus three down below me that means I can also go delete word that's how you would delete a single word delete two word you could delete two words if you really want to again I don't do the whole two with words or back or any of those that means you can do DB and delete backwards so D is a command you can mix with a motion hopefully you're starting to understand and you're happy that I kind of went over the anatomy of a motion CU this should start making sense okay I can move the cursor and I can delete move the cursor all right so let's talk about insert mode now first off I want you look at where my cursor is right up here now this cursor of course is fat it had you know car it covers an entire character it's just something I've gotten used to uh some people when they go into insert mode their cursors can become thin I never like that so don't be taken back by this as you watch me go but if I press I I go on the left side of my cursor for insert mode so as you can see I pressed I to go into insert mode and then I just started typing and boom these characters come here now we can leave insert mode by pressing escape you can also press contrl C and you can also press control Open Bracket control Open Bracket and Escape are the same contr C does have some minor variations to it mostly in visual block mode that cause some weirdness to it so you got to know which one you like all right so you can press U to undo of course now if I press a you'll notice that it moves my cursor but I'm in insert mode so now as I type I'm on the other side of the zero and you're thinking okay when would you actually do you actually ever need that you'd be surprised at how often pressing a to move forward like how often you actually use that a and I become something that you should just use all the time it's very very surprising so I'm going to leave insert mode undo it there we go there's a bunch of other ways to enter into insert mode but I think you should really just get used to that right now we're about few options even if it's inconvenient you need to learn the basics and move fast all right so let's move on to the last part visual mode so this one I'm actually going to show you how to use yank and paste in visual mode so visual mode has two primary ways you're going to use it if you press V you'll go into visual mode down here that means I can use any of the Motions that I've learned so far so I can press W and you'll notice that it's like highlighting as it's go as it's going this is effectively like using your mouse and dragging it and highlighting a region at this point I could copy and paste so normally you'd press probably control C or command C if you're on one of those fills drink and coffee Max but in Vim you just press y y Yanks it now if I press p i will paste it as you can see I pasted now notice something it's not giving me a new line it's pasting the exact contents of what I yanked so let's undo it with you undo undo undo and this time let's use shift V we'll go into visual line mode now when I yank this if I paste it I actually get that new line at the end a little bit more convenient so you kind of got to know which one you want to do let's do a little bit of comboing so what I can do is I can actually highlight I can yank it I can jump down here and I can highlight this and I can paste it so you can paste over a highlighted region but I'm going to show you something that's a little bit weird what do you think happens if I press P now well I get the last thing I deleted so that means if you press DD and delete a line and you press P you actually paste the last thing you deleted so yanking and deleting go to the same buffer and again if you highlight a bunch of stuff and you paste it you'll delete all of that and then paste over with the contents you have in your paste buffer and now when I press p i get all the previous things I deleted and of course you can use yanking just by itself so I can press y y you don't have to highlight first to use yanking now if you remember the anatomy of a motion I did show you that you can do a command a count and the motion so I can do yank say five and down I just yanked all the way to this offset line right here so now when I press P you'll notice I got those five lines and I was able to paste them all which means I can also do the exact same thing with d D5 J and now I can paste the things that I just got done deleting down below now I really wanted to stress this and I wanted to make sure you had the time to hear me on this one which is this is going to be the hardest of all the lessons if you are new to Vim this is the hardest part learning how to do HJ K&L learning how to do relative jumps kind of getting your feet wet with that vimrc which is still linked down below like it's just a lot of information it is very difficult so if you're using VSS code don't feel bad just keep using VSS code that's what I did when I was going to use Vim as my editor I actually started off by using idea Vim the intellig plugin and I used that for years I got really good at the motions and then moved over so once you feel like you've really mastered these motions and they don't feel hard for you to think about they feel very second nature you got to move on to the next part of this series I don't know how many videos this series is going to be somewhere between 3 to five but I am hoping to give you a very detailed way to make Vim as your editor and mostly I just hope that you feel encouraged because you know what when I use Vim as my editor it almost feels like a game because I have all these combos that I'm doing I can move really really fast and I have the ability to do a lot without having to think too much I really do like the idea of making everything second nature to the point where you don't think about editing the text you just edit it really fast and that's the coconut oil goodness that I'm trying to give you okay if you want to be smooth and sliding around you just got to master the fundamentals okay practice for a week and come back for part two the name is the primagen