Today, I’m sharing with you my ~/.vimrc, and some of the gotchas I’ve learned to live with over the years;

My indentation level is four-spaces-to-a-tab — but there’s so many projects out there that have their own. How do you manage?

Well, the first four lines of my ~/.vimrc read as follows:

set shiftwidth=4
set tabstop=4
set expandtab
set background=dark

The last line sets the color theme to one that is legible better on dark backgrounds (like mine), which speaks to the colors I’m about to use next — but pick your own.

Five more lines:

highlight ExtraWhitespace ctermbg=red guibg=red
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()

This very clearly emphasizes any trailing whitespace — that is to say, in bright red. But not while you’re typing.

Another practice is to keep lines below a certain length. Usually (in Python for example), this is somewhere around 79 or 80 characters.

highlight OverWidth ctermbg=darkgray guibg=darkgray
autocmd BufWinEnter * 2match OverWidth /\%80v.\+/
autocmd InsertEnter * 2match OverWidth /\%80v.\+/
autocmd InsertLeave * 2match OverWidth /\%80v.\+/
autocmd BufWinLeave * call clearmatches()

You could also do:

autocmd set colorcolum=80
highlight ColorColumn ctermbg=black guibg=black

However, this changes copy/paste by selection with the mouse behave differently from xterm. A triple-click is still a line, but becomes a line with however many spaces are necessary to get to column 80.

And then, we get to my Cyrus IMAP bits (line continuations for legibility);

autocmd BufNewFile,BufRead \
    /home/*/devel/src/cyrus/cyrus-imapd-2.5.git/*/*.{c,h,testc} \
    set tabstop=8 softtabstop=4 shiftwidth=4 list \
    listchars=tab:>. noexpandtab

autocmd BufNewFile,BufRead \
    /home/*/devel/src/cyrus/cyrus-imapd-2.5.git/acsite.m4 \
    set tabstop=8 shiftwidth=8 noexpandtab

autocmd BufNewFile,BufRead \
    /home/*/devel/src/cyrus/cyrus-imapd-2.5.git/configure.ac \
    set tabstop=8 shiftwidth=8 noexpandtab

Cyrus IMAP 2.5 (and before) had a special indentation policy, but did not (still does not) include the vim-specific configuration in its source code files:

  • Each “odd” (i.e. first, third, etc.) indentation would be 4 spaces,
  • Each subsequent “even” indentation would be a tab with a width of 8, replacing the 4 spaces.

So yes, I have two copies of the Cyrus IMAP GIT repository — one to work on master, and one to work on -2.5.

Furthermore, my default indentation policy conflicts with the markup of debian.rules files used in the OBS, which needs to be treated as if they’re Makefiles:

autocmd BufNewFile,BufRead \
    /home/*/devel/osc/*/*/debian.rules \
    set tabstop=8 softtabstop=8 noexpandtab