Nginx Syntax Highlight in Vim

Categories: Webserver

  1. To enable Syntax Highlight in Vim start by downloading nginx.vim and place it in your ~/.vim/syntax/ folder

  2. Create ~.vimrc and add the following to it

    set number            # Shows line numbers
    syntax on             # Enables syntax highlighting
    colorscheme default   # Default color scheme
    set tabstop=2         # Set tabs to 2 spaces
    set autoindent        # Enables auto indent

  3. You do now have to choose how you want your syntax highlighting to work. It can be done either by telling vim where the Nginx configuration files are located or by adding a line in each nginx configuration file that tells vim which syntax highlighting to use.

    • Option A. Create a file called filetype.vim in ~/.vim/ and add the following to it
      au BufRead,BufNewFile /etc/nginx/*,/etc/nginx/conf.d/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
    • Option B. Add # vim: syntax=nginx to each nginx configuration file
      # vim: syntax=nginx
      • Pro: This method works for Nginx files outside of /etc/nginx/conf.d/, /etc/nginx/, /usr/local/src/nginx/conf/
      • Con: You need to add this line to the file