Normal Mode Shortcuts in Vim


2012-06-02

One of the most powerful things about Vim is the ability to assign arbitrary key shortcuts or mappings. This allows you to make your most frequent activities into very fast mappings of just a few keystrokes. For example, I have the movement from the current tab to the previous one assigned to "gr" in normal mode.

You can do this by going to normal mode and entering

:map gr :tabp

and then pressing enter. This tells vim to enter :tabp and a carriage return whenever it sees you press "gr" in normal mode. Otherwise you would have to enter ":tabp" and press enter yourself.

Entering the shortcut in Vim like this will work, but it will only last until you close that instance of Vim. If you want the mapping to work every time you use Vim, you can save it in a file called .vimrc in your home directory. Open this file with

vim ~/.vimrc

and then save that shortcut in it. The next time you open Vim, it will already know about your mapping.

This is a simple normal mode mapping, but it saves keystrokes that take time. You can do all sorts of complicated mappings in normal mode and Vim's other modes. Play around and experiment to see how much you can do with just pressing a few keys.