| by Arround The Web | No comments

What is Vim Leader Key

In Vim, the leader key is used to create shortcuts and execute commands. The slash (\) key in Vim is the default leader key, but you can change it to suit your needs.

I preferably use Vim to edit my documents, and its many undiscovered features always amaze me. The leader key is one of them. If you are a Mac user, then you must be aware of the command key, which can also be referred to as the leader key. The functionality of the leader key in Vim is quite similar to Mac’s command key.

Purpose of the Leader Key

You must be aware of the Vim key mapping feature; it lets you set custom shortcut keys to activate various Vim commands and functions. But many keys already have some kind of command line functionality, so you cannot set them as shortcut keys. This is where the leader key comes in handy.

The leader key is a prefix key added before another key to map a shortcut for a Vim functionality. Many plugin developers also use the leader key for their plugin shortcuts.

Using the Leader Key

To use the leader key, first press the leader key (\) and then the mapped key or command. For example, if you have mapped <leader>s, you can run this using \+s keys in the Vim NORMAL mode.

It is important to note that after pressing the leader key (\) you will have only 1 second (1000 milliseconds) to press the other key or type the command. By default, in the Vim window, you cannot view whether or not you have pressed a key.

But you can view the leader key in the Vim window and change the post leader key pressing time duration, thanks to Vim customization. To show the commands at the bottom of the Vim window, place set showcmd command in the vimrc file.

set showcmd

To change the time, use timeoutlen=[value], where the [value] is in milliseconds.

timeoutlen=[value]

For example, to set timeoutlen to 2 seconds, use 2000 as value.

To learn more about showcmd and timeoutlen use :help showcmd and :help timeoutlen commands in Vim.

Changing the Leader Key

The backslash (\) is the default leader of Vim, but some users find it inconvenient. The leader key can be changed easily, and normally a comma (,) key is preferred for the leader key.

To change the Vim leader key, open the vimrc file and place let mapleader=, for legacy versions of Vim.

let mapleader=","

If you have Vim version 9 or above, you can also use g:mapleader=,.

g:mapleader=","

Here, g is a prefix used in Vim to signify the global context.

Note: Even if you have the Vim version 9, you can still use the legacy way of defining the leader key.

After making the changes, save the vimrc file by pressing the shift+zz keys or typing :wq command.

This is how you can change the Vim leader key to a comma (,), but you can also change it to any key, depending on your personal preference and workflow.

Mapping Keys with Leader

To map a Vim functionality with the leader key, use map <Leader>{key} [Command/Function] syntax:

map <leader>{key} :[Command/Function]

Let’s map the line numbering functionality with <leader>n keys.

nnoremap <leader>n :set number<CR>

Place the above command in the vimrc file and save it.

The nnoremap indicates the non-recursive mapping for the NORMAL mode, <leader>n is the leader and custom keys combination. After the colon (:), any vim command or function can be placed to be mapped. The <CR> indicates the carriage return or the Enter key.

Now, whenever you press the ,+n key in the Vim document, the line number feature will be enabled.

To map a Vim Script function, first, create a Vim Script function in the vimrc file and then use the call command with the function name.

nnoremap <leader>m :call ToggleMouse()<CR>

Moreover, many plugins come with long commands; you definitely want to create shortcuts for them. I recommend using the leader key for that.

Local Leader Key

Vim comes with another leader called the local leader, which is similar to the leader but buffer-specific. For instance, if you are working on a certain type of file with specific settings, and you want a separate leader key for that file, you can create a secondary leader called a local leader.

Just like the leader key, it can also be set to any key. To set the dash (-) key as the local leader, add let maplocalleader=”-“ in the vimrc file.

let maplocalleader="-"

The sole purpose of the local leader key is to define shortcuts for specific file types. For example, The NERDTree is a well-known Vim plugin to explore files. To open the NERDTree file explorer, you will not like to type the entire :NERDTree command. Well, get the assistance of the leader key noremap <leader>n :NERDTree.

Using Multiple Leader Keys

You can define multiple leader keys in the vimrc file. But using multiple leader keys depends upon how you define them in the vimrc file. For example, you have set two leader keys, one is comma (,) and the other is period (.). All the mapping under the comma (,) defined leader key will use it as the leader, and all mapping under period (.) will use it as the leader.

let mapleader=","

nnoremap <leader>m :tabnew

let mapleader="."

nnoremap <leader>m :tabclose

The ,+m keys will create a tab in Vim, while .+m will close it.

Below is my final vimrc file configuration after making these changes.

Conclusion

Vim leader key is a namespace for user-defined or plugin-defined shortcuts for Vim. Backslash (\) is the default key in Vim, however, it can be changed using the let mapleader=[somekey] command. While local leader is another leader key Vim which can be served as a file-specific leader key. Moreover, you can have multiple leader keys defined in the vimrc file. Overall, the leader key is a Vim feature to enhance its functionality and can be used in various ways to be more efficient with the workflow.

Share Button

Source: linuxhint.com

Leave a Reply