24

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.

MikeyB
  • 38,725
  • 10
  • 102
  • 186
HappyDeveloper
  • 654
  • 2
  • 6
  • 13
  • 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: http://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: http://www.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
  • `:se ft=conf` for having the comments at least highlighted. – sjas Sep 29 '16 at 11:47

5 Answers5

34

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.

colan
  • 500
  • 5
  • 9
30

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

autocmd BufRead,BufNewFile /etc/nginx/sites-*/* setfiletype conf
mgorven
  • 30,036
  • 7
  • 76
  • 121
  • slight correction autocmd BufRead,BufNewFile /etc/nginx/sites-*/* setfiletype conf –  May 31 '12 at 20:05
  • 1
    http://www.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
2

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

jdkelleher
  • 21
  • 1
  • This doesn't seem easier than the autoconfiguration provided by the accepted answer. – womble Jul 28 '20 at 09: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 '20 at 12:25
1

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:

/home/ubuntu/.vim
├── 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.

vatosarmat
  • 111
  • 3
0

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
done
Walf
  • 293
  • 1
  • 3
  • 16