User Tools

Site Tools


vim:leader

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.

  • This is meant to be a prefix for mappings that only take effect for certain types of files, like Python files or HTML files.
: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

vim/leader.txt · Last modified: 2021/02/15 23:21 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki