The Beauty of Vim

I work with vim and its fabulous. Although I’ve been a casual linux shell user for some years, I’ve never had to get to grips with vim as my main editor which I’m using eight hours a day until now. And I love it.

Cheat Sheets

Its vital to get a good cheat sheet to start with. This is like a menu of commands to remind you how to do things. Then when you think “wouldn’t it be cool if this program did …”, you can look up how to do it (and I guarantee vim has the feature you wanted, whatever it is. Some of my favourite tricks not always listed on cheat sheets are:

gv reselect your most recent selection
% when on a bracket (either ( or { ), jump to its partner

For more cheat sheets, probably best to look on my del.icio.us page (here and linked in left hand bar) as I keep my favourite links of the moment updated there

Colour Syntax

I have finally managed to get my vim working with colour highlighting which is making my life much easier (and prettier, of course). I’m running vim 6.2 on AIX 5.3 and found that the only way to get my vim into colourful mode was to turn on the syntax and set my terminal to dtterm.

To change your terminal type, at the prompt type:

export TERM=dtterm

Then when you do

echo $TERM

it should tell you that your term type is now dtterm. Unfortunately the change in terminal type made my function keys stop working (argh – see my earlier post on this topic). As a compromise I have aliased vim to set the terminal type when it runs, by adding the following line to my .kshrc file (if you’re running bash then add it to .bashrc instead)

alias vim=’vim -T dtterm’

Arrow keys

I normally use h,j,k,l to navigate in vim (left, down, up and right respectively), but I get stressed by the cursor not wrapping at the end of the lines. I googled for the problem and found that adding this to my .kshrc helped:

set -o emacs
alias __A=$(print ’\0020’) # ^P = up = previous command
alias __B=$(print ’\0016’) # ^N = down = next command
alias __C=$(print ’\0006’) # ^F = right = forward a character
alias __D=$(print ’\0002’) # ^B = left = back a character
alias __H=$(print ’\0001’) # ^A = home = beginning of line

Regex

Regular expressions in vim are more powerful than I can imagine, and I’m loving the find-and-replace, especially because you can use the pattern you matched in your replacement expression by typing \0 as part of the expression. Its so powerful.

For my next trick, I will figure out how to group parts of my pattern to use bits of them in the replacement – I feel a tutorial coming on.

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.