要让VIM正确识别并高亮显示Nginx的.conf文件,你可以按照以下步骤操作:
下载nginx.vim语法文件:
mkdir -p ~/.vim/syntax/
wget https://www.vim.org/scripts/download_script.php?src_id=19394 -O ~/.vim/syntax/nginx.vim
创建文件类型检测规则:
mkdir -p ~/.vim/ftdetect/
echo "au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/*,*/conf/nginx.conf,*/conf.d/*.conf if &ft == '' | setfiletype nginx | endif" > ~/.vim/ftdetect/nginx.vim
如果你使用插件管理器(如Vundle、Pathogen或vim-plug):
对于Vundle:
在你的.vimrc
中添加:
Plugin 'chr4/nginx.vim'
然后运行:PluginInstall
对于vim-plug:
在你的.vimrc
中添加:
Plug 'chr4/nginx.vim'
然后运行:PlugInstall
如果不想使用插件,可以手动配置:
在~/.vim/filetype.vim
中添加:
augroup filetypedetect
au BufNewFile,BufRead nginx.conf setf nginx
au BufNewFile,BufRead */nginx/*.conf setf nginx
au BufNewFile,BufRead */conf.d/*.conf setf nginx
augroup END
下载语法文件到~/.vim/syntax/nginx.vim
打开一个Nginx配置文件后,输入:
:set ft?
应该显示filetype=nginx
如果未正确识别,可以手动设置:
:set filetype=nginx
如果想让VIM自动识别没有.conf后缀的Nginx配置文件,可以添加:
au BufNewFile,BufRead * if getline(1) =~ '^#.*nginx configuration' | setf nginx | endif
对于更完整的支持,可以考虑安装vim-syntastic
插件来检查Nginx配置语法:
Plugin 'vim-syntastic/syntastic'
let g:syntastic_nginx_checkers = ['nginx']
这样配置后,VIM就能正确高亮显示Nginx配置文件了。