Now that you are comfortable working with plugins, you may want to consider customizing your Vim further by remapping commands to suit your preferences. Plugins are written by many kinds of different people, and everyone's workflow is different. Vim is infinitely extensible, allowing you to remap nearly every action, change certain default behaviors, and really make Vim your own. Let's talk about remapping commands.
Vim allows you to remap certain keys to be used in place of other keys. :map and :noremap provide just that:
- :map is used for recursive mapping
- :noremap is used for non-recursive mapping
This means that commands remapped with :map are aware of other custom mappings, while :noremap works with system defaults.
Let's add a custom mapping to our .vimrc file:
noremap ; : " Use ; in addition to : to type commands.
In the preceding example, we're remapping ; to function the same way : does. Now, we don't have to press down Shift to enter command-line mode. On the downside, we now don't have a command that repeats the last t, f, T, or F (find character and find till character) movement.
We're using noremap because we still want to enter command-line mode, even if : gets remapped to do something else.
You can use special characters and commands in mappings as well, for example:
" noremap <c-u> :w<cr> " Save using <Ctrl-u> (u stands for update).
<c-u> in the preceding example represents Ctrl + u. The Ctrl prefix in Vim is denoted by <c-_> , where _ is some character. Other modifier keys are represented similarly:
-
<a-_> or <m-_> represents Alt pressed with some key, for example, <m-b> would correspond to Alt + b
-
<s-_> represents a Shift press, for example, <s-f> would correspond to Shift + f
Please note that a command is terminated by <cr>, which stands for a carriage return (the Enter key). Otherwise, the command will be entered but not executed, and you will be left hanging in command-line mode (unless that's exactly what you want).
By the way, here are all of the special characters you can use:
- <space>: spacebar
- <esc>: Esc
- <cr>, <enter>: Enter
- <tab>: Tab
- <bs>: Backspace
- <up>, <down>, <left>, <right>: Arrow keys
- <pageup>, <pagedown>: Page Up and Page Down
- <f1> to <f12>: Function keys
- <home>, <insert>, <del>, <end>: Home, Insert, Delete, and End
You can also map a key to <nop> (short for no operation) if you want the key to not do anything. This, for instance, could be useful if you're trying to get used to hjkl-style movement versus arrow keys. Disabling your arrow keys in .vimrc would look like this:
" Map arrow keys nothing so I can get used to hjkl-style movement.
map <up> <nop>
map <down> <nop>
map <left> <nop>
map <right> <nop>