Keys worth remapping in Vim

Let's look at some key basic key remapping

If you're into IT there's a fair chance that you've at least heard the name Vim. If you're using Linux it's almost certain that you already have it installed. You might even have needed to manually close your terminal emulator after opening Vim by accident. This article won't help you with going back to shell, but instead will guide you through some key mapping conventions worth considering in your setup.

You have been warned!

Before dos it's better to share some don'ts. The main thing you shouldn't do is remapping most non-alphanumeric keys unless you use the same terminal app on every machine you use Vim. An example: I had difficulty getting insert, delete, home, end, shift-tab to work in Konsole and at least some of them in xfce4-terminal. Secondly, you should be careful when mapping keys that are in use in vanilla Vim and learn their functions or remap them to something else. Type :help <key> before remapping. Lastly, the function keys are generally not a good idea. They might interfere with some system settings or Vim plugin settings, notably Vdebug. There's nothing they can do that leader key combinations can't.

The great escape

Probably the first thing you'll feel you need configured is your escape key. By default you have two options: the Escape key (duh) and Ctrl-[ combination. The first one is uncomfortably out of reach and the second one disrupts your hjkl movement. There's also Ctrl-C but it doesn't do the same thing in every circumstance as the regular escape, for example in mass insertion from visual block mode. Many people prefer to use double j instead, but it makes it daunting to type multiple j's which you might need to do in some languages. Instead, I recommend using Ctrl-K or Ctrl-J. They have a benefit of not moving your right hand off your basic navigation, so the only difference between them is the finger you'll be using. They also don't have any significant role in Vim, unless you're using digraphs heavily. The only downside is that I had to unmap the one I wasn't using because it was too easy to missclick.

Lost in space

One of the best normal mode keys to have remapped is the spacebar. Its default behavior is just advancing cursor by one character and it's perfect for continous pressing. Because of this you likely want it for something that can be undone by pressing it again, like on-off switch for a common task or cycling through file marks. Personally, I use it for moving to the next window, which normally is achieved with double Ctrl-W. It works so well that I rarely ever use any other method of navigating multiple vim windows, like Ctrl-W + hjkl. A bonus behavior is to precede a spacebar press with a number, which will cause Vim to focus the window assigned to this number.

Vimium compatibility

Vimium is a Chrome extension that allows you to browse the web using Vim-like set of key bindings. There are two keys you need to get configured in order preserve your sanity when using both Vim and Vimium at the same time - J for :bprev (previous buffer) and K for :bnext (next buffer). This way you'll have consistent keys for buffer / tab switching in both, they are also very comfortable to click. As for their default behavior - J is for joining lines which is quite useful so I just remapped to leader-j combination, but K runs system man on current symbol which for me is reduntant. By the way, by default Vimium uses D for page down and U for page up, which is not vim-compatible. You might want to get that fixed in your Vimium configuration.

The Ex mode

I'm willing to bet most people only enter Vim's Ex mode by accident by pressing Q. Unless you ever used Ex you presumably won't find it useful, as single Ex commands can be issued in normal mode with a colon. Personally I haven't found any good use for it, for its mapping on the other hand... it serves me better by splitting with a new terminal window.

Pick your poison

Using leader key sequences is a great way to set more shortcuts in Vim than would normally be comfortable. It's very hard to use them by accident and by being sequences they can be typed with one hand (which is problematic with some Ctrl combinations for example). For my leader key I use comma, which is widely referenced as a good choice for it. Remember that a combination can have more than just two keypresses in it. This way you can group sequences to make them more memorable, like having all filesystem bindings under <leader>s (mnemonic: fileSystem).

Realm of possibilities

This article described the reasoning behind some of my choices and preferences in a great piece of software which is Vim. I think everyone who wants to learn using this editor should start with (almost) clear configuration file and add settings gradually to suit personal needs while having fun in the meantime. I've been using mine for two years (and three months of real usage - as a main editor) and this journey has only just begun.

Comments? Suggestions? Send to
Published on 2019-07-23