Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I open vim for a file like /etc/nginx/sites-available/default, syntax highlighting works fine. But then if I create my own file /etc/nginx/sites-available/myapp, vim does not highlight its syntax. I have to do :setf conf every time.

Is there anything I can put in ~/.vimrc to tell vim "if you don't know which syntax to use, just use conf" ?

A .vimrc template for a vim noob is also welcome. I'm not using it as an IDE, I use vim mostly for config files only.

Note: I'm using Ubuntu 12, in case it matters.

share|improve this question
I changed your title to more accurately reflect what you are trying to do and re-asked the question here, since this can be useful in the general case: – MikeyB May 20 '12 at 12:59
@MikeyB I didn't know or care about 'filetypes', as far as I knew vim could have been doing the syntax highlighting just by looking at the code and guessing the language. And the main part of the problem was that I wanted it to be 'by default'. I already knew how to 'set the language/filetype when editing'. Also, whether it is for nginx or anything else, is irrelevant. Do as you want, I never understood why serverfault people decide to close or edit questions (for SEO maybe?). Unless grammatically incorrect, the original title reflected my problem. – HappyDeveloper May 20 '12 at 15:26
possibly interesting: (I use it and it's pretty nice, add a # vim:syn=nginx at the bottom of the configs) – SingleNegationElimination May 20 '12 at 17:37
@HappyDeveloper: the reason I edited it was that your accepted answer indicates "Oh, I really was trying to do that." Now future visitors will be able to search for 'default vim filetype' or 'default vim syntax' and get the correct answer for that instead of getting frustrated since the accepted answer doesn't say how to accomplish the title of the question. Plus, now you also know how to set the default filetype :) – MikeyB May 20 '12 at 17:49
@MikeyB The new title doesn't even use the word 'default'. The original one did. – HappyDeveloper May 21 '12 at 10:13
up vote 16 down vote accepted

The following line in ~/.vimrc should do this.

autocmd BufRead,BufNewFile /etc/nginx/sites-*/* setfiletype conf
share|improve this answer
Thanks! ####### – HappyDeveloper May 20 '12 at 5:42
slight correction autocmd BufRead,BufNewFile /etc/nginx/sites-/ setfiletype conf – user123094 May 31 '12 at 20:05 provides better syntax highlighting than the conf filetype. If you use Vundle, try Plugin 'vim-scripts/nginx.vim'. – Adam Monsen Jan 14 '15 at 19:56

There's actually an Nginx file type defined in the official Nginx repository in the contrib/vim directory. It provides better syntax highlighting than conf.

To use it, copy the detection lines to your .vimrc (and tweak as desired):

au BufRead,BufNewFile *.nginx set ft=nginx
au BufRead,BufNewFile */etc/nginx/* set ft=nginx
au BufRead,BufNewFile */usr/local/nginx/conf/* set ft=nginx
au BufRead,BufNewFile nginx.conf set ft=nginx

Then copy nginx.vim to your ~/.vim/syntax directory.

All Nginx files following the above rules should now be highlighted.

If you'd like the indenting as well, you can also copy the file from the indent directory into your .vimrc.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.