Joining the cult: richo’s vimrc

This blog post is about vim. As many of you know, I use vim for everything, day to day work, editing config, in depth programming and a few other things.

I’m going to break my vimrc down into chunks and walk you through each one. Feel free to steal portions of this, or even the entire thing if you see fit. I ask that you credit me with the parts I wrote, and that you retain the credits I’ve included for others.

" .vimrc
" $Id: .vimrc 152 2010-12-01 15:41:54Z richo $
" Rich Healey '10

I keep this in version control, with a checkout for my home directory. This makes it easy for me to maintain my setup the way I like it across multiple machines that I do work on.

" This file depends upon a few other bits and pieces. If you're using it and
" it's throwing errors, commend out the blocks that are chucking em.
"
" You will want the following:
" - python_fn.vim
" http://www.vim.org/scripts/script.php?script_id=30
"
" - brainfuck.vim - If you're silly enough to use brainfuck :)
" http://www.vim.org/scripts/script.php?script_id=716

These are the two main dependencies of my scripts. I try to avoid having external dependencies as portability becomes an issue, those ones are also checked into my home directory.

set nocompatible
set nohlsearch
function ReTab()
%s/\t/ /g
endfunction

This is a helper script for fixing old vim files It also comes in handy with PHP, generally a pass with ReTab and then gg=G is enough to make anything readable.


filetype indent on
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
\ | wincmd p | diffthis

This is a quick hack for showing what we’ve changed. It doesn’t work properly with a scratch buffer, but can be a better option than :wq, svn diff, vim file etc etc..


command LongL call LONGL()
function LONGL()
if !exists("b:long")
let b:long="matching"
let w:m1=matchadd('Search', '\%<81v.\%>77v', -1)
let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)
else
unlet b:long
:call matchdelete(w:m1)
:call matchdelete(w:m2)
endif
endfunction

Highlight lines longer than 80 chars. I just flick this on if I’m writing documentation or emails, generally.

set foldmethod=marker

Code folding is amazing. I stick with the default of {{{ to open a fold and }}} to close a fold.

This means I can code a big definition with zc and reopen it with zo. This, combined with the fact that a fold is window dependent, means that I can have a reference window (^W s) open with a fold containing a big chunk of data open to read it, but edit around in in my main window.

syntax on
set ai
filetype plugin on

These ones are fairly self explanatory, highlight syntax, auto indent, be aware of what type of file I’m editing.

"set nu

I’m still very undecided whether I like numbering, I tend to turn it on only when I need it.

set ts=4
set et
set sw=4

These defaults are necessary for python, but nice for most things, so I set it globally.

set backspace=indent

Autoindent is nice, but if it does something you don’t expect, not being able to backspace it back to sanity is frustrating, even if I is quicker.


" Hax to let us have multiple highlights within a single file
" http://vim.wikia.com/wiki/Different_syntax_highlighting_within_regions_of_a_file
" This took me so long to find and get working properly.
function! TextEnableCodeSnip(filetype,start,end,textSnipHl) abort "{{{
let ft=toupper(a:filetype)
let group='textGroup'.ft
if exists('b:current_syntax')
let s:current_syntax=b:current_syntax
" Remove current syntax definition, as some syntax files (e.g. cpp.vim)
" do nothing if b:current_syntax is defined.
unlet b:current_syntax
endif
execute 'syntax include @'.group.' syntax/'.a:filetype.'.vim'
try
execute 'syntax include @'.group.' after/syntax/'.a:filetype.'.vim'
catch
endtry
if exists('s:current_syntax')
let b:current_syntax=s:current_syntax
else
unlet b:current_syntax
endif
execute 'syntax region textSnip'.ft.'
\ matchgroup='.a:textSnipHl.'
\ start="'.a:start.'" end="'.a:end.'"
\ contains=@'.group.'
\ containedin=ALL'
" XXX ^^ This is needed for PHP, everything in a block is part
" of a highlighting group, which breaks the rule as per vanilla in the wiki.
endfunction "}}}

