WebDevChallenges Logo
WebDevChallenges

A brief introduction to Vim (Part 7)

Updated June 5, 21
Advanced configurations, key mappings and mapleader in Vim.

About

Here is the sixth (previous) part of this series: A brief introduction to Vim (Part 6)
Here is the eighth (next) part of this series: A brief introduction to Vim (Part 8)

Advanced configurations

Basic configuring possibilities were mentioned in A brief introduction to Vim (Part 3) already but Vim is highly configurable so lets dive into more advanced configurations in Vim.

Key mapping

I will simpify things here, because this topic is huge and complex but if you want to read more, this article is quite extensive.

With Key mapping you can create a shortcut for repeating a sequence of keys or commands.

The general syntax for creating a key map is: [command] [shortcut] [execution]

Commands

Vim has the possibility to create key maps which are available in most modes but it is also possible to restrict them to certain modes.

  • map creates a key map which then is available in most modes
  • nmap creates a key map which then is available in normal mode
  • imap creates a key map which then is available in insert mode
  • xmap creates a key map which then is available in visual mode

Shortcut

The shortcut is the sequence of keys you enter, to execute the key map.

Execution

The exection is the sequence of keys that will be executed once you press the shortcut.

Available characters

You can use printable characters (like a and 5) for defining the shortcut and execution.

Non-printable characters (like CTRL) can be used aswell but have a special notation:

  • <BS> : Backspace
  • <Tab> : Tab
  • <CR> : Enter
  • <Enter> : Enter
  • <Return> : Enter
  • <Esc> : Escape
  • <Space> : Space
  • <Up> : Up arrow
  • <Down> : Down arrow
  • <Left> : Left arrow
  • <Right> : Right arrow
  • <F1> - <F12> : Function keys 1 to 12
  • <Insert> : Insert
  • <Del> : Delete
  • <Home> : Home
  • <End> : End
  • <PageUp> : Page-Up
  • <PageDown> : Page-Down
  • <bar> : The Pipe character
  • <C-n> : CTRL+n

Example

Now we know enough about commands, shortcut and execution to create a key mapping. Lets say, we want to create a key map which is available in normal mode, it should save (write) the file when we press SPACE followed by w.

The command for this would be: nmap <Space>w :w<CR>

Recursive and non-recursive mapping

Now lets say we have the following two key maps:

:nmap j gg
:nmap Q j

By pressing j we would perform gg (jump to the top of the file). By pressing Q we would perform j which performs gg.

This is called recursive mapping and is activated per default.

All the commands I mentioned above are separately available as non-recursive mapping:

  • noremap creates a (non-recursive) key map which then is available in most modes
  • nnoremap creates a (non-recursive) key map which then is available in normal mode
  • inoremap creates a (non-recursive) key map which then is available in insert mode
  • xnoremap creates a (non-recursive) key map which then is available in visual mode

That means that if we add the following third key map: :noremap W j

And then press W, only the action j and not gg will be performed.

Defining a Mapleader

Now if you want to create your own key mappings it is an option to always start with a certain key (I for e.g. use the Space-key).

You can define a printable mapleader in your configfile (typically ~/.vimrc) by adding the following line: let mapleader=" "

A non-printable mapleader can be defined like this: let mapleader="\<Space>"

Now that we have defined a mapleader we can use it in a key map. For e.g.: :nnoremap <leader>w :w<CR>

Which will write the file if we press SPACE followed by a w in normal mode.

The advantages of using a mapleader are:

  • It is easy to change the leader on one place in the config file
  • If you want to try a few mappings from someone else’s ~/.vimrc config, you can use your own prefered mapleader but simply copy their key mappings (if they use a mapleader)
  • Many Vim plugins use the mapleader to create their own mappings - these mappings will use your configured mapleader (if you have one). This can also be a disadvantage because it might interfere with your already existing mappings.

Ask me questions

I will be happy to answer your questions in the comments section below. Also let me know if you have any tips or ideas for me to improve my post!