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)