How to highlight Bash scripts in Vim?

never_had_a_name picture never_had_a_name · Apr 5, 2010 · Viewed 66.7k times · Source

My Vim editor auto highlights PHP files (vim file.php), HTML files (vim file.html) and so on.

But when I type: vim file and inside it write a Bash script, it doesn't highlight it.

How can I tell Vim to highlight it as a Bash script?

I start typing #!/bin/bash at the top of the file, but it doesn't make it work.

Answer

Mark Rushakoff picture Mark Rushakoff · Apr 5, 2010

Are you correctly giving the shell script a .sh extension? Vim's automatic syntax selection is almost completely based on file name (extension) detection. If a file doesn't have a syntax set (or is the wrong syntax), Vim won't automatically change to the correct syntax just because you started typing a script in a given language.

As a temporary workaround, the command :set syn=sh will turn on shell-script syntax highlighting.