Transcript from the "Files & Navigation - Remaps" Lesson
>> All right, so we've now learned how to move around inside of NETRW. It's pretty nice, right? We did a little bonus extra learning with some window movements, fantastic. But honestly if you wanted to explore, do you think you'd want to press :Vex every single time to open up a file explorer every time you wanted to do it.
[00:00:21] My guess is no, no you would not, no, and why? Because it's inconvenient, it would be a lot of typing, it's five characters just to open up a file explorer. So, if only there was a way that this could be easier, if only a way we could shorten it up and make it the way we want it.
[00:00:42] Wait, we can, it's vim, right?. So let's jump back and let's all open up our vimrc together. Now this is where it's gonna get a little bit goofy, but bear with me, it pretty fun, it's pretty fun. This is where you really start to take control of vim.
[00:00:57] And this moment right here is gonna help you use plugins, it's gonna help you personalize vim, it's gonna help you do a bunch of things that you want to do. And you really should get into this mentality where every time something feels inconvenient or you don't like it, you make it the way you want it.
[00:01:15] All right, so let's go all the way down. Remember, you can just press g to go all the way down to the bottom, or you can press j a whole bunch, or I guess I could have pressed 8j gone all the way down. Let's press o to insert a new line, hit an extra enter.
[00:01:28] There we go, we're rocking. And now I'm gonna type in a couple items. And this is gonna get a little confusing for a second, but I'll explain them. The first thing I'm gonna do is called a mapleader, all right. And I'm gonna just set that to a blank space.
[00:01:39] The next thing I'm going to do is going to be a remap. I'm gonna go nnoremap. And I'm gonna go <leader>pv :Vex<CR. Now that is a lot of stuff I just got done typing all at once, and you're probably thinking, okay, what are these mean? You might be able to start guessing what they kind of mean, but let's go over it one at a time.
[00:02:01] So first, what is a mapleader? A mapleader effectively is you telling them, hey, I have a special key I would like to press and from henceforth it shall be known as leader, right? That means if there's plugins that say, hey, we'll provide you some easy movements by pressing leader k, it will just work for you because you've set that key, the leader key.
[00:02:25] And so I've defined the leader key as a space. So meaning when I press space, I am now pressing the leader key. And that means when I use it in remaps it will be known as that. So it's kinda like a way to name a key and it can be whatever key you want.
[00:02:40] So let's break down this line. This line was particularly confusing, and honestly, I'd even read for like five minutes to figure out what this line meant. I just knew that that's what you did and you copied and pasted all over the place. So let me kind of explain this a little bit more.
[00:02:53] There's actually four parts to this. And so I'm gonna break it up in that sense. And these two kind of go together. So, first, what does the first n mean? Well, the first n means the mode you're in. That means I could have used i, I could have used v for visual mode, i for insert, c for command, t for terminal.
[00:03:16] There's a couple other ones, but for now we're just gonna focus on n, v and i, n being normal mode or the mode you're in where you can move around. So that means I'm saying, hey, it's on this mode. Next, this nore, this nore is actually pretty straightforward.
[00:03:31] It means no recursive execution is how I look at it. That's how I say it in my head or nore. Effectively what it means is that if you don't include this, if we were to remove that part, your remap, the other side, could execute more remaps. Now that may sound a little bit confusing, but it can totally do that.
[00:03:52] And I'll show you here in one moment. This last part, map is just saying, hey, it's a map command. Then leader is a space for me, it could be whatever key you want, I would highly recommend getting used to space. Some people use commas, there's a reason why I don't like using commas.
[00:04:06] Use space, use whatever. And then I do pv. I just chose two letters at random, project view. I want to do project view when I wanna see my file tree. So I'm gonna go space pv to see the file tree. And this is called the left hand side, this thing right here is called the left hand side.
[00:04:24] This is the set of keys I'm gonna press to remap. The right hand side or this side is what will be executed when the left hand side is typed. That means I can replace anything, if I don't like p pasting, I can make p do what I want it to do.
[00:04:40] Don't recommend it, but you can. So, let's look at this. So on the right hand side, I'm gonna go from normal mode. Remember, we're in normal mode, I'm gonna go into command mode. I'm going to type in Vex. And then remember, as I said in the beginning in the abbreviation section, cr is enter.
[00:04:58] So I'm gonna execute an enter. So I'm literally just gonna type the command in and press enter. So, let's test it out. Let's see how this feels. So what we're gonna do is we're gonna source this file. So to source this file, everyone should be able to do this type in :so stands for source, and %.
[00:05:15] Percent sign is a shorthand for saying, my current buffers file. So I'm gonna hit enter. So there we go, we have sourced this file, we've re executed our vimrc and now I should be able to press leader pv. All right, it's open, just opens right for me. It's exactly the thing I want to do.
[00:05:34] It's a lot easier than typing Vex. I can type it super fast cuz I'm very, very used to it as opposed to five characters it's three characters. Three characters is like the maximum optimal length for a remap, anything beyond that, it gets really hard to build up that fast muscle memory.
[00:05:48] But that is like a really nice kind of way to go about things. Now you can choose whatever makes sense for you. You can have leader t, why not? It's yours, don't tell me how to let you live your life, right? But, there are some things that are little bit dangerous about remaps.
[00:06:05] So I'm gonna do something that's kinda fun. But before we do that, let's look at what recursive execution means. So what I'm gonna do is I'm actually gonna delete out the nore and I'm gonna delete the rest of this and I'm gonna type in, let's do xx, we'll remap to yy.
[00:06:22] And let's do that in insert mode, right? And then I'm gonna copy this and I'm gonna say that y equals zz. And I'm gonna say that z equals aa. So when I press xx, it's going to remap it to be yy. And when I press y, it's gonna remap it to be zz.
[00:06:44] And when I press z, it's gonna be aa. So let's source it, oopsies, let's source it right now. So now that I've sourced it, here's one thing that's kind of weird is since I've already sourced a file with leader pv, it still works, leader pv still works because it's still there cuz we haven't restarted vim but it's no longer in there.
[00:07:01] So remember, as you execute, whatever commands you execute, stay alive for the life of your vim unless if you replace them. So now if I go into normal mode or insert mode, which I'm here, if I press y it does aaaa, cuz it did two z's and z's were recursively executed into a's.
[00:07:21] If I do xx, it does all that, it does all these a's, right? But also notice something different, when I press x, it appears after my cursor for a moment, do you see that? That's because vim is allowing you to have a moment to do the remap. You may have remaps, it needs to do so, it doesn't want to interrupt you, but it gives you only a fraction amount of time like a part of a seconds.
[00:07:44] So first I'm gonna erase this cuz that's just. Don't actually do that to yourself that seems like really inconvenient, right? I'm gonna go nore right here. There we go, we're back to normal. I'm going to close down vim for a quick second, reopen up so I no longer have that weird x, y, z, a thing that was going on there, and I'm gonna look at these and we're gonna try something different.
[00:08:06] I'm gonna yank, right, so I just yanked a line right there. Now I'm going to paste it, I can totally paste it right. Well let's just say I use space to move forward. Watch what happens when I press space. Do you see that lag? Why does it lag?
[00:08:21] It's because my leader key is the space bar. It's giving me a brief moment to execute a command as opposed to do its original command, its default command which is to move over to the left one. Space is equivalent to l. l and space do the same thing, the only difference is, is that space is my leader key.
[00:08:42] So it's gonna take a moment to actually execute, which means that if I did space p it would first go up. Is it gonna do v, is it gonna do it? No it didn't. I'm gonna move over 1 and then I'm gonna paste, right. So I did the original command, but it gave me time to press v.
[00:08:57] So there's always a little bit of risk. And if you don't type fast enough with these type of remap, especially on p, because you will end up pasting content you didn't mean to if you're too slow to execute. Or something that's even worse, if you remap something that looks like this, just pv to go into it, let me source this.
[00:09:16] When I go to try to paste something, I'll go paste. Okay, there it is. Wow, that took way too long. It's because I remapped p which is the paste button to do something else, but only part of the paste button. So remaps can be both awesome and you could accidentally shoot yourself in the foot.
[00:09:34] So, be thoughtful. That's why I always have a leader key or a control or something that's a little bit unique for me to press to be able to start the process. Like I don't navigate with space bar. If you navigate with space bar, that's crazy, right, like that's way too inefficient, just I'm gonna walk over here, like you would never want to do that, instead you want to be able to move fast as possible, so that's why I chose it.
[00:09:59] Hopefully everyone liked this little moment. Hopefully it demystified kind of how these remaps work. Cuz honestly, there's just a lot of questions that go on. A lot of people don't realize how simple yet how dangerous these things can be if you don't understand them. All right, awesome. So hopefully the people in the chat and the Zoom call there are a little bit more experienced, just learned something during all this, they're starting to get some momentum here, things are getting a bit more complicated.
[00:10:24] Which is ultimately what we want to do. So we're gonna make some edits to our vimrc, and we're gonna be coding. Now what you don't wanna have to do every single time is that, all right, I've made some edits to my vimrc, I'm gonna quit, I'm gonna reopen up vim.
[00:10:38] Yay, everything's working. So let's add one more remap. Now hopefully, here shortly when I say these things, you're gonna be like remap. Awesome, but for now, I'm gonna keep queuing you until it gets into your brain. So I'm gonna go all the way down. I'm gonna copy, I'm gonna paste it.
[00:10:52] My goodness, see I forgot to take out that leader key. Look how slow that is, it's painfully slow. So let me get rid of all this. Let me put in the leader, right. And now I actually do have to I don't know, I'm gonna reset it just because p was already being used.
[00:11:08] I'm gonna take this, I'm gonna paste it, and I'm gonna make something from me. I'm gonna go, every time I press leader CR, as in leader enter or space enter for me, I am going to go shout out and then I'm just gonna go straight to my vimrc, right?
[00:11:26] fig/nvim/init.vim, there we go. So every single time I press leader enter, I'm gonna source my vimrc. So right now, if I try to do it right now notice nothing happens, cuz I haven't sourced it yet. So let's source it, oopsies, let's source it that one last time. Shout out %, as I always say in my head.
[00:11:47] Now when I press space enter, notice what it says right into my command bar right there. It says so and the path to my init.vim, there we go. I just made something go to my init.vim and do it. So that means if I add something else, let's go imap x yy.
[00:12:06] If do that, I can go space enter and press x. And it should, my goodness, why didn't that work? Hey now, did I forgot to save you? I think it's cuz I forgot to save you, there you go. I did actually just forget to save. There we go, awesome.
[00:12:19] I can now just source it without having to leave or manually type it in or be in my vimrc and use the percent sign. I can just press two buttons and have it all sourced for me. It's pretty convenient, also, I just think it's pretty neat. Cuz every now and then I add my vimrc and, right, I hate to do that.