Skip to content

Instantly share code, notes, and snippets.

@ralavay
Last active November 5, 2024 02:16
Show Gist options
  • Save ralavay/c4c7750795ccfd72c2db to your computer and use it in GitHub Desktop.
Save ralavay/c4c7750795ccfd72c2db to your computer and use it in GitHub Desktop.
Enable syntax highlight for Nginx conf file in Vim
#!/bin/bash
#
# Highligh Nginx config file in Vim
# Download syntax highlight
mkdir -p ~/.vim/syntax/
wget http://www.vim.org/scripts/download_script.php?src_id=19394 -O ~/.vim/syntax/nginx.vim
# Set location of Nginx config file
cat > ~/.vim/filetype.vim <<EOF
au BufRead,BufNewFile /etc/nginx/*,/etc/nginx/conf.d/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
EOF
@timogoosenwork
Copy link

This worked for me on Mac:
Thanks!

$ mkdir -p ~/.vim/syntax/
$ wget http://www.vim.org/scripts/download_script.php?src_id=19394 -O ~/.vim/syntax/nginx.vim
$ cat > ~/.vim/filetype.vim <<EOF
au BufRead,BufNewFile /etc/nginx/*,/etc/nginx/conf.d/*,/usr/local/etc/nginx/* if &ft == '' | setfiletype nginx | endif
EOF

@Yelfive
Copy link

Yelfive commented Jan 16, 2019

thanks

@cimminob
Copy link

Works on Ubuntu18.04 as of 3-27-19, thanks a bunch

@lingtalfi
Copy link

Thanks.

@nballenger
Copy link

May want to use cat >> ~/.vim/filetype.vim <<EOF if you have a pre-existing filetype.vim file--the single > output redirection will overwrite the file, double >> will append to it.

@Soneji
Copy link

Soneji commented Jul 28, 2019

thank you =D

@kamilr
Copy link

kamilr commented Apr 13, 2020

for my Vim (8.1) by team+vim@tracker.debian.org only this code will work:

#!/bin/bash
#
# Highligh Nginx config file in Vim

# Download syntax highlight
mkdir -p ~/.vim/syntax/
curl http://www.vim.org/scripts/download_script.php?src_id=19394 -o ~/.vim/syntax/nginx.vim

# Set location of Nginx config file
cat >> ~/.vim/filetype.vim <<EOF
au BufRead,BufNewFile /etc/nginx/*,/etc/nginx/conf.d/*,/usr/local/nginx/conf/* if &ft == '' | set filetype=nginx | endif
EOF

additionally I add previous fixes

@meson10
Copy link

meson10 commented Jun 15, 2020

#!/bin/bash
#
# Highligh Nginx config file in Vim

# Download syntax highlight
mkdir -p ~/.vim/syntax/
curl -L http://www.vim.org/scripts/download_script.php?src_id=19394 -o ~/.vim/syntax/nginx.vim

# Set location of Nginx config file
cat >> ~/.vim/filetype.vim <<EOF
au BufRead,BufNewFile /etc/nginx/*,/etc/nginx/conf.d/*,/usr/local/nginx/conf/* if &ft == '' | set filetype=nginx | endif
EOF

Add curl -L to follow redirects.

@caio-vinicius
Copy link

Very very useful, thank you very much. Eye candy for my eyes

@normic
Copy link

normic commented Jan 21, 2021

Quite useful, thank you.

@serafin-tech
Copy link

an alternative to filetype.vim file would be having sth like that at the end of nginx config file:

# vi: filetype=nginx

pros: you are not limited to have nginx config in /etc/nginx directory
cons: you need to add this modeline to the file

@francotel
Copy link

perfect ubuntu 18.04

@crpb
Copy link

crpb commented Nov 8, 2021

๐Ÿ‘ now i can see my typo's a bit earlier :-)
for users with vim-plug

cat << EOF > ~/.vim/ftdetect/nginx.vim
au BufRead,BufNewFile /etc/nginx/*,/etc/nginx/conf.d/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
EOF

@saltfishh
Copy link

it's useful,thanks for that

@uvwild
Copy link

uvwild commented Feb 8, 2022

not sure why its missing, but the standard nginx.conf is not in the filetypes list.
Why would i not use the coloring on any file called nginx.conf?
so i needed
au BufRead,BufNewFile nginx.conf,/etc/nginx/,/etc/nginx/conf.d/,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif

We use a lot of dockerized / containerized nginx's where the config is never in the standard locations

@jwwb681232
Copy link

๐Ÿ‘

@AysadKozanoglu
Copy link

thanks

@fessacchiotto
Copy link

๐ŸŽ‰๐Ÿ‘๐Ÿป

an alternative to filetype.vim file would be having sth like that at the end of nginx config file:

# vi: filetype=nginx

pros: you are not limited to have nginx config in /etc/nginx directory cons: you need to add this modeline to the file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment