How to set vim filetype when editing nginx config files

Solution 1:

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.

Solution 2:

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

autocmd BufRead,BufNewFile /etc/nginx/sites-*/* setfiletype conf

Tags:

Vim

Ubuntu