Customize the appearance of each text file you edit with vim!
It comes in handy to have different filetypes appear differently in your text editor. Perhaps you want tabs turned into spaces automatically in all C files, but you don’t want that to happen in Makefiles, where the tab character is necessary. Perhaps you like to do python coding with a black foreground, and write a text file with a white foreground. Maybe you want html code to have 100 characters per line but have a text file wrap at 80 characters. You get the idea.
Filetype detection is actually not very hard to do with Vim, even though the documentation might you scratching your head about how to do it. Luckily, I’m here to explain it via examples. 🙂
First of all, make sure these two lines are anywhere in your vimrc:
filetype on filetype plugin on
This will turn on the filetype detection.
Second of all, create the directory ~/.vimrc/ftplugin
In that directory, add the file, [file-type-name].vim where [file-type-name] is the name of the filetype you are customizing. For instance, the customization for C files would be c.vim; python files, python.vim, etc. In Ubuntu 9.04, you can find a list of supported filetypes in /usr/share/vim/vim72/ftplugin.
In the file you just created, add the customization options what you want. When you open a new file, Vim will automatically detect the filetype (its pretty good at this) and apply the settings you specified. Use ‘setl’ instead of ‘set’ so the customizations only apply to the correct file. For example, here is my file for C text files.
custom C settings file, c.vim
" File: ~/.vim/ftplugin/c.vim color default "use the default colorscheme setl textwidth=80 "80 character lines setl nowrap "don't wrap lines at the end setl cindent "use C style indents setl sw=4 "Use 4 space tabs
See, simple as that. You’ll need a different file for each filetype you want custom settings for. Now you know how to have each filetype do its own thing!