The vim documention on how to set the default leader key ( :h mapleader) provides one simple example:
:let mapleader = ","
I want to map it to backspace and tried a bunch of options, this being the first one:
:let mapleader="<BS>"
But nothing seemed to work.
To make it work I had to use:
:let mapleader="\<BS>"
Another option is to not override the default \ leader key, but map backspace to it, effectively ending up with two simultaneous leader keys:
map <BS> <Leader>
Related
After seeing how doom-Emacs use their leader key for almost everything I wanted to implement something similar to my vimrc.
Specifically the " ws" as a s-split is much more comfortable then the default keybinding "ctrl-w s"
Is it possible to map "ctrl" to my leader key (which is " " space)?
Vim won't let you map the Control key individually.
Instead, you can make custom mappings:
nnoremap <leader>ws <C-w>s
nnoremap <leader>wv <C-w>v
etc.
I want to map the <space> key to <leader> (which is currently the \ key here) in VIM 7.4.
I would also like to be able to use both the <space> and \ keys as leaders.
If possible, it would be great to see the / character appearing in the bottom right corner when I type it (instead of funky stuff like <20>), but I can live without it.
I've tried to
nmap <space> <bslash>
this works for simple <leader>keys commands, but <leader><leader>key commands (like the easymotion maps) don't work.
I also tried to
let mapleader = " "
nmap <bslash> <space>
but analogously to the problem above stated, the <bslash> key doesn't work anymore for <leader><leader>key commands.
I already tried a bunch of stuff in these related questions/wiki pages:
Can I use SPACE as mapleader in VIM?
http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-Tutorial(Part_2)
:h mapleader
I can't see your .vimrc, so I can't guarantee this is the issue, but I would bet that the issue comes from using nnoremap. The following works for me:
let mapleader =" "
nmap <leader>i iHello World<esc>
nmap <bslash> <space>
I can use either <space>i or <bslash>i and both of them run the iHello World<esc> mapping. But this:
let mapleader =" "
nnoremap <leader>i iHello World<esc>
nnoremap <bslash> <space>
Does not work. <space>i runs the mapping, but <bslash>i does not, which is exactly what should be expected, since nnoremap is used to avoid nested/recursive mappings. So one possible solution would be to use nmap everywhere. I would definitely not recommend this, since you'll likely end up in a map loop. This solution should work better:
let mapleader =" "
nnoremap <leader>i iHello World<esc>
nmap <expr> <bslash> mapleader
Note that if you change the mapleader setting, this will break because, as :h mapleader says:
Note that the value of "mapleader" is used at the moment the mapping is
defined. Changing "mapleader" after that has no effect for already defined
mappings.
On MacVim, I'm trying to remap leader to ' (single quote), and change ' to \. However
let mapleader="'"
nnoremap \ '
is having no effect after :w and :so %. Nor does let mapleader="," seem to remap it to ,.
Am I doing it correctly? Any help? Thanks!
Changing mapleader only affects new mappings that use the <Leader> special key. So, you need to set this in your ~/.vimrc and restart Vim so that all plugins correctly pick this up.
If I understand you right, you then want to map \ to ', in order to use the jump mark command without the delay introduced by using that key for the Leader now, right? That should be fine.
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.
I see <leader> in many .vimrc files, and I am wondering what does it mean?
What is it used for?
Just a general overview of the purpose and usage would be great.
The <Leader> key is mapped to \ by default. So if you have a map of <Leader>t, you can execute it by default with \+t. For more detail or re-assigning it using the mapleader variable, see
:help leader
To define a mapping which uses the "mapleader" variable, the special string
"<Leader>" can be used. It is replaced with the string value of "mapleader".
If "mapleader" is not set or empty, a backslash is used instead.
Example:
:map <Leader>A oanother line <Esc>
Works like:
:map \A oanother line <Esc>
But after:
:let mapleader = ","
It works like:
:map ,A oanother line <Esc>
Note that the value of "mapleader" is used at the moment the mapping is
defined. Changing "mapleader" after that has no effect for already defined
mappings.
Be aware that when you do press your <leader> key you have only 1000ms or 1 second (by default) to enter the command following it.
This is exacerbated because there is no visual feedback (by default) that you have pressed your <leader> key and vim is awaiting the command; and so there is also no visual way to know when this time out has happened.
If you add set showcmd to your vimrc then you will see your <leader> key appear in the bottom right hand corner of vim (to the left of the cursor location) and perhaps more importantly you will see it disappear when the time out happens.
The length of the timeout can also be set in your vimrc, see :help timeoutlen for more information.
The "Leader key" is a way of extending the power of VIM's shortcuts by using sequences of keys to perform a command. The default leader key is backslash. Therefore, if you have a map of <Leader>Q, you can perform that action by typing \Q.
Vim's <leader> key is a way of creating a namespace for commands you want to define. Vim already maps most keys and combinations of Ctrl + (some key), so <leader>(some key) is where you (or plugins) can add custom behavior.
For example, if you find yourself frequently deleting exactly 3 words and 7 characters, you might find it convenient to map a command via nmap <leader>d 3dw7x so that pressing the leader key followed by d will delete 3 words and 7 characters. Because it uses the leader key as a prefix, you can be (relatively) assured that you're not stomping on any pre-existing behavior.
The default key for <leader> is \, but you can use the command :let mapleader = "," to remap it to another key (, in this case).
Usevim's page on the leader key has more information.
The default leader key is backslash(\). It's used in bindings as a chord with other keystrokes.