5

I am trying to create a mapping that has 2 functions:

On 1st use: redefine itself to be used

After: execute the newly saved mapping

The workflow could be :

<leader>r -> map <leader>r dd

<leader>r -> dd

<leader>r -> dd

...

So I came up with this mapping :

map <Leader>r :nnoremap <Leader>r

And I get :

<leader>r -> :nnoremap r

My leader key is \<Space> and so a space is inserted and now I can trigger the mapping but with only pressing r.

Do you know a method to "escape" the leader key in a mapping ?

2 Answers 2

3

Inside a mapping, escape < with <lt> (and | with <Bar>). The following example shows a working demo of your idea:

:nnoremap <Leader>r :nnoremap <lt>Leader>r :echo "hi"<lt>CR><CR>

PS: You should use :noremap for the initial mapping, too.

0
4

From reading the Vim source, it would seem that all occurrences of <Leader> in a mapping are replaced by the leader character, you can't escape this. From term.c replace_keycodes() (abbreviated):

if (strncmp(src, "<Leader>", 8) == 0) {
    p = get_var_value("g:mapleader");
}

I only did a quick reading, so perhaps I missed something.


The easiest workaround I can figure out is using a character code to "hide" the <Leader> part from this piece of code, which seems to run before the code that replaces the character codes:

map <Leader>r :nnoremap <Char-0x3c>Leader>r

<Char-0x3c> is the hex code for the < character.


Note I didn't know about the <Char- syntax either by the way, I found it by typing :help key-mapping; at the top of the page there's an index and the "Mapping special keys" and "Special characters" sections looked interesting enough to skim, which is where I found this ;-)

1
  • thanks for your answer and for the link to the documentation :)
    – nobe4
    Commented May 1, 2015 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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