Vim mappings are one way of boosting your productivity in Vim. From simple mappings like “delete current line and insert a new one”, all the way to your very own code snippet that does more than just paste the code. It’s customizable to the point where you could end up automating every other thing in Vim you use on a regular basis.
Vim’s greatest standout from other editors is its Modal-Editing feature. It allows you to work with different modes based on what you want to do. With the help of it, Vim can assign a single keymap that does non-identical tasks based on the mode you’re currently in. One way to assign keymaps is using the nnoremap command in Vim, which is specific to the Vim Normal Mode. And that is exactly what we’re going to learn today.
Avoid using the regular map command
It is best to avoid using the recursive mappings, unless you really need to, which for the most part will not be the case. What is a recursive mapping, though? It is basically a type of mapping which, when applied, will continuously keep on functioning unless you invoke an escape, by pressing Ctrl + c. There are very limited use cases where you may really want them. Recursive mappings only work when you assign them using the regular “map” command. The following example should be enough to give you an idea:
I have a mapping assigned to the letter “o” (just for demonstration. I’m not foolish enough to use it this way, nor are you. So don’t do it). The mapping, as shown, basically deletes the current line and enters Insert Mode on the next line, by running the sequence of “ddo”. At least, that’s how it is supposed to work, unless you really see it. It does not work as intended. It rather deletes everything it finds, because the command it is assigned to itself contains the letter “o”, which redirects to the initial command and executes itself again. That is recursiveness.
Recursion occurred in the previous section due to the fact that we had used the regular map command to assign the mapping. Now there’s one other thing to note. If the command that was mapped to the given letter does not itself contain the same letter, it wouldn’t function as recursive, so you won’t have to worry about it. Take this for example:
:map o dd
The command does not contain the letter “o” itself, so it won’t be recursive. Don’t believe in my words? Why not see it for yourself?
The other way around is to use a different command instead of the regular map command. There is a non-recursive variant of mappings in Vim. Each of these non-recursive mappings are specific to the mode you are in. The most common of all is the nnoremap command, which maps non-recursive commands and invokes them only in the Normal Mode. Now if we try out the same, previous keymapping but this time with nnoremap, see what happens:
Wala! It works just as intended, doesn’t it? Besides nnoremap there are others such as vnoremap for instance, which will invoke the assigned command only while in Visual Mode. Now here’s the cool thing about it. If you assign the same key to a different command with vnoremap, Vim will be able to differentiate among them, and invoke the commands based on the mode. Say, you have assigned the following command with the letter “o” to be executed in the Visual Mode, besides the already assigned one with Normal Mode:
:vnoremap o <esc>yypo
While this does not resemble any real use cases, it should at least be able to demonstrate the usage. Now look at the following gif to ensure that it works:
Difference between nnoremap and the rest
The main way how nnoremap differs from other similar commands like inoremap and vnoremap is that it is specific to the Normal Mode only. If you have assigned a mapping with the nnoremap command with any given key or sequence, it’ll only be executed in the Normal mode, and nowhere else.
This is very important, as already shown in the previous section, to be able to assign totally separate commands to a single key or sequence. As Vim is primarily a keyboard-biased text editor, it tries to utilize all the keys available to give you the most out of it, without needing to have your hand go all the way to the mouse. Isn’t that sweet? It’s as sweet as sugar!
vim nnoremap vs noremap
A lot of people tend to confuse on this one. Let me help clear things out! Just like we’ve seen previously, the map command will assign a keymapping that is independent of the mode you might be in. Let’s say, you created a mapping like this:
:map <A-o> <esc>dd
Now if you press Alt + o in Normal Mode, you’ll see that it works as intended – it’ll delete the current line. But it will not be limited to the Normal Mode. Whatever mode you might be in, including the insert Mode, if you press Alt + o, it’ll carry out the function. However, this universal mapping scheme will only work if the specified keys are not already assigned using modal-specific commands like nnoremap or imap, or anything else.
Now coming to the real deal, noremap is basically the same as the map command – it’ll assign the mapping globally which is independent of the mode. The only difference between noremap and map is that map is a recursive mapping command, as already mentioned in the previous sections, and noremap is a non-recursive one. If you compare noremap with nnoremap, the difference would be that nnoremap is only specific to the Normal Mode, while noremap isn’t. Quite clearly understandable, isn’t it?
vim nmap vs nnoremap
Nmap is pretty much like a brother to the map command. It is also a recursive mapping just like map, but it is specific only to the Normal Mode in Vim. By the way, Noremap is the same thing as Nmap.
Now’s the cool thing to know. Both nmap (noremap) and nnoremap commands assign mappings that’ll only work in Normal Mode, but nmap (noremap) is a recursive mapping, while nnoremap is a non-recursive mapping. This means that if you assign a keymapping with nmap (noremap) and the command you assigned to contains the same letter as the mapping, it’ll function like a recursive keymapping, as shown in the beginning of the article. Cheeze.
A few examples of nnoremap
Well here are a few, basic examples of how Vim Power Users usually assign a keymapping with the nnoremap command. It can range anywhere from simple tasks all the way to long chains of cool functions you could combine together to automate stuff. Here are a few that I use myself:
Open the Netrw file explorer with the F2 key:
noremap <F2> :Lexplore<CR>
Print an html template from a template file:
noremap <F4> :r ~/.config/nvim/templates/html<CR>
The next one is a very cool example I found from “Learn Vimscript the Hard Way” by Steve Losh, and yet a very, very advanced mapping. It is an amazing book you should check out yourself.
What the mapping does is that when you place your cursor on any word, and press <leader>”, it surrounds the word with double quotes! Here’s the command:
:nnoremap <leader>" viw<esc>a"<esc>bi"<esc>lel
You should now have a pretty clear understanding of what nnoremap is and what it does. Make sure to avoid using the recursive command whenever possible, unless you really know what you are doing and want to use it for real. If you are like me (who doesn’t like to mess around much), you would probably want to avoid it. Also, note that the noremap command will assign a key-binding that is independent of the mode you are in. Whatever command you assign to a key using noremap, it will work across all modes. It is best to avoid this one as well for the most part. Yes, you’ll need to type a bit more if your command is the same across all, but at a later time, it could be easier and a lot more hassle free to assign a different command for a specific mode. Mark that, will ya?