Vim Tips I Wish I Knew Earlier

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
my name is Sebastian and in this video I'm going to show you some tips in Vim that I wish I knew earlier and I'm not talking about how to exit Vim I assume you know this already and they have a somewhat basic understanding of the editor but there are a lot of things that I noticed during the years and also actually quite recently that can make your life easier so let's start right away the first one I want to show is relative line numbers that's kind of interesting so if we start of them what I have here in some sort of um let's do a few lines say we have hello um world so what you notice here my line numbering looks really weird on the left so that's a relative line numbering here it shows the current line but that um all the other lines are not the actual absolute lines so but relatives so why well it's basically you can jump like imagine some Crosshair or something like that if I want to jump up to hello that's six up right so because it's six lines above the current line so one down two down three down and so on and so forth and you can then combine this with you know instead of going up up up down down down and things like that you just say I want to go down to world five down so 5 J in this case and then I'm there or now 7 k for seven up and then I can jump up and down so this is already quite helpful if you're used uh to that or once you get used to that that's pretty cool that that's also nice in combination with a context um sort of environment let's start uh start something like markdown file where we say this is a title and then this is um a subtitle and things like that where I then say I have a lot of lines in between where then let's make this set such that you can see it so now I'm in this paragraph or a subsection and then if I want to jump up it still works with the relatives line number so if I want to jump up to the next section or if you use this in a programming language for a function class whatever I say 99 up and then I'm there at the subsection or all the way up well all the way up is easy but you get the point so that's really helpful relative line numbers are pretty cool in this regard and they help you just uh that you get used to them all right now the second one is how do we actually map Escape or use Escape because depending on the keyboard that you use especially if you use a laptop or then some MacBooks that don't even have a physical Escape key it might be quite difficult to hit this so there are other ways for example to map jnk for the sort of rolling uh where then go you go back to the normal mode but it really makes sense to have an actual escape and what I did uh was just to remap that to caps lock uh there were times when I did this on an operating system level gets a little bit harder depending on the operating system uh it's quite easy on Linux uh but now I used this other keyboard um where you can actually remap stuff on the firm firmware directly so you know you can check out previous videos where I explained that but that's a big one just to use it in an easier way so you don't have to do anything you don't have to change any configuration it's just there and it always works and you can easily access it so I highly recommend that now next one is this Norm or normal mode that you can invoke from a command so why would you do this well it's kind of interesting if we say I have something like um hello world things like that and I would like to uh for example well add something for each and every line so of course I could now uh say and go well uh have something like VAR and then repeat this every time but that's not that quick right but instead I could say well select all of these lines and then colum and do Norm for normal you can already type Norm that shorter but basically normal and then type what you otherwise would press so for example uppercase I uh for insert on the left and then say VAR um space and then escape to uh sorry enter to execute it okay so that works uh quite nicely also with a for a pen something for semicolon or you know uh this type of things I do this quite often uh once I hit multiple uh things or added multiple things so that's pretty cool to invoke the normal mode from uh for multiple line selections if you do the line visual line highlighting and this works really really well so that's a very nice uh tip similar to that and works slightly differently has some Nuance is a vertical visual mode that you can hit with control V that's kind of cool or interesting looks a little bit some like some other frontend editors out there so I can say now I would like to select all of these lines like that um what I can do why would I need this so here for example if I do this and then I hit directly uppercase I I can say do this um well magic for all of the lines what I did before already so without typing this Norm stuff um it it's it's more limited but then it's faster if you do uh the visual uh selection here for the vertical uh visual selection so I can do this again uppercase i and then apply them on all the files it also works oops control V it also works for um appending something but then you need to jump to the back like basically to the last uh character which then you can do with uh just pressing dollar sign and then it works to uh press um uppercase a and then we can say whatever you need and that will be appended to all of these lines as well so that works that's a a shortcut to do this other um appending and inserting to each and every line L that you select that's kind of cool contrl V for um this select it's also helpful what I recently just learned uh for um some more advanced arithmetics so that's a kind of cool tip uh maybe you know that Vim can um calculate can do arithmetics and I was showing this in a previous video before so for example what does that mean if I say I would like to um calculate something uh for example I have my Java list and then I say get zero I can say please increase that number and count it up um or down with crl a Orr X so that works but it also works if we say we have for example a few of these and then I would like to have some variable or whatever extract them and then typically you would say something like this but you see it already gets uh quite cumbersome to count and um press these keys or have a prefix again like five and then control a uh which is a little bit more annoying what you can do you can select all of these lines again and then press G um uppercase uh sorry G contr A if you press control a that was not without the G sorry if you press contrl a just like that all of them are um um added added up incremented or if you press G CR a then they're incremented one by one so basically what it does UMR a jumps always uh to the next one uh so then on each line it jumps to the next one and then depending on uh sort of the previous increment uh they do this accordingly so you have 1 2 3 four five so of course I could do this like that that's typically what we use G um control a and then here we go if you had multiple of these um integers or numbers here in your line then what it can do so again I have to undo this then if I would select it and do this again G then it takes the first one as I've just seen or if I say for some reason I would like to increment this one then again I can use this uh visual vertical select with contrl v and then do the G uh crl a and then I can do the same trick for this one these are just a few things I also learned recently that you can do with the uh these selections like either the normal line selection or vertical selection plus the arithmetics I think that's just uh kind of a cool thing so now that's also something I learned recently and got uh annoyed quite a few times by not knowing about it because that's really helpful we can once we are in the visual mode switch the direction or sort of the the end on which you um select by where basically uh your orange thingy is here the cursor by pressing O So once we're in the visual mode now I can select up here then I can switch down there and I can make it bigger and smaller just how I want to which is really nice because I had this a few times that you start selecting everything at the the start of a function and then you're like oh now I missed one line or something like this and you need to do it over no you can actually just press o make it bigger smaller however you want to do so o changes the selection uh of this here works for both the line selection and also the the normal visual selection so that's pretty cool also what I recently just found out and I should have known this much much earlier is percent uh percent sign for jumping to the matching um well bracket basically like different brackets that it can do so for example if I say I have something like this and this and this that's now not a valid syntax but I can use percentage sign to switch to the next one or to to have that which is especially helpful if we say I would like to have some um some selection or some action with that so for example I want to delete uh everything here in this particular block it could be a function block or let's have some quy uh bracket uh that I would like to delete uh anything inside I show you another uh tip about that later I can say okay now please do something like a visual select to the next one or actually just do a delete to the next one so D and then percent sign that basically you know deletes everything here in inside in between so that is kind of helpful and it's also helpful if you have something like a very long function where where you say okay you could jump here to that function based on some uh paragraph by by jumping with um brackets or you just um go there and say Okay jump uh to the end and then percent sign jump here you don't really see this let's include something here you jump to the beginning at the end of the function with that percent sign so that's pretty cool both for movement combination like a visual selection or deletion or just by jumping around so that's really nice what is also really nice is I and a not for insert and the pent but for a selection with inside a word or inside a paragraph so that's pretty cool um let's undo this again and say something like hello world where we say if I would like to select the whole world the whole word here and the whole world I could go there go back visual select and go to the end of the word sorry that's e for the end of the word and select this but that's rather cumbersome I could also say Okay V for visual select I for inside W for that word so that it looks like following that I say V IW or what you can also um do if I say VA W it would select this but then also with that uh space so it depends what you have it's especially helpful if you don't do it with a word uh but also with something it works with uh um uh parenthesis and with uh uh quotes here if I say hello let's do this if I say for example I would like to select just a hello then I can viw if I would like to select this um the um the quotes I say VI quotes that's inside the quote so VI and then I press this quote or I say VA quote that also selects the quotes here so that's interesting if I have some um surrounding character that I want to select by and this works with the single double quotes with parenthesis and so on uh then this works quite nicely so I can say VI and then I press a parenthesis so now it's inside ba now it's including that so of course I can combine this then with a movement or an action where I say c for substitute that c i everything inside the quotes or anything inside the parenthesis U sorry that was V CI parenthesis and I change this here same for the um uh curly braces same for these other things that's especially helpful if for example they're somewhere on this line and then I say okay please select uh anything inside the curly braces or substitute that or copy it delete it and so on and so so forth so that's really helpful I don't need to search for that or jump around even it will just uh do this and then I can you know delete it or substitute uh same for same for inside the parenthesis and things like that so that's really cool um both that difference with I and a depending on whether you also want to select uh that surrounding character depends on what you do so again these typical uh ones are there another interesting tip is that I don't use that often but it's very good to know is the uh movement uh with the word versus what I always use the lowercase w versus the uppercase W that is well a world a word that is separated with a space so for example if you have something like a um function call then usually W just jumps to the next well the next thing that is delimited by a word or special characters so you know this one uh list Dot is now not a a space of it or if you have you know something like this you would just go almost character by character or word and then plus special characters if I do uppercase W it goes all the way to the end why because then this uh just looks for the next blank for the next um white space which is helpful if I say I want to remove everything here maybe until uh the closing parenthesis I could go something like uh CF and then uh parth Paris brackets where it closes but maybe I have multiple of these and then I already need to uh calculate so then this would leave this one open or I can just say um C and then uppercase W and then this just basically selects or does the action until the next white space or the really end of the word uh these are which are characters without a white space in between so that's pretty cool it's just nice to know uh that difference if to say okay for that uppercase word it it basic basically goes uh all the way there to the next word here or you know CW is very helpful or delete a word d uh sorry I didn't press this DW and that just deletes that whole structure or replaces it accordingly which is really helpful so it's just good to know that difference I'm pretty sure you know about W but uppercase uh W it's also really helpful now another thing that probably you have seen if you followed uh my channel before but I wish I had done this a little bit more with you know more thought because it's really really flexible is to invoke some commands basically anything that you could do on the command line you can invoke from inside Vim uh by just saying well you know you select something and then colon exclamation mark where you can do stuff so let's say I have uh something like a Json object hello world and I can of course invoke something here where I say um bang JQ Dot and I can for example predy print uh that okay so which is especially helpful if I have a lot of these so for example that's going to be just all in one line and then I say please uh pretty print this that's really helpful oops that's really helpful I did something wrong here oh yeah this was not a valid Json sorry for that one two 3 so let's do this again now this is basically uh just uh pretty printed which is kind of cool now we could also um invoke some other commands there are many many things that you can do um another very helpful uh thing is for example to sort something so if I go there bang sort which is just the Unix sort that you can invoke so line by line uh that would just work quite nicely you can combine this uh with some [Music] um unique so for example sort and then pipe unique which then also does this so basically anything that you invoke could invoke on the command line which is really cool because if uh we say well we can take the whole input and do whatever we want we can invoke uh for example some other stuff um maybe you seen this on my YouTube channel I showed how to do for example a title capitalization so this is a title if I say I want to capitalize this almost every word except the a because of well reasons so this just invokes some script here and you can do stuff on the command line there or actually invoke commands which are really helpful and you can invoke pretty much anything that's that's how I do my presentations if I have some title here and and I say I want to have this for example very big as an sart or you know do some other stuff with it it's very interesting to just invoke uh things here also with the contents if you say you do uh some technical writing what I like to do I write uh like to write some ask do here uh and then I would like to uh let's say um have a preview of this so this is now just some sort of asky do formatting um which I can preview I have just a a preview here that does the um asky do compilation opens this up in the browser so I can quickly uh preview that well and go back so this is just a sh scpt that I wrote for that purpose and then it invokes these things you can do multiple steps there um and just invoke this basically from invm from the command line another helpful tip that I should have uh done much earlier is a similar automation that you can do uh with any Snippets so a snippet is something that you can predefine very similar to a life template in your IDE where I say for example please type my name and then this is just much faster which is really helpful you should definitely have this for you know the typical things that that you write doesn't matter if if it's just a normal you know Pros that you write as a text or some sort of technical terms uh that you type and press all the time uh this is really helpful it can be an email signature uh whatever you you always uh type it's just much faster with that but depending on the plugin that you use uh you can also do uh Dynamic things which is really helpful so for example the current date um or you know a current time some sort of Unix uh Tim stamp or a uid whatever you um keep uh doing yourselves you can just you know in the same way sort of invoke a script but already um in in line while you're typing and that's just pretty cool so to do things like that it's just really helpful similar to some boilerplate code or boilerplate you know text uh Snippets that you always keep typing manually this is really really helpful and another thing that is kind of cool that I just started to do recently it depends a little bit in which way you would like to use Vim so for example um if you like to use it more as a well knowledge system or even as in direction of an IDE can be even with plain text but you know to do more than just editing one file at a time you can for example say well I would like to have this uh default file explorer um this net RW or however it's called um that ships with them which is not the the most fancy or helpful one but it's basically uh still possible to just use it and then move around some directory structure that I have here uh this together with using some plugins just for a fuzzy find or a search which I'm using here with this files integration there so for example if I say I would like to um open up a file similar to what you would do in your IDE then I say I would like to open up this particular greeting which I can fuzzy find by just typing this here and then opening it up there especially if you open up Vim in a directory and say then take anything starting from that current uh starting point when you open um up some sort of bigger project for example for a documentation structure that you would like to have in your Vim editor and things like that that's also just helpful this together with some search um that you can do in the same way so if I search for a particular uh structure here then it's just helpful as well uh to do this and then to Jump Around so that is also something that I don't use very often but when I use it it's really helpful because it's much faster rather than to go back to your command line to grab search this manually which is also actually quite fast and and helpful but still once you can stay inside Vim open up a new buffer where you have this file open then already that's just kind of cool to know about these things uh as tips so I hope this was helpful um especially together with some other videos I have on this topic U you know feel feel free to check out these tips this video was a little bit more about the actual movements and typing if you have some other tips I'm happy to know them please put them into uh the the comments down below and also if you're interested in the topic of effective development I have a whole video course on this topic also link down below and you can uh check this out and I hope this was helpful and thanks a lot for watching [Music] bye oh
Info
Channel: Sebastian Daschner
Views: 42,656
Rating: undefined out of 5
Keywords: Vim, Effective Developer, Automation, Keyboard
Id: 5BU2gBOe9RU
Channel Id: undefined
Length: 23min 0sec (1380 seconds)
Published: Wed Mar 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.