This function is brilliance. And it’s not written by me (although that last hack is my work. Basically you can call this with a start and finish marker to nest a second syntax highlighting rule in amonst a file. I find it’s most useful with SQL (because the other use case is generally PHP and html, but the PHP highlighter is already wise to this situation) but I also use it for times when I’m nesting a python (pl/python) script inside a big SQL file.

"Python Trickery {{{
" This is a hack for sql files that define plpython functions
" .plpy is probably a better extension now I think of it...
" I wrote a lot of functions that lived in a PostgreSQL database in python
" This was a rule to let the scripts look half decent on screen.
function CONFIGPYSQL()
set ft=sql
call TextEnableCodeSnip('python', '#@
endfunction
autocmd BufRead *.pysql call CONFIGPYSQL()
autocmd BufRead *.plpy call CONFIGPYSQL()


function CONFIGPYTHON()
call TextEnableCodeSnip(‘sql’, ‘@begin=sql@’, ‘@end=sql@’, ‘SpecialComment’ )
” Fix dodgy python highlighting
” I believe this is now fixed.
let python_highlight_numbers = 1
let python_highlight_builtins = 1
let python_highlight_exceptions = 1
” Courtesy of
” http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/
” Execute file being edited with + e:
map :w:!/usr/bin/env python %
” TODO Change this to write out to a temp buffer
endfunction
au BufRead,BufNewFile *.py{c,w,o,x} set ft=python
au FileType python call CONFIGPYTHON()

I don’t have much in the way of python hax, considering how much I write. I find that python_fn script provides all the functionality I need.
I update the syntax rules because after a while you know which colour you’re looking for- much easier than visually searching for a keyword, and I bind my shell so that ^e will (e)xecute my program, for testing.
I want to wrap this in a script that goes straight to the error if there is one, but this is a while down the track.

"autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
I used this for a while, I don’t any more. Smart indenting was fixed ina more recent version of vim, and so didn’t need as much coaching.
"}}}
Fold markers, they are teh win.

"PHP Trickery {{{
" Let's face it, PHP contains SQL. End of story
" This lets my SQL highlight nicely. It would be nice to have a less obtrusive
" marker though.
function CONFIGPHP()
call TextEnableCodeSnip('sql', '@begin=sql@', '@end=sql@', 'SpecialComment' )
endfunction
au FileType php call CONFIGPHP()
"}}}

Very little in the way of hackery for PHP, just configure the SQL highlighting. You’ll notice the code folding markers throughout my vimrc to help me see the important info.

" HAML hax {{{
" Haml likes indents of 2 spaces
function CONFIGHAML()
set ts=2
set sw=2
endfunction
au FileType haml call CONFIGHAML()
" }}}

HAML’s syntax requires a specific spacing, so I let vim know this is how I want it.

" Brainfuck hax {{{
" Brainfuck is excellent. Winrar!
au BufNewFile,BufRead *.bf set filetype=brainfuck
" }}}

" Ruby Hax {{{
" Prawn files are includes for a pdf rendering library
au BufNewFile,BufRead *.prawn set filetype=ruby
" }}}

" XML Hax {{{
" http://vim.wikia.com/wiki/Vim_as_XML_Editor
" I can't fathom why this isn't a default
let g:xml_syntax_folding=1
au FileType xml setlocal foldmethod=syntax
"au FileType xml normal zR
" }}}

This is a quick kludge to make XML handle a little better. I tend to start with a file fully folded and zo my way through the tree to the info I want. You can always zr a file to open them all anyway.

My vimrc will generally be available, and up to date at http://natalya.psych0tik.net/~richo/vimrc if you’re interested in it’s original and usable version.

If you do choose to use it, I suggest making your customisations in .vimrc.local and sourcing that file (As I do), because then if you want updates from me periodically you won’t get stooged having to merge them in.

About richo

Flying, coffee, computer stuff.
This entry was posted in Hax and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *