Lesson 22 of 26
Edit and source your .Vimrc faster
In this lesson
Roy creates two leader mappings to make changing the vimrc faster: leader ev opens the vimrc in a vertical split through a special variable that always points to the vimrc file, and leader wq writes and quits it and then sources it. He tests them by adding a mapping that opens NERDTree when the leader is pressed twice. He also shows that a mapping removed from the vimrc stays in memory until Vim is restarted.
Transcript
[00:07] (external link, opens in new tab) It's become faster changing and sourcing our vimrc file so that we have less pain to keep changing it continuously as we work through things. I'm going to type commande and then my vimrc which is a special variable that always points to the correct vimrc file. And here if you remember we have a map leader which is our comma and I'm going to do a new mapping to open the vimrc file in a split and then I'm going to create another mapping to
[00:39] (external link, opens in new tab) Save the vimrc file and source it. So one mapping mapping one will open the vimrc file in a new split. mapping two will write and quit it and then source it. So these are the two mappings I want to create. So let's do that.
[01:10] (external link, opens in new tab) And norm map. So normal mode, no not recursive map. When you press leader and for me I'm going to type EV for edit vimrc file. I want that to map to a vsplit, sorry, vsplit and my vimrc variable and of course pressing enter at the end. And the second one, I'm going to copy this line and paste it right here and say leader.
[01:46] (external link, opens in new tab) Let's change it to right quit is basically not going to be a regular right quit. I'm going to type like comma wq and that will do the following. It will write quit, which is what I want, but then it's going to press enter, of course. And then it's going to say command
[02:17] (external link, opens in new tab) Source. Sorry, I have to do like a right quit here now that I forgot this. So, we're going to type right quit and press enter. Then, we're source my vimrc and press enter. So that's it. Now hopefully what's going to happen that if I'm going to I'm going to close this just once and I'm going to open Vim again. Now let's see what happens when I press leader EV. Leader EV and we have the split.
[02:48] (external link, opens in new tab) So let's change it. Let's add something here. Let's add a new mapping that will that we'll know definitely works. So, let's map a new leader to NERDTree. I want NERDTree to appear when I press leader twice. So, I'm going to copy this line and I'm going to add another one another shortcut leader leader and delete word delete word delete word and repeat with dot
[03:22] (external link, opens in new tab) And I want that to map to NERDTreeToggle as well because today if I press leader leader nothing happens. I have to press controlN but with this line active I should be able to see NERDTree just by pressing leader leader twice leader twice so let's save this again with our new shortcut which is this one leader write quit so leader WQ and I don't know if you've noticed but right at the bottom here we had a source of MRC that happened so let's see if that actually works I'm going to type leader leader and there we have our NERDTreeToggle.
[04:01] (external link, opens in new tab) Let's do that again. Let's try to open our vimrc leader EV. And now we can let's say delete this. Delete this line and leader right quit. And we had let's see if that works any that still works. Now the reason that still works is because the old bindings have not been removed just because they were removed from vimrc. They're still in memory. But if I close Vim again, sorry, I'm going to just quit because there's nothing to write and I open Vim again, the mapping doesn't work.
[04:37] (external link, opens in new tab) And technically, I could just say unmap, but I don't want to do that in my vimrc file if I don't have to. So that's one way you can get really more productive and much faster by editing your Vimrc file with special commands. Command leader EV leader right quit for example.