My version is : NVIM v0.3.0-dev

I'm trying to replace mappings with w mappings. Initially I came up with

nnoremap <leader>w <c-w>
nnoremap <leader>wb <c-w>s
nnoremap <leader>ws <nop>
unmap <c-w>

But it didn't work. I didn't have any mappings with w. After a minute I came up with this:

nnoremap <leader>w <c-w>
nnoremap <leader>wb <c-w>s
nnoremap <leader>ws <nop>
nnoremap <c-w> <nop>

This works as I expect it to. I was just wondering why my previous attempt didn't bear fruit. What's the difference between unmap and nnoremap to ? And what is the convention to this sort of things.

N.B: I want to unmap cause I'm used to those default keymaps, but I'm having pinky issues with ctrl lately. So, I want to train myself to use mappings instead.

  • 1
    If you dont want to obliterate all the window control keys (some of them are quite useful) the trick I used was nnoremap <Leader>w <C-w> – D. Ben Knoble Jun 4 '18 at 13:01

It is important to first understand that <c-w> is not a map but a built-in command when vim is operating in a particular mode. It is often referred to as a "normal command" since one uses it in normal mode1, and it can be triggered using the :normal! command. There is no mapping involved; the normal commands are hard coded in C. If you open up normal.c, you can see the character ^W is to associated with the function nv_window in a large, fixed, data table of normal commands.

Although mappings allow you to override default behavior, you cannot change the built-ins and they can always be accessed using noremap, :normal!, feedkeys(..,'n'), etc. When vim is processing input when mappings are allowed, such as in map, :normal, feedkeys(..,'m'), it first checks if there are any mappings which match, then continues processing normal commands or until an invalid sequence is encountered.

This explains why you cannot unmap a built-in normal command and why you can nnoremap to a <nop>, which is specifically designed for this purpose. Why would you choose nnoremap instead of nmap in this case? Well, there's not a great reason, but the convention is to use *noremap always, unless you really need remapping. A good example of when to use an nmap is those to <plug> mappings in a plugin.

Side note: you might wonder why there are the :map and :map! forms in addition to nmap, vmap, etc. Originally, in vi there were two main modes; (normal) command and insert, so having two forms made sense. However, to write robust code, it is almost always better to spell it out in each mode.

1. At one time normal mode was referred to as command mode and visual mode, but forget about this entirely.

  • Good answer. But you didn't specifically say if my 2nd process (one which worked) was correct or not. – klaus Jun 4 '18 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.