Just another tech blog.

A blog abt GNU/Linux, programming, hacking, and my life.

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 ..

6 Responses to “Vim – applying separate settings for different file types”

  1. 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.

  2. Turbo said

    vim surprised me again!!!
    Hope to know more about vim from u.

  3. Atul said

    mast hai yaar… no other methos will as easy as that one

  4. 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

    autocmd BufNewFile *.c r ~/.vim/mainheader
    autocmd BufNewFile *.cpp r ~/.vim/mainheader

    2. Write all your includes, macros, typedefs, etc. in ~/.vim/mainheader/ For example:-

    /* This program has been made my Ajay Somani */
    #include
    #include
    using namespace std;
    int main()
    {
    return 0;
    }

  5. 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 !! “

  6. [...] 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 .. [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>