Table of Contents

VIM - Leader

vim allows a set of keys to be mapped as an alias to a different command in the ~/.vimrc file.

For example:

:nnoremap -d dd

NOTE: This maps that when -d is typed, it actually runs the dd command.

In this example,it would delete the current line.

  • :nnoremap: denotes normal mode.

Why not just map d to dd. Surely this would save typing a keystroke.

  • This could be dangerous, as just typing a single character could delete data.
  • As a safety precaution it is best to have to type this extra character.
  • But what if in future you want to change this prefix character?
  • One way is to manually modify each alias. Alternatively, use a leader.

Use a leader

:let mapleader = "-"

NOTE: Sets the leader to a hyphen.


:nnoremap <leader>d dd

NOTE: Defining the leader in one place makes it easy to change later.


Local Leader

Vim has a second “leader” key called local leader.

:let maplocalleader = "\\"

NOTE:

  • \\ is used, and not just \ because \ is the escape character.

The <localleader> can be used just like <leader>.


Help

:help mapleader
:help maplocalleader

References

https://learnvimscriptthehardway.stevelosh.com/chapters/06.html

https://vim.fandom.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_1)

https://vimhelp.org/syntax.txt.html