How to set vim filetype when editing nginx config files
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.
Solution 1:
The following line in ~/.vimrc
should do this.
autocmd BufRead,BufNewFile /etc/nginx/sites-*/* setfiletype conf
Solution 2:
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 3:
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
Solution 4:
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.