What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in Vim?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Vim, one of the most powerful features is its ability to be customized through user-defined key mappings. These mappings allow you to define your own shortcuts for commands, making your workflow more efficient and tailored to your needs. However, Vim offers different types of mapping commands: remap, noremap, nnoremap, vnoremap, among others. Each serves a specific purpose and understanding their differences is crucial for effective customization.
Understanding Vim Mapping Commands
1. remap
The remap command in Vim is not a mapping command per se but a setting that influences how other mappings behave. When remap is turned on (which is the default), Vim allows recursively remapping keys. This means that if you remap one key to another, and that second key to a third, pressing the first key will ultimately trigger the third key's action.
For example:
With remap enabled, pressing j will act as k, and since k is mapped to l, j ultimately behaves like l.
2. noremap
noremap is used to create non-recursive mappings. This is generally safer and more predictable than remap because it ensures that the mapping will do exactly what you've defined, without being affected by other mappings.
For instance:
Here, pressing j will only result in k. The mapping of k to l does not affect the behavior of j due to the use of noremap.
3. nnoremap
nnoremap is a mode-specific version of noremap that applies only in normal mode. This is particularly useful because it prevents your normal mode mappings from affecting other modes like visual or insert mode.
Example:
This mapping saves the current file (:w) when the space bar is pressed, but only in normal mode.
4. vnoremap
Similarly, vnoremap is a mode-specific mapping for visual mode. It guarantees that the mappings you set apply only when you are in visual mode.
Example:
This ensures that pressing F5 will sort the selected lines only in visual mode.
Practical Considerations
Understanding when and how to use each of these mapping commands can significantly impact your efficiency and effectiveness in Vim. Use noremap series commands to ensure that your custom shortcuts don’t unintentionally trigger other mappings. Mode-specific mappings (nnoremap, vnoremap) are particularly useful for ensuring that mappings make sense within the context of the current mode, thereby avoiding unexpected behaviors.
Summary Table
Here's a quick reference table summarizing the key characteristics of each mapping command:
| Command | Recursive | Mode | Usage Example |
remap | Yes | All modes | Influences other mapping behavior. |
noremap | No | All modes | :noremap j k (j becomes k in all modes) |
nnoremap | No | Normal | :nnoremap <Space> :w<CR> (Maps space to save file in normal mode) |
vnoremap | No | Visual | :vnoremap <F5> :sort<CR> (Sort lines with F5 in visual mode) |
Conclusion
The choice of mapping commands in Vim can immensely affect how effectively you work with this editor. By understanding and applying the right type of command, you can ensure that your Vim environment is both efficient and predictable, tailored to your specific needs. Use non-recursive mappings by default and rely on mode-specific mappings to keep your configurations clean and context-appropriate.

