0

I'm a vim's newbie. I'm trying to do some remapping in vim.

When i use:

map <leader>cp <Esc>:CtrlP<return>

nmap cp :CtrlP<return>

It does the same thing.

I know what <leader> is. But i want to know the different between normal mapping and mapping with <leader> mapping.

So what is the different between them? When should i use <leader> for mapping?

1 Answers1

2

You said you know what <Leader> is but to be sure I'll just say it again: <Leader> allows you to define an alias to a keyboard key that you can use in your mapping. The default value is \ but you can change this key with mapleader. For example:

let mapleader="\<Space>"

Allows you to use the spacebar as your leader.

After that let's take a closer look to map and nmap:

  • map map creates a mapping which exists in 3 modes: Normal, Visual and Operator-pending modes.

  • nmap creates a mapping which exists only in normal mode.

( see :h map for more details and the other possible commands like vmap, imap etc...)

Now about your two mappings:

  • map <leader>cp <Esc>:CtrlP<return> This means that when you are in normal mode, visual mode or operator pending mode if you press Leader + c + p you will go back to normal mode thanks to <Esc> and call the function to open Ctrlp

  • nmap cp :CtrlP<return> Whereas this mappings means that when you are in normal mode, pressing c + p will call the function to open CtrlP.

So when you say:

It does the same thing

I guess that you tried in normal mode to press Leader + c + p and also c + p which both opened CtrlP. This is the expected behavior since you mapped both key combination to the same action and I suspect that you did so in the same vim session whitout cleaning the previous mapping.

If you replace your second mapping with nmap cn :CtrlP<return> you'll see that c + p will not open CtrlP, Leader + c + p will open CtrlP so will c + n.

Finally to answer the title of your question "When should I use <Leader> mapping in vim" I'd say that it depends on your workflow I'm not sure you can give a good answer (that is to say which isn't purely subjective) to this question. Just try to find what suits the best to your needs.

Bonus Also for your future mappings consider using noremap, nnoremap, vnoremap etc... which disable the recursive mappings and can save you a lot of trouble.

Bonus 2 For your vim related questions you might be interested on the vim stackechange site which is pretty reactive (at the time I'm writting this answer 95% of the questions have an answer)

Community
  • 1
  • 1
statox
  • 2,827
  • 1
  • 21
  • 41
  • " If you replace your second mapping with nmap cn :CtrlP you'll see that c + p will not open CtrlP, Leader + c + p will open CtrlP so will c + n. " What is your mean? – Trần Minh Phương Jul 27 '15 at 16:15
  • I used `cn` as an example it could have been any other key combination. My point was: you now have 2 mappings, `c+n` which opens CtrlP and `Leader+c+p` which also opens CtrlP. And now `c+p` (without `Leader`) doesn't exists anymore so it doesn't open CtrlP. I'm not sure what you didn't understand, did I answer your question? – statox Jul 27 '15 at 16:27
  • Oh ok i've got it. Thank you bro! – Trần Minh Phương Jul 27 '15 at 16:28
  • You're welcome :-) if you think that my post is good enough to answer your question you might want to accept it with the green tick under the vote buttons (or wait for another answer of course). – statox Jul 27 '15 at 17:00