Friday, August 24, 2012

Vim tips (especially for the Scandinavian keyboard)

Despite being a Vim user for 9 years I have often felt unhappy about the fact how inefficiently I'm using the editor. I decided to dedicate some time and become more proficient in Vim. Most of the new power comes with the increased use of Vim's basic stuff, e.g. the change command and simply trying to find the simplest way to define what to yank, delete, and where to jump.

Some of the new power comes with better key mappings. This is especially true for Scandinavian keyboard owners like me which is be default unsuitable for Vim. This post is about .vimrc.

The first thing is always enforce use of hjkl for simple navigation by disabling the arrow keys in normal and insert modes.

map <up> <nop>
map <down> <nop>
map <left> <nop>
map <right> <nop>
imap <up> <nop>
imap <down> <nop>
imap <left> <nop>
imap <right> <nop>

Let's note that in Vim hjkl for navigation is usually the most inefficient way.

In Finland the ~ character is behind a combination of altgr, ~, space. That is three keypresses. The ~ toggles between lowercase and uppercase in Vim. The following mapping brings it behind one keypress as it's meant to be.

map § ~ "for Finnish/Swedish keyboard

Often search is the quickest way to define a desired location in the document. Again on the Finnish keyboard the / is behind two keypresses. Search should be behind one press, so it was mapped to space.

map <space> /

Switching between windows should be quicker. I mapped Ctrl with hjkl for window navigation.

map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

For buffer navigation I mapped leader (the comma) with ö and ä. This is again useful for the unfortunate using Scandinavion keyboard, which by the way is not at all optimal for programming either.

map <leader>ö :bNext<cr>
map <leader>ä :bprev<cr>

For switching to paste mode and back I have previously used the :set paste and :set nopaste commands. Now it is mapped to leader pp which toggles between the modes quickly.

map <leader>pp :setlocal paste!<cr>

Finally, some coffeescript friendly configuration. The compilation stuff demands the coffee.vim plugin which everybody has already.

In coffeescript string interpolation is "done like #{oh boy}". The Scandinavian keyboard users' wrists are in pain. The following very warm and fluffy insert mapping injects the #{} in the context, puts the cursor after the { and stays in the insert mode.

imap <leader>§ #{}<esc>i "The § is located left to 1 on the Scandi kb.

Coffeescript programmers need to check the compiled javascript from time to time. I have mapped leader c to CoffeeCompile which opens the current buffer's compiled version in a new split window.

map <leader>c :CoffeeCompile<CR>

For checking the visually selected region's compiled code, I use

vmap <leader>c <esc>:'<,'>:CoffeeCompile<CR>

I don't want to pollute my source code directories with the compiled .js files. The following can be used to change the compile target path.

let coffee_make_options = '--bare --output ~/tmp/coffeecompiles/'.expand("%:h")

The following setting does CoffeeCompile automatically when a .coffee buffer is saved. Useful!

au BufWritePost *.coffee silent CoffeeMake! -b | cwindow | redraw!

Disclaimer! Most of the stuff here has been found from the net from various forgotten sites, so I reject all credit, except for the #{} mapping which me and my co-founder came up with - yes we write a lot of coffeescript every day!