My .vimrc tips and tricks

Vim is my main text editor. I don’t consider myself as an expert in Vim usage and I’m always looking for new ways to use it. At the moment these little config values and tricks make my Vim what it is.

Window handling

Window handling in Vim was quite problematic for me a long time. But then I came up with this little gem:

nmap w <C-W>

It maps key w to sequence Ctrl W. It allows me to cycle thru all windows by just hitting w repeatedly. Other subcommands for w are:

  • n for creating new window
  • v splitting window vertically
  • c for closing window
  • o closes all other windows
  • and arrow keys (hjkl) for navigating between windows

One good command is :sf[ind] which creates new window and finds a file in path and opens it. More commands and explanation how the windowing really works is in :help windows.

Indentation

Indentation, tabs and whitespace usage is the most problematic question after choosing the editor. Over the years I’ve tried several different rules for indentation and currently I’m adjusting my rules based on language, project and the team I’m working with.

But my personal preference still is this:

set tabstop=2
set shiftwidth=2
set autoindent
set expandtab
set smarttab

Basically it means: tabs are replaced with 2 spaces.

With command :retab I can kill tabs really easily. It converts tabs to spaces according to the rules above.

Status line

The statusline at the bottom of the window is handy little tool to show all kinds of information about the file and the character where the cursor is located.

set modeline
set statusline=%F%m%r%h%w\ [\%03.3b]\ [\%02.2B]\ [%04l,%04v][%p%%]\ [%L]

This shows, from left to right:

  • file name
  • is the file modified or not flag
  • readonly flag
  • flag for indicating if the window is for help
  • same for preview
  • value of the char under cursor in decimal
  • same in hexadecimal
  • line number where the cursor is
  • and virtual column number
  • cursor position in buffer in percentage
  • number of lines in the buffer

You can get more choices from :help 'statusline' and customize the statusline to your own liking.

set laststatus=2
set wildmenu

The first one allocates own line for commands and the latter turns on enhanced command line autocompletion which uses the statusline to show autocompletion results.

Encoding

I like my files in UTF-8, thank you. This makes it so:

set encoding=utf-8
setglobal fileencoding=utf-8
set nobomb
set termencoding=utf-8
set fileencodings=utf-8,iso-8859-15

And I hate BOM because it messes some editors, command line tools and such. So it is turned off by default. Also for this very reason I have a rule: first line of all source code files is empty. Also the last line of the file has to be empty.

Line numbering

set number

That was easy.

Syntax highlighting

I once used candycode for syntax highlighting color scheme and can recommend it, but lately I’ve switched back to the stock highlighting rules. It is enabled with simply:

syntax on

That’s all for now. I’ll update this page when something new comes up.

Written 2011-02-09 18:34 and tagged with vim, config.

Ilkka Hakkari

entrepreneur, co-founder and guru at haltu