Vim Tips You Probably Never Heard of

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're gonna quickly learn a whole bunch of vim bindings you probably don't know about um now every key in vim is a shortcut to something it makes everything much easier it's much easier to have a delete button or a change button or find button but we're going to talk about one key on the keyboard that is unique in vim and that is g okay now when you learn vim tutor you will probably learn that you know gg means go to the top of a document capital g means go to the bottom uh and you know i i think vim tutor teaches you other things like g uh you know control g gives you file information down here at the bottom um but in reality g has a bunch of other features other i don't know a bunch of other things that go with it so let's learn some of them so firstly here's one of the big problems if you're a new vim user you think it's a problem it's not actually a problem let's say you have big text like this um one in and you're going up and down with h and or excuse me with j and k one thing you might find weird is the fact that all of this text here is treated as one line so if i'm up you know if i'm up here i go i press j to go down j to go down and i press j to go down again i'm now totally past all that text that is often a little weird for people they don't think of that now there's a reason that works that way in vim uh i will say that so you should use it this way however that might be you might want to scroll through this text as if it is multiple different lines and you do that with gj and gk so for example if i press gj it actually goes through the text as if it is you know different lines or gk same thing right so you can go up and down no problem i've actually seen some people remap j and k to gj and gk you can do that you can put in your vim uh rc you can probably say something like this it might have to be a remap or something like that uh but you can specify that you want to move through lines individually you can do that i don't recommend that what i really recommend uh is formatting your lines properly so they're not like this okay so another let me actually write these down so there's gj and gk and additionally uh there are other things of course in vim you know that there is dollar sign to go to the end of the line and zero to go to the beginning of a line well these also work prefixed by g for example so you know dollar sign goes way back here to the end of the paragraph but if i press g dollar sign it actually goes to the end of the visual line uh same thing if i'm here and i press zero it'll go back to you know this word here but if i press uh g zero it will go to the beginning of the line so you can add you actually have a constellation of different uh uh g shortcuts that will only treat it will that will only acknowledge lines uh visual lines rather than actual vim lines now the most important of all these is uh actually gq though okay and that's because when you are uh dealing with uh long text you frankly just especially if it's like prose text in a markdown or like some kind of markup file you really should not format it like this you should really format it so the lines aren't too long uh and you know they wrap and stuff like that and of course when you compile it it doesn't make a difference it's just more visible uh not just it's more usable than vim it's also more usable in other computer tools so gq that is actually a command that you can put a text object off after so let's say gqueue um i don't know well i'll just say gqq to operate on this whole line and you'll see what did this do well it took all this text and it's made it it's basically returned a bunch so that it is now one big paragraph that's actually multiple lines that are treated as multiple lines uh in vim right so to undo that notice that you know we have uh just one big line gq that has uh formatted it all as one thing okay now of course gq is just a command that you can give any text object so let's say i give g q five down okay it's going to go five lines down and it's going to take these other two paragraphs and it's going to format them in the same way and of course you can use it with i don't know uh select around a paragraph and then press gq same thing as any other kind of kind of vim command okay so that's that and that's very useful i actually have in my vim rc i have norm q gq okay now capital q is uh you know i basically never use capital q someone's gonna tell me i should uh but a lot of people press capital q accidentally and then they're like oh my goodness how do i get out of this mode either way i just actually remap it to gq so i i just like pressing capital q capital q uh to use that uh you know to format text okay all right so next thing with g commands capitalization okay one thing that i think a lot of people want to do a lot in a text file is to change the capitalistic like capitalize something uncapitalize something so here's an example let's say for whatever reason uh well i'll go ahead and give you the commands okay gu means uncapitalize something uh g of big u means capitalize something and these can be followed by any kind of text objects so let's say uh you know let's say i'm here i want to uncapitalize everything from here to the end of the line well i can say gu to uncapitalize and then i can just give a text object let's say the end of the line now gnu linux is no longer capitalized okay or i could actually do see how i is still capitalized here well i could do g u u that acts on the whole line right uh or let's say g capital u uh three words that will capitalize the first three words okay normal just use they use normal text objects it works pretty much how you expect okay or let's say uh g u u to capitalize the whole thing or we'll lower case the whole thing another capitalization you you may know about this there's another binding not related to g but there's a tilde and what tilde does is it switches the capitalization of a particular letter so let's say we just want to capitalize i we can press tilde okay uh or well let's go over here we'll capitalize uh we'll find the next l so gnu linux is capitalized um now additionally i mentioned tilde there's also g tilde and so tilda will change the capitalization of one letter g tilde is actually you you know you can use it as a command that runs on a text object so i can say uh g tilde tilde to act on the whole line or i could do something like g tilde uh you know big word and it will now instead of gnu linux it will be you know uncapital gnu on capital l and then the rest is capital because we're just switching capitalization so again uh gu to uncapitalize g capital u to capitalize and then g tilde if you just sadistically want to change capitalization okay so here's some other stuff just random g commands that might be useful here's one i use all the time uh one interesting thing is that if you have a file name in a vim document and some people will know this some people don't but i have created a file called another underscore file if i press gf while i have this highlighted this will go and open that file so here's my other file yeah this is another file press uh the little carrot thing to return to the previous file so i'm going to do that um so that's a way of moving from file to file and you can also i mean you can even do things like let's say uh it doesn't have to be a relative link it could be a you know a full file name or something like that so let's say etsy hostname uh i can go to there and look that pops up all right so here's my actual hostname file uh so you know that that's just a nice thing to do um and it's very easy when you're i mean it's easy when you're when you're doing some kind of uh uh i don't know actual work frankly it's very useful all right so uh reselecting here's some other things you can i don't know other random g commands when you have uh let's say you select some text okay and you unselect it and maybe you do some other work somewhere else well you can actually at any time press gv and you will automatically jump back to that text that you just selected and it will reselect it and then you can do some more stuff over here you know uh and you can press gv again and it it's going to be selected once again all right so gv that's another nice thing reselect text that can be usable now one more thing is conjoining lines all right now uh one great fantastic uh um uh tool actually the opposite of the thing we're doing up here we were doing doing gq to put everything on different lines you you also might know that there's capital j and that means join lines so this will automatically format a bunch of lines however many you say into one big thing okay let's actually do that the right way um now the thing with j capital j though is that let's say we have all the letters in the alphabet right here well if i press capital j you'll see that it's joining these lines but there's a space in between okay that's sort of annoying and to be clear you can also use that let's say we highlight all this and press j there's spaces in between we don't like that well there's a analog of capital j which is g capital j as you may be able to guess and uh you know what this does is you can highlight it all and press g capital j and that will join the line i'll join all the lines but it will not put a space in between okay now normally you do want that space in between because a lot of the times you are formatting text like this like this pros uh or other things where you want some kind of space uh but you know sometimes you don't let's say you know our alphabet example that's a good that's a good one so yeah and um that's pretty much all yeah that's just a useful thing to keep in mind otherwise you'd have to do like j x j x j x you know what you could do if you really want to just like one or two lines um now lastly um one thing that you do a lot in a file is substituting like substituting a function name or a variable name that you're changing you just want to change it throughout a document but a lot of the times you want to you want to you know check to see if your substitution is actually working first so i'm going to do something here let's say we have uh i'm going to delete all this stuff besides the latin text up here because we're going to use that so i'm on this line and i have the word nebulae um and of course you can run a substitution command let's say i don't want that ae at the end i want it to be the ligature that's like ae together okay so i can substitute the sequence of ae for uh the uh ligature of ae i have an international keyboard so um and then we'll do that you know globally for this line now the substitute command by itself only works on the line that you're on okay otherwise you have to go back into the command change it put a uh a little uh percent sign at the beginning or whatever to make it work on the whole document but uh one nice little command out there is g uh ampersand okay and what that does is it takes the previous uh substitute command that you ran and instead of running it on one line it runs it throughout the document okay so i'm just gonna run g and then ampersand and you'll see some changes in the top well what actually happened is that all of our aes were replaced by ae with the ligature okay so that is a good way like g ampersand it's a good way you can chest test out a uh replacement command a substitute command on one line if that works then you can just press g ampersand and it runs throughout the document okay so that's a fantastic thing all right so this has been a whole bunch of m commands in one sitting now you know a whole lot more than what you knew before and oh you know what there's even more because in vim you can of course type you can actually just type i think help g and that will bring up a list of all the other commands that start with g so most of which i have not talked about there some of them are very rarified but you can learn any more these are the ones that i use pretty often but anyway that's it and i'll see you guys next time
Info
Channel: Luke Smith
Views: 114,262
Rating: undefined out of 5
Keywords:
Id: bQfFvExpZDU
Channel Id: undefined
Length: 12min 11sec (731 seconds)
Published: Fri May 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.