1

I saw the so called <Leader> in my vimrc quite often. But is there a way to define my own key in a similar manner? I mean I would like to define some sort of <Mykey> and use it further in my vimrc to define more complicated combinations. I mean something like this:

nnoremap <Mykey>u "some keystroke

This means that depending on how I define <Mykey> I get different combinations with it.

1
  • I think what you want is described at :h <plug> Commented Sep 27, 2016 at 6:52

3 Answers 3

6

You can define your own <leader> key with:

let mapleader = "something"

You could also hack the <localleader> to get a second <leader>:

let localleader = "something else"

But <leader> and <localleader> are not special keys at all. Assuming your <leader> is set to , (the example in :help mapleader) the two mappings below are strictly equivalent:

nnoremap <leader>f :find *
nnoremap ,f        :find *

So, basically, you can use any normal key to function as <leader> without setting anything.

3

Vim has this abstraction, and it's <Leader> (and the <LocalLeader> variant which is meant for buffer-local mappings). Unfortunately, there's only one.

If you need a common prefix for a set of custom mappings, and you want to be able to change this in a single location, you can define a variable, and then use :execute to define the mappings.

Example

let g:MyKey = '<Leader>x'
execute 'nnoremap' g:MyKey . 'u' "some keystroke
execute 'nnoremap' g:MyKey . 'v' "another keystroke
1

Here's an interesting usage that I first saw (and wandered about) in Shougo's Unite plugin, which creates an artificial leader key "placeholder" (I'm quoting directly from the Unite's help file):

" The prefix key.
nnoremap    [unite]   <Nop>
nmap        f         [unite]

nnoremap <silent> [unite]c  :<C-u>UniteWithCurrentDir
        \ -buffer-name=files buffer bookmark file<CR>

... (etc.)

In the above example, the whole sequence of characters [unite] is mapped non-recursively in normal mode to do nothing (and thus, it becomes a "prefix"!) Then, a "leader" key is assigned to this complicated <Nop>; in this case, f.

What follows are the actual maps, in which every time you type f <some other char(s)>, Vim translates it to [unite] <some other char(s)> -- if the combination is defined (and typed in time), the map gets executed, otherwise -- the prefix is cancelled (because it gets resolved by itself to <Nop>, i.e. nothing) and it's like you didn't type it at all. Mind boggling.

I didn't test, but I think the way the sequence itself is defined (here, [unite]) is quite relevant -- if I got it right, it will create delays for any key combinations starting with [, [u, [un and so on. So you would always want to use a leader placeholder "string" that starts with characters you don't normally (pun) use by themselves.

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.