codegourmet

savory code and other culinary highlights

Vim: Staying on the Base Line

| Comments

Staying on the touch-typing base line (a, s, d, f, ..) is strongly promoted by the Vim movement keys - but :w, ctrl, escape and more tend to distract my hand position. Here are some of the steps I took to deal with this:

  • map , to Leader
  • map Space to ^
  • map Control-m to save all
  • dual-map Caps Lock
  • map jj to Escape in insert mode
  • remap window focus movement
  • map Escape-Escape to nohlsearch

map , to Leader

Most people remap the leader key, so I’ll not lose many words on this:

1
let mapleader=','

map Space to ^

Space is a big key (actually, two keys because both thumbs can use it) and it’s obvious that it should be mapped to something useful/often-used.

There’s a lot of possibilities, some interesting ones to get you started:

  • Leader
  • repeat last macro
  • nohlsearch
  • save-buffer
  • page-down
  • colon

My current favorite is “jump to indent-start”:

1
map <Space> ^

I’m still evaluating binding it to page-down or to :, although I’m pretty happy about getting rid of the ^ key!

Here are two extensive discussion threads on this (via reddit):

map Control-m to save-all

I almost never selectively save single buffers since all my files are version controlled and there’s an undo queue. This binding will eliminate a very common use of the : key, which is straining both pinkies.

1
nmap <silent> <C-m> :wa<CR>

dual-map Caps Lock

Many people map CapsLock to Escape. Other people map it to Control. Both mappings substitute the use of a difficult to reach key.

You can get the best of both worlds by mapping a tap to Escape and a hold to Control, since the control key is only used as a modifier and the Escape key is never used as a modifier.

I’m doing this mapping outside of Vim. Here’s how it’s done in Ubuntu 14.04:

mapping Caps Lock to Control

First, you’ll have to map Caps Lock to Control. I did this via dconf-editor. Note that there are multiple ways to do this, but dconf or gnome-tweak-tool worked best for me.

1
2
sudo apt-get install dconf-tools
dconf-tools

Now navigate to: org -> gnome -> desktop -> input-sources and set the entry: xkb-options to: ['ctrl:nocaps']

Caps Lock: tap is Escape and hold is Control

There is a tool named xcape which does this: “xcape: Linux utility to configure modifier keys to act as other keys when pressed and released on their own.”

It is available on github: https://github.com/alols/xcape

The install instructions provided there worked well for me:

1
2
3
4
5
6
sudo apt-get install git gcc make pkg-config libx11-dev libxtst-dev libxi-dev
mkdir xcape
cd xcape
git clone https://github.com/alols/xcape.git .
make
sudo make install

I added the sudo make install line to have it available system-wide. Now let’s use it:

1
xcape -t 0 -e 'Control_L=Escape'

The zero timeout value is important since else it would sometimes interpret leaving insert mode and pressing another key as a Ctrl-? combination if you’re too quick.

This command is not persistent, so if you have tested it out and like the way it works, append the above line to your .bashrc or .profile file.

map jj to Escape in insert mode

1
inoremap jj <Esc>

This is really cute and it’s been sitting in my .vimrc for quite some time now. But I have to admit that I’ve never used it - I probably should, though, since this will greatly reduce the strain on my left pinky.

remap window focus movement

Moving between windows is awkward with most configurations.

I was using \<Leader>-W as my window command initiator, but this is still a lot of keypresses just for jumping with your cursor.

Using Control+Movement keys to jump intuitively between window splits seems to be a nice idea, but for me this only became viable once I moved the Ctrl key to CapsLock (as described above).

1
2
3
4
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

map Escape-Escape to nohlsearch

This is a pretty new addition to my .vimrc but I started to really like it, since somehow feels related to visual highlight removal.

1
nmap <silent> <Escape><Escape> :nohlsearch<CR>

Conclusion

While one of the most important features of Vim is interruption free touchtyping, some hard to reach keys are a part of many of it’s workflows.

While on the one hand this is not as bad as constantly reaching for the cursor keys while coding, reaching for medium-distance or awkwardly placed keys can put a substantial strain on your hand.

Most of the settings above are fairly trivial, but some of them had a huge impact on my vim workflow.

Happy Coding! – codegourmet

Comments