Vim Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Any one has some good videos for experienced user? I want to go deeper.

πŸ‘οΈŽ︎ 7 πŸ‘€οΈŽ︎ u/rudevdr πŸ“…οΈŽ︎ Aug 12 2019 πŸ—«︎ replies

set mouse=a

Now you don't have to 'throw away your mouse because you see as we click around nothing happens'.

Also, holding down 'h/j/k/l' to go in a direction is just as bad as using the arrow keys.

πŸ‘οΈŽ︎ 4 πŸ‘€οΈŽ︎ u/crajun πŸ“…οΈŽ︎ Aug 12 2019 πŸ—«︎ replies

Didn't saw all but u might want to use ^ instead of 0w

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/tobeportable πŸ“…οΈŽ︎ Aug 12 2019 πŸ—«︎ replies

you made a lower case register as one example and then you made an upper case register an other example when you recorded macros.

That worked by fluke as the register was empty when you started. Upper and lower case letter registers point to the same thing, it's just that upper case appends to the previous content of the register lower case replaces the content of the register. So this way you can add more keystrokes to an existing macro.

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/phaul21 πŸ“…οΈŽ︎ Aug 14 2019 πŸ—«︎ replies

It would be awesome if you had a written list of the topics you talk on this video.

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/caotic πŸ“…οΈŽ︎ Aug 12 2019 πŸ—«︎ replies
Captions
all right so I'm gonna be teaching you everything that I know about vim we're gonna be starting from some of the beginner things and then ramping up to some more advanced techniques that I use so you're gonna be learning all the key minds and shortcuts that I use when I'm just coding everyday in vim now I think this is going to benefit you a lot if you spend the time to learn it it probably takes about a few weeks or so to actually get you know used to them and then after that it's one of those things that's gonna pay huge dividends over the course of your software engineering career and I wanted to just answer one comment I saw about what's the big deal about so you can code faster in vim and I want to just answer that by saying I don't think it's so much about what you can get coding faster I don't think it's about the speed at which you're typing or going because I'm not even that fast of a type or myself my words per minute is not that fast but what I think what does give you is efficiency and being precise and very effective I kind of see it like do you want to be the guy that is digging a hole with a shovel or do you want to be the guy that's digging a hole with a bulldozer you're just gonna be able to do it a lot easier with the bulldozer and I feel like that's the same way with them once you learn it it's gonna feel like magic and you're gonna be like wow I want to use this everywhere so it is a text editor and you can use the Edit code and you're gonna want to kind of use those key binds in other places cuz you just like it so much and you're gonna wonder where did I why was I not using this before so yeah let's just go ahead and dive into things I have my terminal up right here and so Mac and Linux users you can open your terminal and vim I believe comes pre-installed windows you're not so lucky yeah you're gonna have to Google how to install them maybe with the Linux subsystem that can help you out and you can open up prompt and follow along alright so we are going to type them and then a file and you'll notice I have these little alerts that are gonna be popping up here so you can follow along with the keys that I'm pressing as I'm doing stuff into them all right so I said vim that's the command to open them and then I'm pointing it at a file so this has happens to be a file that I'm in my current directory and I'm gonna say enter to open it up so if you want to follow along with the exact same code that I'm using I'm gonna put this link in the description otherwise just open up a piece of code that you'd like to edit so just vim and then the name of the file that you'd like to open all right so this is them so you'll notice the first thing is you can throw away your mouse it's pretty much useless see as we cook around nothing happens but at the top left here we can see that is our cursor all right and I thought the first thing I would teach you is how to exit them because that's become a meme now and you just get stuck in here so so you don't get stuck let's start with how you can get out of here all right so if you say : you'll notice your cursor is now down here and I can type letters down there the letter you're gonna want to type is Q that means quit now you can prefix this with a W which means you want to save and quit or you can put an exclamation point at the end that means quit and don't save the file so after you've typed the command that you want if you hit enter it's gonna quit out for you all right so : and then WQ would save the file and quit Q just quits if you don't have any Save Changes but let's say like I don't know I add a J alright we just actually I don't want delete all those files well add some stuff there alright so I added these two letters right here at the top we're gonna talk more about how I just did that and I press Q it's gonna say no right since last change so we can either do Q exclamation point to not save our change or WQ to save it alright the other thing to note is if you want to just save your change and not quit if you say : W and then enter that I'll save the file as you're going along alright so by default you're in command mode when you first enter vim this is what command mode looks like your cursor looks like that and so what you do from here is you can move your cursor around by pressing J and K so J goes down K goes up you impress L to go to the right you can also hold these things down and then H takes you to the left alright you can hold it down to go up down and so on and then once you've navigated to the place you want to start typing stuff you press I this drops you into insert mode when your insert mode you can type but here I can say hello alright and then this is just you can type normally and then if you hit escape you go back into command mode and you can start typing commands the first command we're gonna learn is the D command which is delete so if you press D twice it deletes a line so now I just deleted the hello line that I just added press K to go back up J go back down and again L and H to go left and right so yeah this is very weird at first I remember well strings for starting vim this was very hard to get used to but it's gonna become natural soon enough so force yourself to use these because you can also use the arrow keys to get around but this is way worse you're able to keep your key your fingers like right where they should be if you're normally typing and so it's very handy for other stuff later on so definitely recommend just kind of moving it around getting used to this and forcing yourself to use these keys alright so the other way you can move around in vim is with the G key so here I'm going to press capital G to go down and double G to go back up and one thing I'm going to just mention real quick is if you watched my last video we're gonna be going over some of the similar techniques so if you want to skip ahead a little bit you can or if you want more practice and go through it again alright so here G takes you to the top your capital G takes you to the bottom and G takes you to the top so that's the cool thing about them we don't just you know move one at a time up and down we can start moving in different ways alright so capital G to take your cursor should in the file Gigi take you to the top you can just press those back-and-forth to go up and down the other one that I quite like to use is to skip blocks of code so that is right curly brace it goes down a block of code so you notice how there's like code all the way from here to here and so what its gonna do is gonna take your cursor from here all the way down to there and then I can do the left curly brace to go up a block and then I can hold it down and we can just skip a couple blocks of code and we can go back up so a lot of times what I'll do is I'll capital G to go down and then I'll go up a block of code and then I'm basically near the bottom of the file and that's one way you can kind of navigate all right and then again I can just hold my K if I want to move like this if I wanna move a little bit slower the other thing you can do is you don't have to just sit here and press K the entire time or J and just wait for your cursor to go down you can put numbers in front of it so for example I can put the number 20 and then press J what that does is it takes you down 20 lines so I could say you know May it's go 50 lines down or 50 lines up or ten lines to the right so you type a number and then you can say hjkl any of those things work by the way numbers and postage eg you go to the top number she can pretty much put in front of all of the commands and them and it's gonna basically do it that many times so for example I can say ten and like you do a curly brace and that's gonna take me down ten code blocks through three code blocks up all right all right let's do GG back to the top all right now let's talk about deleting a line and then undoing it so here I'm going to delete this line with DD and then I'm gonna press you to undo it all right and then I can press DD again DD again DD again so I just deleted it three times I press you that's gonna undo it once I can undo it again so you can press you a couple times to unravel each undo so use a very handy one and again we're all in command mode right now so what you're going to be doing is you're going to be doing commands to move around and modify the code and add code usually gonna go in and you're a type you know like that alright we can delete that again with D so how I just did that sequence is I pressed I getting press enter you can go up with arrow key there and I can just say hello escape out D D to delete it all right J down we're gonna do L over the other thing is when you delete something you may want to undo it you can redo with ctrl R alright so undo see you control ours redo those are very handy and again you can control are a couple times if you need to so let's delete a couple things here I just use period so I guess period read does the thing that you just did so for example I just did D you know I want to do it again so you can press period to do it again we're gonna talk more about period in a second but it's a nice one alright so what was gonna show though was I can you a couple times I can also control are a couple times to read you a couple times so each one you can repeat alright so another hand evil things I like to do is just copy and paste lines of code so let's say I want to paste this line of code right here I can it Y twice that copies the line to my clipboard and then I can go to where I want to paste it and then push P now P will go below so paste below now I believe if I want to paste it above I can do capital P yeah I rarely use capital P I just had like a vague recollection of it I usually just press P to paste it down the other thing you can do is you can delete a line so DD and that's gonna copy it to your clipboard so let's actually delete this one since we already had that one on our clipboard again dd delete P to paste go and paste as many times as we want right can move up paste move up paste so K to move up and then P to paste the other thing is usually I like to know how much I'm gonna delete before I delete it but if you know exactly what you want I can press for example 10 and then DD and that's going to delete ten lines of code so again you can put numbers in front of these and it's going to multiply or do that number of times okay so I like to use capital V what this does is this drops you into visual mode so visual mode allows you to select a group and you can visually see that you have it selected so you just from here you can do K to go up J to go down you didn't use all your movement commands so the other movement command we went over is curly brace to go down you can do capital G to go down GG to go back up so any of movement commands you can use here and that's just going to show you what kind of the text you have selected and then when you have it selected you can do stuff on top of it so for example I can press D to delete it then I'm like you know what I really want to delete that let's press E you well you know what I actually did want to delete it and we could do re do with ctrl R and then you again to bring it back alright so J to go down the other thing is I want to insert a line right here like you see where my cursor is it's right here and I don't like how these things are touching so to insert a line what I'll usually do is I'll press o and it'll drop you down and then you can hit escape and I'll take you back out to insert mode so by default what o does is it adds a new line and puts you inside insert mode hey alright so we can now type however we want to and I can escape to go back out press D to delete it if I don't want that line so I can also add a line up above right o below capital o above all right so just there's capital o it's gonna add a line above or my cursor ease so I'm xscape again by the way this is my escape symbol kind of weird-looking alright so I'm gonna press D delete that other thing I wanted to mention real quick is there's about a hundred different ways to do the exact same thing in vim so pick the way that is most most natural to you so what I'm showing you right here are the commands that are easiest for me and I remember it the easiest but again there's tons of different commands you can mix and match so for example we can press D and then we can do a move command so like curly brace down and that's gonna delete the entire block so that's a pretty cool one that I just learned and WordPress to you but what I prefer to do is to just usually capital V and then select the line so I can visually see what I'm deleting before I delete it so like 99% of time if I'm deleting something I'll usually go into visual mode and by the way capital V is selecting blocks of code or lines of code so it notes like when my cursor goes up it selects the entire line so usually I want to see what I'm deleting before I delete it and so I'll do that and I'll press D to do it but again if I want to delete that there's tons of different ways I could have used to delete it right there right so I think I can you know remember we talked about you can do like 15 and then dd delete 15 lines of code I'm so just note you can pick the different ways you like there's no right way to use vim there's a tons of keybinds that you can do okay so now we're going to get into how to move in the horizontal direction so we kind of talked about where you can get to a single line or move up and down so now once you get to the line you want to go to we can now quickly kind of get to the power of the line that we care about so for example my cursor is up here at the top and I want to just go to the next word so this is the most common thing that I'll do to move across the line it's all press W and W takes you to the next word right so you can see if I press W it basically just goes a word at a time now I can do the reverse direction by pressing B now to go backwards so W and B is a very nice way to just go through the line here so this is me just pressing W and B to go back and forth one other thing that I want to mention was if want to go to a specific line we can press a colon and then do like 30 and it'll take you to line 30 so let's say I want to say : 200 to go to the bottom or colon 0 to go to the top we can do that so I'm gonna go to line 80 here we're gonna move down and now if I press the W you're gonna notice it's gonna stop at some interesting places right you can see it stops like that now if I want to move my cursor all the way to the beginning again I impress B all the way back but that's kind of annoying so we can speed up the process by pressing the zero zero takes you to the very beginning I press W again carat will take you to the beginning and then where your cursor is right here the first word basically a combo that I like to do is I'll press 0 and then W so 0 W basically emulates the behavior of karat all right we can go to the end of the line by saying dollar sign so 0 W at the beginning and then if we do capital W you'll notice that we kind of like go further with every press and so what the W does is it ignores punctuation or capital W and there's also a capital B to go backwards with that so if I press W we go all the way to this equal sign so you notice I guess we just break out spaces so 2 space actually let me start pressing capital W yeah so you notice like it ignores everything until the next space it looks like is what capital W does the way I've heard it said is it ignores hoops let's turn off my sound um the way I've heard it said is it ignores punctuation capital W and capital B is gonna take you back so depending on how how much you want to move in the line you may use W right or you may use capital W I tend to just use W the most and then I'll just hold it down until I get to the place that I want to this is pretty fast to get to the place that I want to now we can be even more precise if we want to so like let's for example say I want to go to this exclamation point here if you say T and then exclamation point the T stands for go-to basically so it's see how it took my cursor right next to it I believe if I push F so I just did 0 W take me to beginning if I say F an exclamation point you'll notice my cursor will be directly on it so f takes you on it t takes you one to the left of it so we can go exactly where we want to end the line another cool one is the percent sign % will actually take you on the same line or else span multiple lines what do I mean by that so here you can see it's a curly brace % will take me to the other curly brace over here and then we can toggle back and forth so let's say I go up to a parenthesis right here and you can see the ending parenthesis here so if I say % it's gonna take me down there and now my cursor is here so that's a pretty handy one now again we can press like D and then % and we're gonna delete everything between those two percent signs so we can press Udaan do that so you can put a I forget what they're called I don't know if they're a chain emot-- I know this is called a motion so motion is like when you do a move I'm not sure exactly what D is called I think that may be called like a change let's see we're gonna do see in a second but it's basically a command that changes something oh by the way so like I've pressed D here and let's say I'm I was thinking about pressing D again to delete this line but then I changed my mind in the middle like I press D and I'm like you know what I don't actually want thee you can hit escape to clear that out so I press D escape and then you basically reset so you can do that if you want to be like oh you know what I actually do not want to like use D at the beginning yeah so if we can do per D and then whichever move in command we want right so that's where we can do percent we can do J can do K can do our up block down block and so on all right let's see you take us back all right now let's talk about C sub D is for deleting C is for changing all right so let's say I have the word - duze here and I don't want the word - duze I want something else I want to say to do list or something so what I can do is I can say CW and that stands for change word so I can say to do list so you'll notice what it did is it got rid of the word and then it dropped me an insert mode alright and I'm gonna hit escape alright and I'm gonna move my cursor to the beginning of the to-do list by pressing B and I'm gonna say DW it's gonna be delete a word um so I can very easily spam DW a couple times to delete some stuff now and press u and undo this a little bit oops went too much I'm gonna control our alright I'm gonna say shift G to go back down to where I was and I'm gonna do up curly brace left curly brace to move up alright a lot of times you may want to like just delete everything to the end of the line it's like for example here I can do capital D to delete the rest of the line you done do so capital D deletes the rest of the line where your cursors are everything to the right of your cursor basically capital C is going to delete it and then put you in insert mode so so a very quick iteration that I can do with this is I can be like change and then we can type out our code and then we can go back to command mode all right I'm done do both those now you'll notice usually if I'm coding I don't want to delete like the very end like maybe I want to keep this bracket right there so a lot of times what I'll do is instead of doing a capital C to destroy the entire line or entire to the right of the line is I'll do C and then I say T and then I say what I want to change until so what that means is I now press the letter so the letter I want to change into is this curly brace so I'm gonna do race so what I just did is I changed deleted everything up into the curly brace and then I went to insert mode I could do the same thing with Dee so I can say D T and then in curly brace alright deletes everything - the curly brace and I can do this with really any character so up here a common thing that I'll do is I want to delete to the slash so DT / so again it is DT and then whatever letter you want to delete - now note let's say I want to delete - this T you notice that there's a T already here so if I do d TT deletes to the T it's gonna hit the first one and I can do d TT again to delete into the T all right so there's change t / if I want to drop into insert mode and if I want to like delete it and just stay in command mode then I dude the deke if instead of the C all right let's do you go back alright so there you go so that is a really nice one that I spam a ton so this one is very handy also for when I want to just like change the prefix of a word so like I have to do form here well today I want to keep the form part I'm just gonna do say CTF and then we can say like Yolo form there right so here I just changed the beginning part here and kept that part so it allowed me to very easily just change the parts that I want to or a lot of times like my function names are camel cased and I want to change that and our cool command that I like to use is the star so here I can see them on the word handle delete to do so I'd like to see where else I'm calling this in the in this file or where the word handle delete - is used so if I do star you notice it's gonna take my cursor to this word so if I hit star again I can basically toggle between all the instances of this being used all right can do this with toggle complete - I can see it's being declared here I can see there is it there as well and you can see basically it just finds all the words all the places where this word is being used so we can do this let's move our cursor down here I was clicking with the mouse like a noob all right here we go so update to do show you can star around and we can get the location and then I can say CT T and I did capital T there because that was a capital letter here now I can rename that to Bob whatever I want and now compressor you don't do cuz I don't actually want the Bob but I can control are to redo if I want you back and there we go alright so W again is one that I like to just move around to location and I would say those are the main ways that I move around this way is I'll press W and B to go back and forth if I there's not a particular place but if I want to be like really elegant then I will just go directly to the place that I want to go so I'm gonna say T to the parenthesis and I didn't take me directly there or F to the pen see if I want to go on oops F that print C if I wanted to go directly there now let's talk about one other thing which was if you want to go to like the next instance alright so what do I mean by the next instance so let's see there is a capital D here there's a capital D here so let's say I say T and then capital D I took my cursor here now let's say I want to go to the next instance of D I can actually hit the semicolon here it'll take me to the next instance alright so I can press that again there's no more in this line let's do this one here so I'm gonna find that T and then hit semicolon and I can go through each T and in this line alright by just hitting semicolon it goes to the next one so that's another way you can kind of go that way and occasionally I'll use this alright I don't think I talked about this command yet but the Z command you press z twice and it's gonna Center your cursor so I use this occasionally too just like I brought my I I want to like code on this line of code here I did 0 W there to go at the beginning but that doesn't really matter and I press Z to bring this to the middle z we're now in the middle here you can do this when your cursor is at the bottom of the screen it's easy or at the top of the screen I like to do this occasionally to just Center my work yeah and I think that is all I wanted to show with the horizontal direction again W and B are gonna be your main players here and then T and then going to something or F and going to something most of the time I think I use T that just seems to be the one I use though f is another nice one as well oh there's one other command I want to show you guys and that is a so capital a is usually the one I will use okay let's actually talk about what lowercase a does first though alright so a will take you into insert mode and it'll move to the right once all right let's show that again so my cursor is on the T I press a and you can now see my cursor moved over to the right so if I press I that puts me in insert mode directly where my cursor is a brings you over one to the right capital a takes you to the very end of the line so a lot of times like let's say I want to get rid of this in my colon a lot of times I'll do is I'll do capital a backspace and I'll go back to the insert mode or sorry command mode hit escape can delete that with you another command I haven't talked about it's just X X just deletes a letter there so where if my cursor is on this I can say X and I'll delete whatever my cursor is over another place again I could say 10 X and it's going to delete 10 letters to the right capital I takes you into insert mode at the very beginning of the line this I tend to use sparingly it's super handy one I just don't tend to think of it too often all right so now we're gonna get into kind of some miscellaneous things and just commands that I haven't gone over yet that I really like and use all the time so the first one that I want to do is capitalizing things so I'm just gonna move around with W here and J and K and let's say I get to like this to do and I actually want this feed lower case for some reason if you hit the tilde this will actually swap the case so if it's lower case a good upper case it's upper case lower case so I can tilde some stuff and I can swap the case of it so I find this super handy you can also do this multiple characters at a time so like let's say I want to do the next five characters I want to be changed the case you can do five then tilde and then it changes them all swaps them around and so because I'm doing camel case a lot in my code a lot of times I'm gonna want to swap things go over camel case it or on camel case it and so that's a nice little thing I could do to swap alright the next thing I want to show is the period command this one is probably my second favorite vim command so what it is is it redoes the last thing you do alright so what does that mean well let's say I do CW to change this and I'm gonna rename it Bob and then I hit escape so now I've moved my cursor here and on a push period alright so what you just saw is it renamed it to Bob so it reran the command I did CW and then retyped it to Bob alright we're gonna do both that I mean this is not just this kit works with any previous command that you did so like DW delete a word come down flush period deletes the word and so I love using this command you can do DD delete I can just spam period a couple times just deletes a bunch of stuff because that was the last command I wrote and you can get really creative with this I think the my like favorite use case for this is like changing things it's like between the two parentheses or sorry - clearly braces here I'm gonna change this to be my funk and that's what we're gonna call so now I can do the same thing here alright so I've now I'm now able to both these places easily make that change and notice the insides were totally different so I was able to just rerun the command which was change until it'll be clearly raced there and then type out whatever word that I wanted so that's the way I use the period the most and it's just super overpowered so I'm gonna do Z and center this another example do capital C with this hello a lot of times it's really just me doing the change command and then wanting me to change it in another place the same way alright so change the line or place it with hello you'd undo what I just did there so here that I want to change this to Bob up here one changes the Bob I want to change this Bob all right so just did that really fast so let me show you break down how I just moved my cursor there so all right so we do see capital C Bob hit xscape I press K to go up here and then I press B to go to the beginning then I press period because period does the command that I did there that you okay I press B to go to the beginning just do that all so let me show you one other way you could have done this right because again there's a million ways you can do these things so let's do CW so we're just gonna replace a single word and we do Tom then we hit escape so now I'm gonna say go up five so five K go up one now I'm gonna say 0 W bring my cursor there hit period k op 0 W period so usually change and delete are the ones that I'm using so if I want to delete the whole line with D and do that and so the period is just super versatile all right so I showed you the tilde showed you the period let's do our next so what are does is that lets you replace a letter so let's say I want this to be a T I can press R and then T and then just replaces the letter there so we can press you to undo that again so let's say I want this to be a parenthesis instead are and what you want to replace it with I actually forget what capital R is I think what it does is it basically lets you just type a bunch of letters yeah so capital R and then I just press a bunch of letters and just see how it's eating the words there all right let's undo that I never used capital R really I don't think I really ever use it I use a lowercase R if I want to replace a couple of things I think you can also this is useful with like a number so let's do oh so I just replaced three letters by doing 3r and then whatever letter you want so I wanted to replace let's say we want to replace this with the three threes so I can say three are three so replace the three tells it how many letters to go R says you're replacing and three tells you what you want let's do three sevens three are so cool so that is a replace let's just move some code about and refactor it and I'll show you like some combos that I usually do all right so I'm gonna just go up to the top with Gigi and then I want to delete this block this comment because I don't want it so I press the J to go down command V we're sorry I keep saying command V I mean just uppercase v shift B and then I'm gonna move down I move down with a curly brace and then press delete to delete my comment I'm gonna say shift G to go up or sorry to the bottom of the file now I'm gonna say K and shift B we're gonna go up with a curly bracket and I'm gonna delete this chunk GE to go to the top P to paste capital o to put a line above all right so I'll just go through that combo one more time so what I wanted was to move a chunk of code from the bottom of the file up to the top of the file so the way I do that was capital G to the bottom K to move up a couple lines then shift V to highlight the code that I wanted I went up with a curly brace move down with J so I'm just selecting the chunk that I want I didn't talk about this but if you didn't want to delete this you can press Y that I'll put it to your clipboard you can delete it with the D as well if you want to so I'm just going to highlight that again and press D so either way if you press Y it'll keep it there D will delete it then we're gonna go up with G and then up here we're gonna paste and then capital o is gonna give us a line all right now I'm just going to type out some code so export cons to-do list and this is just me coding right so now I'm inserting my code and so now I'm like I want to put a closing parenthesis at the bottom of this chunk of code so I went back into command mode with xscape and then I'm going to do right curly brace I'm gonna press o once I put my head my cursor here to insert a line of code that I'm not closing and closing curly brace there as well and had escaped out now let's say I'm not happy with the way this is indented all right so let's say I want to indent this over I can do carrot and you believe it's carrot twice and I'll dents it over now I can command V and select a chunk of code so I did command V then I went down a block of code with curly brace and now I can say a single carrot that way it moves it over now I believe I can also specify how much to indent like if I want to do three indents I can three and carried over three all right okay it's three lines of code not indenting it three that way I use prettier to autoformat my code so I actually never indent code anymore but if you wanted to indent code that's how you do it arrow and we can go at the left direction with left arrow capital V J to move down you can move it back and forth oh this is another use case for the period that I used to do like let's say I want to move it over I can press period to indent it again all right good undo so it just reinvents but yeah I never used the indention anymore because prettier just formats my code I'm using an auto for matter all right so % shows me the closing currency see the closing one there and then I can move it over alright so that was some misc stuff okay so I mentioned that the period command was my second favorite now we're gonna get into my favorite thing about them and that is macros so the period allowed us to replay a single command so for example I have my cursor here I do capital C type something I can come back to another place where I want to do the exact same thing and it's gonna replay that command with the period now let's say I want to do more than just the last command let's say I want to do several commands in a row that's where macros come in they allow you to record your keystrokes and then replay them again so let's see this in action so I'm gonna come down to the bottom of the file here I'm gonna Center it I'm gonna press o to drop myself into insert mode the line below and we're just gonna add a little space here okay so here I'm gonna say cons are this equal to and then I'm just gonna have like on a right here one two three so a lot of times I like to use macros when I need to refactor a bunch of code and they're very similar now I'm gonna hit command V I command capital V hit why to copy P to paste and I can paste this a couple times so we have some good amount of stuff to work with here now what I'm gonna do is I want to run the same command on each row basically so what I want to do is I'm gonna start my macro by saying Q and then I'm gonna press a key that I'd like to save recording to so in this case I'm going to save the recording to W so now I'm going to say tilde because I want to basically capitalize the first letter I'm gonna push H to go back and then I'm gonna wrap this in single quotes so I say single quote escape capital a to go to the end of the line then I'm do till day then I'm gonna do a comma I'm gonna escape go down a line 0 W to be at the beginning and actually know what I'm gonna delete this line I do DD and then we drop down to this line and then went in the recording alright so basically what I did is a sequence of commands I did some stuff in insert mode capitalize the first letter I wrapped it in single quotes I deleted the line under it so then I can replay this macro on any line of code that I want so let's say I want to run it here I can do shift and then I do shift to or the @ sign and then capital W to replay the command so capital W was the key that I saved it to so you can save several macros at the same time by saving them to different keys so now I can replay it by doing at W right and I can replay this macro a couple times and it's gonna play it through and press you to read to undo it and now what oops turn my sound off again I'm going to copy this oops I don't know what I press some random buttons I don't know what just happened but what I'm going to do is I reset it there so I'm going to copy this pay so now I have even more lines of code to work with let's say I array array is very big so what I can do is I can say 20 at W and now it's gonna run it on all 20 lines that I just had there so you can see macros are super powerful you can do any of the commands we just talked about recorded to macro and then replay it a bunch so this can be really powerful and you want to refactor code that's very similar and you can run this wherever you want to so yeah so I can go to whatever line I want and run this at W macro now again it's not really helpful on this line so you know it's whatever alright let's go over one more example so I pressed you to undo alright yeah it looked like undid everything let's go to the bottom with add G and then curly brace left curly brace go up okay so here let's say I'll to do another one like maybe I want to turn this into a bunch of records so I'm going to say like let's say I want to turn these into records where I use these two lines so I'm gonna say Q and I'm gonna save this macro to e so I'm gonna press Q and then e to save it to the e key and I'm gonna insert mode and then I'm gonna do curly brace I'm gonna space it and then I'm gonna hit W I'm gonna hit capital a gonna hit a colon gonna space it get hit xscape gonna do down one I'm gonna press zero I'm gonna press DW I'm gonna go up and wrap this in a single quote capital a single quote closing and then a comma at the end and I'm gonna escape go down here 0 W to restart it alright and then I can play this macro a couple times oh wait that's my W macro can play this macro a couple times at E now again there's nothing special about what I was doing during the macro I just did like what I wanted it to do um and then replace those keystrokes over and over again and we can undo this so there you go that is macros and you can really see the power of them they definitely take some time to get used to so play around create a couple macros and see if he and start getting some effects going on and you'll notice the way I set up my macros is I'll line them up so they kind of start and finish in the same place in the next line if I want to do like a repeat thing where they just go Bam Bam Bam Bam Bam all the way down the line and so that they they finish see how I start at the first letter in the word and then I finish at the first letter of the word it allows me to replay the macro again and again and that's where I can do like 10 and then replay the macro like that alright so I've taught you all of the commands and shortcuts that using them so what I want to do now is kind of refactor some code in slow motion and go through some my thought process and so you can kind of see where I'm using which commands and you can kind of review the ones that we've done and get an idea of how you can use them together alright so with that said I'm at the bottom of the file here and I want to just get rid of this chunk of code I don't want it anymore so I'm just gonna say D and then I'm gonna do left bracket to go up or left curly brace and so that's going to delete that code block alright and then I'm gonna press J to go up and see what we got going on okay sorry to go up and then looks like I have this this guy at the bottom here that didn't get deleted gonna do DD to delete them and do a period to rerun the DD command and just trim at the bottom alright so I have this chunk of code here that I would like to bring to the top of the file so what I'm going to do is I'm gonna put my cursor at the bottom and then I'm going to do capital V and I'm just gonna go up with K and then I'm gonna press Y to copy it to my clipboard I'm gonna press G G to go to the top of the file and then I want to paste it in between these two lines so to do that I'm going to bring my cursor right here and then I'm gonna press P which is gonna paste it below and then I want to add some space here and when I add some space here so I'm gonna do shift o so capital o to add space at the top and I'm gonna press escape to go back into command mode then I'm gonna come down here with J no press o to add space down here I'm and hit escape I'm not gonna worry about indenting this cuz whatever now I want to go back to the bottom of the file so I'm gonna do capital G and you saw me just go up with K and I press W to go here I didn't really need to press W you press o to go back out what I want to do is just delete this block right here and we don't really need this render anymore so I'm gonna say D and then up a block D up a block with the left curly brace and what I could do as well is capital V go up and D that's the one I use a lot as well all right so d now I want to move this up as well so what I'm gonna do is I'm going to move you just saw me move that chunk up I just did I want to move these guys up as well so how would I do that is the exact same way so capital V D up paste capital G to go back down so you can kind of see kinder grab both of these this is a more real time if I was doing it I didn't want to be slow all right so just to redo what I just did capital G to go to the bottom big V to copy and then I'm using J and K to move up I could have also done curly brace to move up a block that I'm pressing D to delete it which also puts it on my clipboard do you go back up P to paste it and then o to add some space and then I can just rinse and repeat and pace it there so that's a very common sequence where I'm wanting to take a block of code move it somewhere else so highly recommend getting used to that combo something look to that effect alright capital G back to the bottom now I want to copy this stuff here so I'm going to command V sorry I keep saying command shift V I'm gonna come to the top and now I'd like to add space here escape and I'm just gonna say cons state is equal to u state and I'm gonna do that alright I think I used the a command there so let me show how I did that so I went Const and I should use brackets here alright so this is just me typing a word so I went into insert mode by pressing I that I typed this sentence out then I was done here I don't want to type anymore so I'm hitting the Escape key and really where I want to go is right here and I want to start typing again so what I did is that went down twice and they did capital a to put me an insert mode right here and then we can close that alright and so that was just me typing and then we could use the percent sign to flip back and forth if I wanted to all right so what if I want to make all these functions and okay you just saw what I did there as I went down I think my cursor was here I went down and then I won my cursor to be at the beginning so I did zero to go to the beginning and W to move to the right a little bit so I'm gonna add Const in front of all these functions that I have so I'm gonna say I to go into insert mode Const and then a space and hit escape 0 W period so Peters just gonna redo what I did here okay go down a block by doing the curly brace go down block and here I'm gonna do period again and I can just go through each one and add Const in front of it right so one thing that I just did here I want to show you guys so I went down and my cursor was like here you can also press B will take you back one right and I can do that so I do that sometimes instead of 0w all right so let's do we have that refactored I don't want the this keyword so I'm gonna say D to W so that says delete two words so the first word that I deleted was the this command and then the period was the second word all right so I'm gonna move my cursor down I'm gonna do a dot and that's gonna delete it on these two oops I didn't mean to press that letter so I just lost my place all right so we're back here I'm gonna press D to W to delete those two words do that here do that here this reminds me there's to come the V highlighting things I haven't really showed you guys yet so let's do that real quick so sometimes you want to just highlight in the middle like for example let's say I'd like to copy this chunk right here you see how I'm highlighting not the whole line so shift V there yeah shift V and there you go gets the whole line if you want to get inside of a single line you can press V in that visual mode so that's non-capital V you can see it highlights like this a little bit different and then you can do all the same things we can press Y to copy and I can paste just that or you press D to delete that chunk all right I can press W or B to navigate in the line after I've highlighted another handy one is to be able to select entire like highlight entire column so I'm going to come down to the bottom here going to going to delete this stuff here with capital V moving up in the D and let's go back to our example where we had a Rea real quick so I can show you this one two three you know we can copy this now we have a couple and I press z twice the center here so what I can do is I can say ctrl V and so that what control V does is you can kind of see you can kind of get this effect where you can do a blocks at a time so what can I do after I've done a block well once I have a block here I can do R and then I don't know B and you can see it replaced all those letters with the beat or I can do a block and I could say D and delete them or I can do a block and for whatever reason I can copy it and we can paste it I copy it with Y paste it with P so this this is I would say as an itch one that now and then is super handy depending on the use case I can't think of a vent to me off top my head but something like this where we have a column of things that we need to select I usually use this visual block for and so yeah those are the commands that I'll do with it also there's capital I hello there and then if you hit escape you'll see how it added that at the beginning of each one all right so let's let's go over that real quick what I just did so I've highlighted this line then I did shift I and then I type whatever you want here and then once you hit escape what's gonna happen is it's gonna write that same line everywhere else you selected then I'm gonna press you to undo because I don't want it in control R to redo it because I actually do want it alright so those were the two selects haven't shown you and then V you kind of just select blocks you can press X to delete or we can press D to delete all right like that let's go back up I'm using the curly brace to do that jump blocks at a time all right so I'm pressing W here to go inside the line I think I like what's going on here and nothing I want to change I got forgot to get rid of this another way I could deleted this like it just did X and hold down X right to delete single character at time if I didn't want this let's go back up here I don't want this dot set state so I'm gonna I'm gonna get rid of it but I'm gonna come up to the very top here and again a lot of times what I do to navigate is just hold down a J or a K or an L or an H to move around if I'm being if I'm feeling a little bit more elegant all the jump blocks but a lot of times I just do this because it's doesn't take that long to do alright so I'm gonna come back here so what I just did there too so where I wanted to be is the one my cursor to be here so what I did to get there was I had my cursor like here and then I pressed me a couple times to go back all right so I'm gonna name that set state hit escape and I pressed I to go to insert mode there and I'm gonna just go here and I'm gonna delete this this is also a place where I can like set state and see all the ones I mean and actually let's just let's do a macro real quick to get rid of this in front of all the set states so this is actually something where it's probably easiest to get do with a search and replace but we're gonna do with macro so we can practice our macros so what I want to do is I want to get rid of the this dot in front of all my set states so I can see set state if I hover over it and I push star it's here and it's here and it's here and it's here and it's here and it's there alright so we have about five instances of it and so what I want to do is I just want to try this beginning part in front of it or maybe I want to rename it totally to something else but I named it set state up here so we'll just get your cut the beginning part so I'm gonna press Q and I'm gonna save this to thee let's do the D key and then what I'm gonna do is I'm gonna say DW to delete a word and I'm just gonna press it twice then I'm gonna hit slash set state and I'm gonna press ENTER and then I'm gonna press B twice and then I'm gonna exit my recording that's all I want to do so I'm gonna press Q to quit and save my recording to the D key and so when I replay this what's gonna happen is it's going to delete two words and then it's going to search for the next set state and it's gonna bring my cursor to the right position so @ d @ d @ d @ d @ d alright so I played that a couple times and now we can see this dot set state is just totally gone now if I had like a ton of these remember what we can do is I can do like let's run this five times at the it's gonna run it against all of them so if we see this dot set state we can see this is the only instance right so I did slash the start searching I typed my word and I pressed in to see where it is and capital end go backwards is the only one left and then I can do my command D to delete it and it's gone so there you go that is our macro for that and then let's do this one as well just reminds you guys because this one I spam all the time I don't actually want to do to do the I D is equal to equal to ID anymore I'm just going to so let me do this in slo-mo where did you I have my cursor here MSA C T and then closing parenthesis so change until the closing parenthesis and here I'm gonna say true and I'm just gonna say always true and I want to make that same change down here for this conditional so on a press period and I'll do it for this conditional as well it's gonna just always be true right so here I've decided I don't want this this line anymore I want to replace this entire line with something else so I'm gonna do capital C here and I'm gonna say to do is it's now gonna be equal to five and then we're gonna hit escape and let's go down here I've seen like this whole block I don't really want here anymore so what I'm gonna do is I'm gonna press D and then the % and that's gonna just it's gonna get everything between these two curly braces alright so I just undid it so you can see you exactly what it was deleting it's gonna delete between these two curly braces so delete % it's gonna get rid of that I know you I think that's a pretty good amount of refactoring of code to give you an idea of some of this stuff let's do another C 2 curly brace so C 2 curly brace and then here we're gonna say state dot Bob and so give you a period to run that again maybe I'm just gonna delete until this and I'm gonna copy Bob and I'm gonna paste it here I'll see that sequence so you I did that pretty fast that's a pretty nice sequence that I'll do a lot so okay so what I did here is I want to delete first off everything from here to here so I'm gonna say DT curly brace then I press K to go up then I press V to start copying I'm doing V because it's in line then I can say W to go exactly where I want or I can say T and the curly brace then pressing Y to copy so once I've selected that I can say Y and then I can come down here hit P and we go so let's practice that in fast motion again so delete come up would be copy what we want Y and P to paste so that's a nice one and there you go okay so that is the end of my them tutorial now we only really got into about ten percent of them really because it's just so huge and there's still a lot more you can explore a lot more key binds you can figure out and try out these are just happen to be the ones that I've like memorized and use all the time now I also didn't focus as much on the editor itself and some of the functionality of the editor and focus more on the keybinds because I actually don't use this editor anymore what I use is a vyas code and then I put them emulation inside of this so what I mean is I have a VIN plugin that I have four vyas code and so I do all my text editing in here and you can see I got that little block cursor and I can move up and down with J and K you can do my capital C my you to undo and so on so I have all my shortcuts and vyas code now it's not perfect them actual them over here is actually way better but this is like kind of a compromise because vyas code brings some stuff to the table that's very nice and then one last tip that I want to leave you guys with is rebinding your escape so it can be kind of annoying to hit escape all the time it's kind of an out-of-the-way key so what i do is i rebind it so envious code this is how i do it and if we go to my settings I have a vim insert mode key bindings and I have it bound to JK so what that means is if I'm an insert mode and I press JK that's equivalent of me pressing escape so I have rebound to that so it's very easy for me to go between command mode and insert mode so I'd recommend rebinding it either JK another popular one is JJ and caps lock is the other popular one I've heard people use another thing I was gonna mention just as a fun fact is I actually use Bhim also inside of Chrome or at least I have this vim plug-in called sebum so this is what I like to use to navigate just with my keyboard in the browser so you can see on the right here I'm pressing J and K so k to go up J to scroll down I can do G to go to the top capital G to go to bottom GG top so it has some of the vim key binds and I press F to click on links so you can see I now press the BQ key to go inside there and so this is pretty handy to just navigate website so like now I want to go to about them I'm gonna press R I press H to go back and yeah so that's just a fun note if you're really liking them you can also add it to Chrome if you like and yet there you go that is my vim tutorial if you want to get more resources I highly recommend checking out the vim subreddit they have a bunch of other resources so you can take your vim knowledge to an even higher level
Info
Channel: Ben Awad
Views: 305,113
Rating: 4.9517431 out of 5
Keywords: vim, vim tutorial, vim crash course, learn vim, vim basics, vim commands
Id: IiwGbcd8S7I
Channel Id: undefined
Length: 64min 44sec (3884 seconds)
Published: Thu Aug 08 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.