Neovim - ~25 Custom Keymaps set with Lua

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be talking about setting key maps in neovim uh so this will be the second video in the neovim from scratch series if you want to follow along with everything i'm doing you can head over to lunarvim neovim from scratch and you can check out the o2 keymaps branch and you'll have the exact same setup that i have here feel free to follow along with your own config it's just a pretty simple video for the most part so if you're just here to learn about key maps you don't really need to have all this stuff if you don't if you don't want to all right so let's talk about kind of where we are right now so the video before this one if you don't already know what init.lua is if you don't already know what the lua directory does i recommend you check out the video before this one where i explain what that does and what those two uh what that file in that directory do so for now let's open up lua user key maps okay and you're gonna see a file with a bunch of key maps and i'm just gonna explain all these key maps and kind of show you how to set up your own keymaps as well alright so to get started you can see that i have different sections for different uh modes right so we have like insert mode some some mapping for insert mode some mappings for normal mode and i'm just going to go through you know some of these mappings and explain what they do all right so let's just start with the better window navigation mappings um this is i think a pretty common thing that people kind of said it's a pretty i've seen this as a pretty common uh key remap so let's uh create a split window here and let's talk about how we used to have to move between splits before uh setting this key map so to move up i would have to do control w k to move down i'd have to do control w j and same for left and right i'd need to do control w uh h to move to the left and ctrl w l to move to the right instead of doing that what i can now do is just ctrl k to move up ctrl j h to move left and so on so forth right so now i can more easily move between uh windows and i just think this is like i don't know a simpler way to do that since i find myself moving between windows pretty often and not really using what these uh what these mappings used to be right so remember that when you remap something you're kind of overriding what that mapping used to be so i recommend looking up what these keys do before you just kind of like override everything and then you end up with some kind of like super non-standard key key sets and key maps right all right so that's what these better window that's what i just think is better for window navigation and those are some options that i have set or key maps i've set so let's talk about the options that i have passed so i have a function here called keymap which takes an n the n is uh for normal mode this is the key that we're going to be mapping and this is what we're going to be mapping it to so let's also talk about well let's first talk about the function here keymap this is shorthand for vim.api.nvimset keymap so instead of typing that out every single time i want to do a keymap i just kind of shorthanded it to this now to make it simple i put keymap but you could just make it k if you wanted to or even shorter than that right but yeah we basically don't want to type this out every single time we want to you know set a key map so we just do this shorthand all right let's talk about options too so no uh remap here actually stands for no recurse map you're basically you basically just want to set this i'm not going to really explain too much about this i don't want to really confuse anyone but uh basically for the most part this is the way you're going to this is an option you're going to want to pass pretty much every single time and then also silent so that we don't actually see an output for our map right so it'll be you can you can experiment with changing this like if you set silent equal to false you can see that sometimes you're going to see like actually something show up in the output down there all right um so that was window navigation explaining the key map function here and explaining the options let's talk about your leader key this is kind of like a really important key this is maybe the most important key and what your leader key is this is essentially a key that you're going to press before pressing a bunch of other keys to open up a ton of different custom key mappings for yourself so i use space as my um as my leader key and you can see that this is basically how you're going to do that you're just going to remap space to a no op and then we're just going to put a literal space as our map leader and local leader i don't really play around with like a local leader like i don't i don't really like use that as a concept really and uh in vim i basically just think of this as just my regular leader is space and i just use that so now let's talk about this mapping here it's for normal mode and we're going to say leader e and where leader now is the space character right so whenever i press space that's actually leader and so what happens when i press leader and then e um we actually run the command uh lex and then 30 and then you see the cr in these angle brackets here and the cr stands for carriage return and what a carriage return is is just literally pressing the the enter key right so let's talk about that before we actually show the leader i'm just gonna do lex which is actually shorthand for lexplor which is like left hand explorer right so we're just gonna do lex and then we're going to do 30 for the size of the explorer and then again carriage return is just pressing enter so i'm just going to press enter now okay and now i open network w over here which is an explorer to the left right and we can do the same thing just by running it again we'll toggle it now what we can what i'll explain here now is that now if i press space which is my leader right and then e i'm going to run this command so i'm going to press space e and space e and i can kind of toggle this explorer so i'm just going to open up another file in here just to show you what it does all right so let's open up our options file and do that and then maybe we want to come back and then open up our keymaps file again all right so you know experiment with some other commands you can just come down here press colon to go into command mode you press tab and you can see all of your commands everything um that basically is a command you can kind of experiment with uh maybe some mappings that some leader mappings you might like right a lot of plugins will also expose a ton of commands for you to use and we're going to go over those in future videos but yeah you know just like experiment with setting like leader f equal to something or leader b equal to you know something buffer related or something like that um i usually like it to be kind of like mnemonic right like if i um i don't know if that's the right word but like if like leader e is for explorer like if i did leader b it would be for buffers if i did leader f it would be for files and so on so forth all right so now let's talk about resizing it's kind of the same deal so this is a command right resize minus two or plus two and then cr is carriage return so these are commands that you can actually run but for resizing would you really want to go down and run that command every single time or would you rather just like kind of uh press a key to do it right so we have control up down left and right to resize you can see i can easily resize things if i jump over here i can kind of resize up and down as well so yeah um let's see actually i'm noticing too now that actually this this this looks a little off to me so what i think actually up should be would actually be plus and then down should actually be minus so that's something that i just caught right now and that's actually how i think that actually should be so i'm actually going to update that now okay navigating buffers so a buffer is kind of like if you're coming from another text editor you probably can think about buffers more kind of like it's not exactly correct but you can kind of think about them like tabs now them already has another concept for tabs but i don't want to get too deep into it right now if we do b next right like this and then i press enter we're going to go to the next buffer right and you remember that i opened up this options file earlier and now we can kind of you know go between different buffers right so i'll do b next again there's another empty one i'm not really sure how that one got open but so and so forth right now we can also move between them now with uh the s stands for shift so what this will actually be is capital l and this will be capital h right so now i can move between different buffers just with capital l and capital h alright so let's talk about this insert mapping so you can see now we have an i for insert and so if we're in insert mode and we press j and then k really fast we're back in in normal mode so what i have jk set here equal to is just escape so usually you have to press escape to go from insert mode into normal mode well instead of that if you just press jk pretty fast you'll just go back in so now you don't have to reach you know all the way with your pinky to escape or something like that maybe you haven't mapped the caps lock which i actually recommend too but um you know jk is just like a way to kind of do that really fast now also you'll notice that like it's not exactly perfect so like if i do uh like j it takes a while to like actually insert the j key right so that's something that you also should be aware of is like that's not always uh ideal but it's not gonna like slow you down or anything so like if i start typing like j and then start typing things you're usually typing fast enough to where you won't actually notice that all right so visual mode um this is for indents so if you want to press like an angle bracket here to indent to the right it'll actually hold on to that for you what then usually does is um is it'll just do it once and then you won't be in this kind of like tabbing mode anymore right so this kind of just holds on to that for me and i i personally like that you could if you wanted to you could probably go like this and press dot or something actually i guess not but like i guess there's there's i'm pretty sure there's other ways to do it but um this is the way that i like to do it i like that it just holds on to it and then just does this all right so let's talk about moving some text i'm not gonna undo this all the way there let's just move it back in so let's talk about moving text uh so a is for the alt key so now if i press the alt key and i press well if we're going to be in visual mode first right so we're in visual mode here like this and if i press the alt key plus j or k i mean i'm just going to move all my text up and if i press the alt key and j i'm going to move all of my text down like this right um that's pretty useful i've seen people like kind of ask for that a lot and it's something that you don't know you really want it until you kind of start using it so yeah so that's i think a pretty cool key map um let's talk about this uh this what what is this right what is this what am i remapping p to in visual mode right um what i'm doing here and this is the last one i'll talk about i'm not gonna really go too much for this is basically the exact same thing as the uh thing i just did earlier a second ago in visual block mode but um what i'm doing here is it's kind of subtle so for instance imagine i yank a key map here right and so now i paste keymap so if i then go and highlight ops and then i paste keymap on top of it what neofim actually does is it then kind of like yanks ops and then if you go to paste again what would normally be in that register would be ops and i don't like that behavior at all that really like it kind of annoys me i like to just hold on to what was uh what i what i'm still pasting right so that's kind of um that's kind of like how i i don't know i just like that better so this is how you would achieve that so that's this that's this right here it's kind of a little confusing to understand it kind of you would have to understand like registers and a few other things but just know that that's what this key map does and maybe i'll go deeper into registers in the future um but yeah so so just just imagine that if like you pasted over opps and then in your pasting from then now on you would have ops in there instead of keymap right so we wouldn't want that or at least i wouldn't want that personally so that's why i have this mapping there all right and the other ones are just like terminal mappings to move between windows kind of like the same thing and then like i said this visual block mode is basically just the only thing i'm doing with this is the exact same thing so like if we are like this and then um like with capital k and capital j just like that right okay so um now i think that's pretty much everything i wanted to talk about when it came to like actual key maps we're just going to talk to really quickly about the init.lua file so if you remember from the last video i required user.options um in this video we're just we're going to kind of do the same thing you'll notice this will be a theme throughout a lot of the videos is now we're just going to require user.keymaps so where is that it's in again lua then user and then keymaps.lua you'll notice that we don't actually again have to put um we don't actually have to put like uh lua.user.keymaps.lua you don't need to do that you can just do user.keymaps just like that and that'll require it so if you didn't want these key maps or you just thought something was messed up with your keymaps or something like that and wanted to test out not having any of your keymap set you could just comment this out and then restart any of them all right and i think that's pretty much it i don't think there's really much else i really wanted to show about basic like key maps and stuff like that obviously you can get a lot more complicated with this stuff and we will in the future when it comes to plugins like actually mapping setting up maps for particular plugin functions that are given to us or commands that are given to us and also way later on in the series we're going to talk about which key which is kind of like this but with like a menu situation as well all right so yeah that's pretty much it um make sure to check out this repository here and you can go under it it'll be on the master branch as well as the key maps branch but if you go through here and you look for let's see where is it keymaps.lua you'll find all of the keymaps that i showed you in this video um but yeah that's pretty much it if you want to support my work here and you want to support the series and all you can support me over on patreon or github sponsors make to make sure to like and subscribe and i'll see you guys in the next video
Info
Channel: ChrisAtMachine
Views: 1,467
Rating: undefined out of 5
Keywords: chrisatmachine, linux, neovim, crypto, vim, free, rice, arch linux, tech, technology, unix, os, operating system, code, programming, software, open source, tutorial
Id: 435-amtVYJ8
Channel Id: undefined
Length: 15min 35sec (935 seconds)
Published: Mon Dec 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.