Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@WyattJia

This comment has been minimized.

Copy link

@WyattJia WyattJia commented Jan 22, 2018

Very useful, thanks.

@yogin16

This comment has been minimized.

Copy link

@yogin16 yogin16 commented Mar 4, 2018

added one more regex param if the nginx.conf file is not for the default location of the nginx - ideally you should just add your location path in that

#!/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/*,*/conf/nginx.conf if &ft == '' | setfiletype nginx | endif
EOF
@movalex

This comment has been minimized.

Copy link

@movalex movalex commented Mar 14, 2018

I'd better use curl instead

curl http://www.vim.org/scripts/download_script.php?src_id=19394 -o ~/.vim/syntax/nginx.vim
@jjsty1e

This comment has been minimized.

Copy link

@jjsty1e jjsty1e commented Apr 9, 2018

great! this is ok for me on my mac

@passerbythesun

This comment has been minimized.

Copy link

@passerbythesun passerbythesun commented May 5, 2018

great

@timogoosenwork

This comment has been minimized.

Copy link

@timogoosenwork timogoosenwork commented May 9, 2018

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

This comment has been minimized.

Copy link

@Yelfive Yelfive commented Jan 16, 2019

thanks

@cimminob

This comment has been minimized.

Copy link

@cimminob cimminob commented Mar 28, 2019

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

@lingtalfi

This comment has been minimized.

Copy link

@lingtalfi lingtalfi commented May 9, 2019

Thanks.

@nballenger

This comment has been minimized.

Copy link

@nballenger nballenger commented May 14, 2019

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

This comment has been minimized.

Copy link

@Soneji Soneji commented Jul 28, 2019

thank you =D

@kamilr

This comment has been minimized.

Copy link

@kamilr 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

This comment has been minimized.

Copy link

@meson10 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

This comment has been minimized.

Copy link

@caio-vinicius caio-vinicius commented Jan 7, 2021

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

@normic

This comment has been minimized.

Copy link

@normic normic commented Jan 21, 2021

Quite useful, thank you.

@SerafinSahary

This comment has been minimized.

Copy link

@SerafinSahary SerafinSahary commented Jun 5, 2021

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

This comment has been minimized.

Copy link

@francotel francotel commented Jun 29, 2021

perfect ubuntu 18.04

@crpb

This comment has been minimized.

Copy link

@crpb 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment