When working with Nginx configuration files in Vim, you might notice inconsistent syntax highlighting behavior. While system files like /etc/nginx/sites-available/default
get proper highlighting, custom files like /etc/nginx/sites-available/myapp
don't. This happens because Vim doesn't automatically recognize these files as Nginx configurations.
Vim determines filetypes through several methods:
- File extension patterns (
*.conf
) - Shebang lines
- Modeline comments
- Content patterns
For Nginx configs, we need to help Vim recognize these files even without standard extensions.
Add this to your ~/.vimrc
:
" Detect Nginx config files
au BufNewFile,BufRead /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
For a more comprehensive solution that works with any Nginx config location:
" Enhanced Nginx config detection
function! s:DetectNginx()
if getline(1) =~ '^#.*nginx configuration'
setfiletype nginx
endif
endfunction
au BufNewFile,BufRead *.conf,nginx*.conf,*nginx.conf if &ft == '' | call s:DetectNginx() | endif
If you prefer a simpler solution that defaults to conf
syntax for unknown files:
" Fallback to conf syntax for unknown filetypes
au BufNewFile,BufRead * if &ft == '' | setfiletype conf | endif
Here's a complete starter .vimrc
for working with Nginx configs:
" Basic settings
set nocompatible
syntax on
filetype plugin indent on
" Nginx specific settings
au BufNewFile,BufRead /etc/nginx/*,/usr/local/nginx/conf/* setfiletype nginx
au BufNewFile,BufRead nginx.conf,*nginx*.conf setfiletype nginx
" Fallback for other config files
au BufNewFile,BufRead *.conf if &ft == '' | setfiletype conf | endif
" Display settings
set number
set ruler
set showcmd
set wildmenu
After making changes to your .vimrc
, verify the filetype detection works:
- Open a new Nginx config file:
vim /etc/nginx/sites-available/test
- Check the current filetype:
:set ft?
(should returnnginx
) - Verify syntax highlighting is working
- Install the
vim-nginx
plugin for enhanced Nginx syntax support - Consider adding modelines to your Nginx config files:
# vim: set ft=nginx:
- For large configurations, use
:syntax sync fromstart
if highlighting breaks
Many sysadmins and developers who work with Nginx configuration files in Vim face this common issue: while default Nginx config files like /etc/nginx/nginx.conf
or /etc/nginx/sites-available/default
get proper syntax highlighting, newly created files don't. This happens because Vim doesn't automatically recognize these files as Nginx configuration files.
Vim uses several methods to determine filetype:
- File extension (
.conf
,.txt
, etc.) - Shebang line (for scripts)
- Modeline (special comments in file)
- Content analysis
For Nginx configs, the most reliable approach is to explicitly tell Vim to treat them as configuration files.
Add these lines to your ~/.vimrc
:
" Set filetype for Nginx configs
autocmd BufNewFile,BufRead /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
This tells Vim to:
- Apply to both new (
BufNewFile
) and existing (BufRead
) files - Match files in common Nginx config directories
- Only set filetype if not already detected (
if &ft == ''
) - Use the nginx filetype (better than just 'conf')
If you prefer file-specific settings, add this as the first or last line in your Nginx config:
# vim: set ft=nginx:
The advantage is it travels with the file, but requires editing each config file.
To check if syntax highlighting is working:
:set filetype?
:set syntax?
Both should return "nginx" for properly configured files.
For a better editing experience, consider adding these to your .vimrc
:
" Nginx-specific settings
autocmd FileType nginx setlocal commentstring=#\ %s
autocmd FileType nginx setlocal tabstop=2 shiftwidth=2 expandtab
This sets:
- Proper comment character (# for Nginx)
- 2-space indentation (common Nginx style)
If syntax highlighting still doesn't work:
- Verify you have Nginx syntax files installed (usually in
/usr/share/vim/vimfiles/syntax/nginx.vim
) - Check for conflicting filetype plugins
- Try manually setting with
:set filetype=nginx