Lesson 20 of 26

Mapping keys to actions

Duration: 8:22 Watch on YouTube (external link, opens in new tab)

Markdown version

In this lesson

Roy maps the down arrow to dd and p and the up arrow to dd, k and Shift-P with the map command, so that the arrows move the current line. He explains that map and its mode versions imap, nmap and vmap are recursive, so later mappings, including those of plugins, can change what they do, and recommends the non-recursive noremap commands. He then adds to his vimrc a noremap that opens NERDTree with Ctrl-N (ending with CR for Enter) and normal-mode noremap versions of the arrow mappings, and loads them with source.

Transcript

1,108 words

[00:07] (external link, opens in new tab) Talk about how we can bind keyboard shortcuts to anything we want to do in Vim. In this case, let's assume that I want to have the following functionality. I want to be able to move lines up and down just with a single keystroke. For example, if I wanted to move line five one line down, I would have to basically delete the line which is DD. And now that's right now in the clipboard of Vim. And then paste. And then that will paste it right after this line. So if I wanted to map that to the down arrow key, which I'm not supposed to be using in in Vim anyway, how would I do that?

[00:42] (external link, opens in new tab) So one way to do that is with a command that later I'll show you why you shouldn't be using it. You should be using a different one. But the simplest command we can learn is called map. So I'm going to type colon map and I'm going to map the down arrow. That's how you write special keys into the following sequence of keys. DD and then P because that's what I would type. And that's it. If I just press enter now,

[01:13] (external link, opens in new tab) If I'm moving up and down with J and K, nothing happens. But what watch what happens if I press the down arrow key. Now up doesn't do that yet. So let's map the up arrow as well. I'll do how would you move a line up? Well, you would have to do dd then go one line up and press shiftp like this. So let's try to map that. Map up

[01:43] (external link, opens in new tab) To dd. go one line up which is K and then shift P. And that should be enough. Let's try and do that. So now if I'm moving I'm pressing the up arrow like this, you can see that it's actually moving the line down in the file. So that's pretty neat. So mapping in this in its simplest form is very very simple.

[02:14] (external link, opens in new tab) However, we should not be using the map keyword. And the reason is is that the map keyword is fully recursive. In fact, if I wanted to, I could use this mapping and say these keys only apply in insert mode, in normal mode or in visual mode (external link, opens in new tab) by just saying IMAP, NMAP or VMAP. And if I just say map, it applies to all modes. But again the problem is even if it's visual normal or insert

[02:46] (external link, opens in new tab) It will still be recursive. That means that in if I go back to vim when I mapped up to ddkp if I then tomorrow do another map to the letter d and I'm going to say whenever you type d I want you to do another following set of actions. the map keyword will actually recursively look up all these characters and expand them into whatever they mean. So it will then use those D's as basically two map command calls as well and expand them.

[03:24] (external link, opens in new tab) So I will have weird behavior because I won't remember that I mapped that specific thing. Even worse, imagine that plugins are mapping specific things and they will override or you will unintentionally use their mappings. So, we should not use the map keyword. Instead, we should always be using the noremap basically. So, for example, noremap by default applies to everything.

[03:56] (external link, opens in new tab) If I want to map but again non-recursive so no o no no no no re is not recursive insert non-recursive map normal non-recursive map or visual non-recursive map is basically how I I would go along with it so whenever you see all these things remember they're just mappings but they'll only work in specific modes like only if you're in insert mode can you will that shortcut actually work but the n is the mostant important part because that's the thing that says look even if you see characters that have been defined in other maps just use them as literal characters don't actually expand them into whatever someone else wrote.

[04:39] (external link, opens in new tab) So that's the basic idea and of course we can use those commands and put them in our vimrc file so that they'll always be available just like I did here. I can say for example let's map NERDTreeToggle into controln because I don't want to type NERDTreeToggle every time. So how would I do that? Well, I technically I could just say noremap

[05:13] (external link, opens in new tab) Because that's what I should always be doing if I want to map something to everything but not recursive. And let's say I want to say ctrlN for NERDTree. So for ctrlN you would say open c for control a for alt etc. S for shift. CtrlN will map to NERDTreeToggle. And this is very important. You have to type carriage return which is basically pressing enter because this is what vim will write for you in the command line when you type ctrlN.

[05:52] (external link, opens in new tab) So now if I press enter here and now I try to press CtrlN you'll see that it calls this command dot vimrc oops I need to save this first going to edit I'm going to just pressing up edit home vrc and I add this specific command so I'm going to say

[06:26] (external link, opens in new tab) Noremap. Controll N to NERDTreeToggle and CR because that's carriage return. And I can of course map my up and down arrows. So I can say in normal mode because I only want to do it in normal or maybe even in visual. Let's say normal mode. So normal noremap

[07:00] (external link, opens in new tab) Up. Sorry, let's start with down because it's easier. I want to map that to delete the the line and then paste right where you are right after basically. And let's do the same thing. I'm going to copy this. I'm going to say change word. Let's move that to up and change that to shift C to change from this line on DD and then move up one line and then press shift P because you want to paste right above this will these two will only happen in normal mode.

[07:44] (external link, opens in new tab) So now all I have to do is write and I can also type source for my vimrc like this commando for source and home slash vimrc. And now if I basically try to use my up and down arrows you'll see that it's already working. And if I use CtrlN, it's already working. So that's how simple and easy it is to map keys to specific actions in Vim.

[08:16] (external link, opens in new tab) Next, we're going to look at leader keys, which are a very powerful addition to this.