Vim – applying separate settings for different file types
Posted by ajay on June 2, 2006
If you are using vim quite often, you might have configured your vimrc very well. But these settings grow continuously; so it is always not feasable to apply same settings for each and every filetype .. you might want to apply some x settings if you are writing a cpp programme and some y settings when you are editing an html code and so on. Your vimrc might be growing too large so you might want to separate it into different files; I mean different configuration for different file types. Here’s what you can do -
[user@localhost.localadmin]vim ~/.vimrc
The syntex for specific filetype settings is
autocmd FileType cpp source ~/.vim/cpp.vim
autocmd FileType py source ~/.vim/py.vim
and so on…
now the third word in these lines specifies the extention and the fifth word specifies the file code to be executed .. that means from now onwards if a person will open a .cpp file ( cpp programme ) the vim settings writtin in ~/.vim/cpp.vim .. will be applied ..
you can specify settings for as many filetypes as you want .. and b sure that if you specify a source file in .vimrc of yours then even if you dont want to apply any settings as of now .. plzz create an appropriate file at an appropriate place which you have specified in the .vimrc otherwise whenever you will try to edit that filetype it will give errors .. thats it so now you can apply different settings for different codes.
I will put my .vimrc and some separate settings for different filetypes may be in a day or two .. any comments if you have any better ideas ..
paresh said
the earlier suggestion about shell scripts would work but it is a round about solution. it is not advisable to break head writing such a script unless ofcourse a person is a scripting freak and wats to show off the power of scripting.
Turbo said
vim surprised me again!!!
Hope to know more about vim from u.
Atul said
mast hai yaar… no other methos will as easy as that one
Turbo said
a tip:
If you want that whenever you open a new c/cpp file then the normal include lines, macros and typedefs be already written into it, then do this:-
1. add thess lines to your ~/.vimrc
2. Write all your includes, macros, typedefs, etc. in ~/.vim/mainheader/ For example:-
ajay said
@turbo .
well ur tip was great .. i was looking for that frm long time .. waise we can do a lot more things with autocmd in .vimrc .. my post was just one example .. another example may be .. suppose you want that your vim editor greets you each time you open a file or close the file you may add these 2 lines to ur .vimrc ..
autocmd VimEnter * echo “Hi turbo .. you can do it ..”
autocmd VimLeave * echo “so u finished .. huh .. see ya latr !! “
Setting up new keywords for programmes in ‘Vim’ .. good one .. « It’s my life said
[...] all the keywords are related to syntax .. so for creating new keywords . first of all we have to create a class of words .. for that we use the syntax command .. for example suppose for a C program you want to highlight some words I mentioned above as datatypes .. what you do is in the vim configuration file specific to C programmes ( Previously I told how to have specific configuration files for specific file types in vim here ) we write the following lines .. [...]
Colum Paget said
Hiya,
this was a great post that put me on the right path. But at first, when I tried this, it didnt’ work for me. I think the reason was that the file type that I wanted to add settings for ‘.txt’, doesn’t exist as a standard vim filetype.
I think (though I’m not sure… but I think) that the FileType event requires the file-type in question to be set up as one of the ones that vim recognises. I’ve got an alternative command that will work with any filetype:
autocmd BufEnter *.txt set spell
The ‘BufEnter’ event might not sound like it has anything to do with files, but it has. Basically a ‘Buffer’ in this context is anything that is being edited (and this mostly means a file). The event can match on the file name with the usual wildcards, and then be used to set something, in this case to turn on the spell-check.
The advantage of BufEnter is it will work with filetypes that you’ve just made up!
Another useful autocmd, I find, is:
autocmd BufEnter *.xml set nosyntax
Because I’m often encountering xml files that have no line-breaks, and are just a single huge file. This really upsets vim if you have syntax turned on, as I suspect it normally syntax hilights as it goes, but now it has to highlight the whole of this huge, single-line document.
Anyways, thanks for this post, I wouldn’t have found autocmd without you!
Colum
ray040123 said
This is great, it save me.
James Cole (@jamesrcole) said
Just a minor point but the third word specifies the name of the filetype, not the name of the extension – they aren’t always the same.
For example, with Javascript you need to use
autocmd FileType javascript …
It won’t work if you use
autocmd FileType js …
the filetype names are defined in filetype.vim (on my system it’s /Applications/MacVim.app/Contents/Resources/vim/runtime/filetype.vim). It defines the javascript filetype like this:
u BufNewFile,BufRead *.js,*.javascript,*.es,*.jsx setf javascript