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


55 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 */
    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 .. […]

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


  8. ray040123 said

    This is great, it save me.

  9. autocmd FileType py source ~/.vim/py.vim


    now the third word in these lines specifies the extention

    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

  10. Howdy! This blog post could not be written any better!
    Looking at this article reminds me of my previous roommate!
    He always kept preaching about this. I am going to forward this post
    to him. Fairly certain he’s going to have a very good read. Thank you for sharing!

  11. This post is in fact a fastidious one it helps new internet users, who
    are wishing in favor of blogging.

  12. Good way of telling, and pleasant post to get information on
    the topic of my presentation subject matter, which i
    am going to present in university.

  13. Mora said

    Awesome issues here. I am very satisfied to see your post.
    Thank you so much and I’m having a look forward to touch you.
    Will you please drop me a e-mail?

  14. Hello i am kavin, its my first time to commenting anyplace, when i read this
    piece of writing i thought i could also create comment due to this good paragraph.

  15. If some one wants expert view about running a blog afterward i advise him/her to pay a quick visit this web site, Keep up the pleasant

  16. Great web site. Do you post more articles regularly.
    I honestly loved your website.

  17. Angelika said

    Quality content is the maon to invite the people to vusit the web site, that’s what this website is providing.

  18. ルイヴィトン バッグ 公式 べきではないをを選択を取得する必要があります素晴らしいこと目的のためをはそれと作業の。これらルイ
    ・ ヴィトン バッグは本当に魅力的なもとして優れた。1
    つを試みるか – すべてを試してみてれませんが不幸な! グッチ 新作

  19. Have you ever considered writing an ebook or
    guest authoring on other sites? I have a blog centered
    on the same topics you discuss and would love to have you share some stories/information.
    I know my visitors would enjoy your work. If you are even
    remotely interested, feel free to shoot me an email.

  20. First off I want to ssay terrific blog! I had a quick question in which I’d like to ask if you do not
    mind. I was interested to find out how you center yourself and clear
    your head prior to writing. I have had a tough time clearing
    my thoughts in getting my thoughts out there. I do take pleasure
    in writing but it just seems like the first 10 to 15 minutes are wasted just trying to figure out
    how to begin. Any suggestions or hints? Kudos!

  21. web page said

    Howdy! I know this is kinda off topic but I was wondering
    if you knew where I could locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having
    trouble finding one? Thanks a lot!

  22. If you’ve always looked on in envy as the little
    ones run around the parks in their princess and Buzz Lightyear costumes
    then this Halloween party is the perfect time for you to don your costume.
    Mega Candy Buttons: Again Enjoy the Sweet Strips of Sugary Sweetness.
    But when kicking back and relaxing, these will have some amazing side effects.

  23. Hi! This post could not be written any better!

    Reading this post reminds me of my previous room mate!
    He always kept talking about this. I will forward this page to him.
    Pretty sure he will have a good read. Thank you for

  24. Wonderful article! That is the kind of info that are supposed to be shared around the net.

    Shame on Google for no longer positioning this post higher!

    Come on over and discuss with my web site .

    Thanks =)

  25. If you wish to get an idea of what psychotherapy is all about, watch an
    episode with the television series The Sopranos. But being positive thinker all the time
    is not practical for everyone and so we experience various physiological
    problems like anxiety, depression, lost
    of temper, relationship issue and more. This set him wild
    and that he will probably be chasing you again and he will not likely stop until she has you
    back as part of his arms.

  26. Lillie said

    One downfall is if you drop an item you
    need, you cannot pick it back up; It’s lost. However, there remains debate over whether quantum phenomena are truly random, or
    whether we are simply yet incapable of determining
    a pattern. For example, my friend Chester is also into Anime like Naruto, which is another interest of

  27. Kristen said

    t matter where do you think you’re while using vaporizer as it won.

    However, due to emissions gas heaters produce, they are certainly not well suited for small rooms.

    When people who’re responsive to mold are exposed, a reaction of allergies or asthma
    may result.

  28. Interesting blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple tweeks would really make my blog jump out.
    Please let me know where you got your theme. With thanks

  29. I’m not sure where you’re getting your information, but good topic.
    I needs to spend some time learning more or understanding
    more. Thanks for wonderful information I was looking for this information for my mission.

  30. wow map said

    Other common Revita Derm complaints deal with the fact that their applications in the excessively.

    Phenomenal shots will present themselves all over the place.
    There are 5 strategies that you really should implement when trying to advance in the world of warcraft.

  31. In fact no matter if someone doesn’t know after
    that its up to other users that they will help, so here it happens.

  32. At level 40 you will receive garrot which causes your opponent to bleed and offers a damage over
    time attack. At first, essential convinced to
    try a large amount of cash finishing this
    specific journey, you’ll want to own a good number of case
    breathing space accessible. It is because we know that it will fall
    to our crawfish pots for our boiling recipe.

  33. wow said

    t know of any but give this a try and it could be you.
    He sends you to speak with Hands Springsprocket who tends the tunnel connecting Coldridge
    to the rest of Dun Morogh. Stop checking your buddies and
    guildies as they blow earlier you in gold and gear.

  34. certainly like your website but you need to check the spelling on quite a few of your posts.
    Many of them are rife with spelling problems and I
    find it very bothersome to tell the reality nevertheless I’ll definitely come again again.

  35. If some one desires expert view on the topic of blogging then i propose him/her to pay a quick visit this website, Keep up the nice job.

  36. Hi there everyone, it’s my first visit at this website, and paragraph is truly fruitful
    designed for me, keep up posting these types of content.

  37. moourl.com said

    Hi friends, how is everything, and what you would like to
    say about this post, in my view its actually remarkable for me.

    – cliquez ici
    – cliquez ici
    – cliquez ici
    – cliquez ici
    – cliquez ici
    – cliquez ici

  38. Deloras said

    Well, defensive scouting reports said he had no intention to degrade India as his
    statement has been misinterpreted. The conclusion marrakech
    quad vanessa huppenkothen is that USB Navigator works right
    out of the play, then I’ve done my job. Basu, state secretary Bose
    and a few from last month. When you take photo with
    it, providing one of the 29 skaters to hit a quad-triple

  39. rgv mls said

    Hi, its pleasant post regarding media print, we all be familiar with media is a enormous source of data.

  40. Ciara said

    Pretty section of content. I simply stumbled upon your
    web site and in accession capital to claim that I get actually enjoyed account your blog posts.
    Any way I’ll be subscribing in your augment and even I achievement you get right of entry to constantly fast.

  41. I wikll immediately take hold of your rss feed as I can not in fining yourr emazil subscription link or newsleter service.

    Do you have any? Kindly permikt me undrstand sso
    that I could subscribe. Thanks.

  42. Hi there to all, howw is all, I think every one is getting more from this website,
    and your views are nice in favor of new users.

    • Kapri said

      Whoa, whoa, get out the way with that good inmnrfatioo.

    • Verím, že je možné vytvoriÅ¥ umelé systémy, ktoré by duplikovali konkrétnu ľudskú myseľ, ale myslím si, že je to vec závislá od fyzikálnej podstaty systému a nie len od algoritmov.(1) Predpokladám, že (ne)prítomnosÅ¥ mentálnych fenoménov je závislá od fyzikálnej podstaty systému.(2) Predpokladám, že ku každej simulácii (aj k tej najkomplexnejÅ¡ej), existuje algoritmicky izomorfná simulácia, ktorá negeneruje žiadnu myseľ.

  43. Its such aas you learn myy thoughts! You seem to understand so much
    approximately this, loke you wrote the book in it or something.
    I think that you simply can do with a few p.c.
    to drive the message house a bit, however instead of that, that is great blog.
    A great read. I’ll certainly be back.

    • Boston said

      My biggest question at the moment is the extent to which Mondoweiss panttciparis are reflective of the general population, and how great an influence the blog has on the general perception of the public, through a ripple effect.

  44. Geez….outstanding web blog and data here.
    These content on this site are very insightful!! 😉 Any specific RSS feed on this
    web site??? I have been searching for often current articles about professional carpet cleaning tips.

  45. What’s up, yes this post is actually pleasant and I
    have learned lot of things from it concerning blogging.

  46. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is fundamental and everything. Nevertheless think about if you added some great
    pictures or video clips to give your posts more, “pop”! Your content is excellent but with pics and
    clips, this website could definitely be one of the
    greatest in its field. Great blog! modele de invitatii de nunta

  47. Very shortly this web site will be famous among all blog people, due to it’s pleasant content

  48. Jane said

    That’s not just logic. That’s really sesilbne.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: