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.

  • 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: serverfault.com/a/390801/2101
    – MikeyB
    May 20, 2012 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. May 20, 2012 at 15:26
  • 3
    possibly interesting: vim.org/scripts/script.php?script_id=1886 (I use it and it's pretty nice, add a # vim:syn=nginx at the bottom of the configs) May 20, 2012 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, 2012 at 17:49
  • @MikeyB The new title doesn't even use the word 'default'. The original one did. May 21, 2012 at 10:13

5 Answers 5


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

autocmd BufRead,BufNewFile /etc/nginx/sites-*/* setfiletype conf
  • slight correction autocmd BufRead,BufNewFile /etc/nginx/sites-/ setfiletype conf
    – user123094
    May 31, 2012 at 20:05
  • 1
    vim.org/scripts/script.php?script_id=1886 provides better syntax highlighting than the conf filetype. If you use Vundle, try Plugin 'vim-scripts/nginx.vim'. Jan 14, 2015 at 19:56
  • For all .conf files, I put the command as this: autocmd BufRead,BufNewFile *.conf setfiletype conf Oct 21, 2016 at 14:51

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.


Easiest way to get vim filetype you want is to specify it at the top of the file via a modeline. In this case, place the following comment in the first 5 lines of the file

#       vim: set syntax=nginx ft=nginx

More information on filetype and modeline is available here http://vimdoc.sourceforge.net/htmldoc/filetype.html http://vimdoc.sourceforge.net/htmldoc/options.html#modeline

  • This doesn't seem easier than the autoconfiguration provided by the accepted answer.
    – womble
    Jul 28, 2020 at 9:52
  • The accepted answer assumes the filename will match an absolute path glob and that you have control of the .vimrc. Neither may be true, e.g. if you're working in a git repo on files pushed out via CICD pipeline or logging in via shared break-glass account. Specifying the filetype selection within the file itself solves those cases.
    – jdkelleher
    Jul 29, 2020 at 12:25

No need to edit vimrc for that. Just take contrib/vim files from nginx source tree and place them into $HOME/.vim so that tree should be like that:

├── ftdetect
│   └── nginx.vim
├── ftplugin
│   └── nginx.vim
├── indent
│   └── nginx.vim
└── syntax
    └── nginx.vim

Then whenever you open nginx.conf ft gets detected and highlight gets applied automatically.


In my case, on a small Debian install, the relevant files already existed (possibly for the default vim.tiny) but weren't sourced in regular vim. It was easily fixed with this:

for d in ftdetect ftplugin indent syntax; do
    sudo mkdir /var/lib/vim/addons/$d
    sudo ln -s /usr/share/vim/addons/$d/nginx.vim /var/lib/vim/addons/$d/nginx.vim

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .