Monday, June 2, 2008

Dynamic Indentation Width In Vim

How to auto-indent differently when coding in different programming languages ? I started hacking this problem when I found myself prefer to indent 4 spaces in Python yet 8 for C. As a Vim user, I do this: Of course I put this in my vimrc:
filetype plugin indent on
Then I copy
$VIMRUNTIME/indent/python.vim
which is the original indentation setting file for python, to
~/.vim/indent/
Finally, customize it by adding the following lines in to the new copy:
setlocal shiftwidth=4 setlocal ts=4
The default indentation width in vim is 8 spaces, and when vim detects a Python source file, it automatically changes that to 4 spaces. Problem solved! A hint about $VIMRUNTIME: you can use this variable IN vim, or find the path it stands for by typing
:!ls $VIMRUNTIME
then hit TAB. I'm curious if someone else has solved the same problem. What's your solution in vim? How about other editors? IDE?

No comments: