A table of contents of this series can be found here.
A summary of Vim can be found here.
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)
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.
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]
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.
mapcreates a key map which then is available in most modes
nmapcreates a key map which then is available in normal mode
imapcreates a key map which then is available in insert mode
xmapcreates a key map which then is available in visual mode
The shortcut is the sequence of keys you enter, to execute the key map.
The exection is the sequence of keys that will be executed once you press the shortcut.
You can use printable characters (like
5) for defining the shortcut and execution.
Non-printable characters (like
CTRL) can be used aswell but have a special notation:
||Function keys 1 to 12|
||The Pipe character|
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
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
j we would perform
gg (jump to the top of the file).
Q we would perform
j which performs
This is called recursive mapping and is activated per default.
All the commands I mentioned above are separately available as non-recursive mapping:
noremapcreates a (non-recursive) key map which then is available in most modes
nnoremapcreates a (non-recursive) key map which then is available in normal mode
inoremapcreates a (non-recursive) key map which then is available in insert mode
xnoremapcreates 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:
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
~/.vimrcconfig, 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!