简体   繁体   中英

How do I map multiple <leader> keys in vim?

I'd like to have a left and right hand leader key. If I want both the default \\ and , to be my leaders I thought it would be as simple as adding nnoremap , \\ or nnoremap , <leader> to my .vimrc . But apparently not. How do I do this?

My <leader> is bound to , and this works for me:

:nmap \ ,

All of my leader mappings are now available using either \\ or , as the leader. I think it's the nnoremap that's tripping you up.

You can map one leader key to the other, as in the accepted answer, but if you're going to use <leader> in the first place, you should make the binding to <leader> itself. That way the binding will still work if you change (or remove) the first leader key.

map , <leader>

Note that this still doesn't quite work like a second leader. If the first leader is unset, the binding will still work, but Vim will also revert to using \\ as a leader, since there is no longer an "official" leader (ie. valid value for the mapleader variable). (This wouldn't be a problem for the OP, but may be for others.)

<leader> is convenient but you can create mappings like ,mm or \\mm without using it. Just duplicate all your <leader>something and remap them with ' and \\ directly:

nnoremap <leader>d "_d

would become

nnoremap ,d "_d
nnoremap \d "_d

How exactly would that work? Vim, when it encounters <leader> it replaces it with the mapped key. Were you to use two (keys for <leader> ), how would it know which one to replace <leader> with?

What you can do however, is use <leader> and <localleader> but that's just two separate leaders, not mapping two keys to one of them.

So, no ... you can't.

You can map your shortcuts explicitly though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM