14

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 '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
  • 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) – 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
24

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 '12 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'. – Adam Monsen Jan 14 '15 at 19:56
  • For all .conf files, I put the command as this: autocmd BufRead,BufNewFile *.conf setfiletype conf – Eren Yilmaz Oct 21 '16 at 14:51
19

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.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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