VIM hints & tips

My favourite editor is VIM. It's a bit weird if you've never worked with it, but once you get to know it VIM is really the perfect editor. For me at least ;-)

One of the beauties of VIM is that it can be extended and configured to your heart's content. Below you'll find some of those configurations I use to make my VIM experience a better one.

For those who want to see my entire config (and for me when I want easy access to it), I've put my entire vimrc online.

Rewrapping text

If you ever edited text and still wanted to make your lines break at about 70 characters, you know it's sometimes needed to "rewrap" the lines to make them fill those 70 characters again.

VIM has the ideal solution for this. Select a block of text and press gq. Instant rewrap! This even "understands" basic things like dashed lists, indentation of a block of text, and even Usenet quotation marks like '>'.

To rewrap the current paragraph, press gq}. Since this is a bit tiresome, I remapped this to q in my ~/.vimrc:

map q gq}

Switching between windows

If you have multiple windows open, you can switch between them using ^W keycombos. What I wanted was a command that says "Switch to the other window à la ^W^W, but maximize the target window too". This was easily done with the following mapping:

map <c-w><c-e> <c-w><c-w><c-w>_<cr>

Irritating Python comments

Python is my favourite programming language. In this language, proper indentation is incredibly important. The automatic indenter however messes up the comments, since it wants to put the '#' sign at the beginning of the line. This is good for C programmers, but not for Python. To fix this, I set the 'smartindent' variable, and use a little trick:

autocmd BufRead *.py set smartindent inoremap # X<c-h>#

Change directory to the opened file

Often, I want to change the current directory to the one a file I open is in. This is really easily done, but remember that it works on all files - helpfiles too!

autocmd BufRead * cd %:p:h


When run in a terminal, vim (as opposed to gvim) doesn't know when you're pasting. If you paste text that's indented, it'll cause a "stairs effect". To prevent this and other annoying side-effects of pasting, type ":set paste". When you're done pasting, type ":set nopaste".