Want to see the full-length video right now for free?Sign In with GitHub for Free Access
Hello! This is Laila Winner from thoughtbot here with a short introduction to mapping commands in Vim. In this Vim screencast I'll talk about recursive and non-recursive key mapping, and how to limit a key mapping to a particular mode
First, let's review what key mappings do: They "create a shortcut for repeating a sequence of keys or commands". The basic structure of a key mapping, as shown on the screen, is first the command - then optional attributes - on the lefthand side, the keys that will be mapped, and on the right hand side, the sequence of keys being mapped to.
I'm going to define my key mappings in a file called mappings in my .vim directory, which I've included in my .vimrc using the source command.
As you can see, I've pulled up my system's keyboard viewer so you can see what I'm typing.
Let's start with an example of the map! command, which works in insert and command-line mode. I'm going to map
:map! <F5> 'Hello world'<CR>
When I close and re-open Vim, my new key mapping will be available. So now when I enter insert mode and type F5, 'Hello world' is inserted.
map works in normal, visual, select, and operator pending modes. Now I'm going to map "Leader comma" to echo 'Hello world'. The leader I have set up in my .vimrc is space.
:map <Leader>, :<C-U>echo 'Hello world'<CR>
As we see after closing and re-opening Vim again, typing "Leader comma" echoes 'Hello world'.
map and map! are recursive. Let's look at an example that demonstrates the implications of this.
When I map "Leader semicolon" to "Leader comma", typing "Leader semicolon" also echoes Hello world.
:map <Leader>; <Leader>,
noremap, by contrast, is non-recursive. I'll demonstrate the effects of this by building on the previous example:
:noremap <Leader>` <Leader>,
Although I've mapped "Leader back tick" to "Leader comma", because I'm using noremap, typing "Leader back tick" does not echo Hello world.
As we know, Vim is a modal editor. So it makes sense that you can define mappings that work only in certain modes, or across all modes. noremap works across all modes. To make it work only in normal mode, we would prepend the command with an 'n'.
Let's say we want to map the key combination Shift-V to echo 'Use insert mode'.
:nnoremap <S-V> :<C-U>echo 'Use insert mode'<CR>
When I'm in normal mode and I type Shift-V, I see the message 'Use insert mode'. When I enter Visual Block mode, however, and then type Shift-V, no message is displayed, and I enter Visual Line mode.
As you might guess from the nnoremap example, key mappings specific to each mode correspond to the first letter of the mode's name. So the insert mode map commands, for example, are imap and inoremap. The exceptions are the visual mode commands, which are xmap and xnoremap.
One last thing worth mentioning is that you can view all your key mappings by running the map command in Vim.
I hope you learned something new from this Vim screencast. If you have any questions or comments about mapping commands in Vim, please take them to our forum at forum.thoughtbot.com.