How to highlight Bash scripts in Vim?

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.


The answers so far are correct that you can use the extension (like .sh) or a shebang line (like #!/bin/bash) to identify the file type. If you don't have one of those, you can still specify the file type manually by using a modeline comment at the top or bottom of your file.

For instance, if you want to identify a script without an extension as a shell script, you could add this comment to the top of your file:

# vim: set filetype=sh :

or

# vim: filetype=sh

That will tell vim to treat the file as a shell script. (You can set other things in the modeline, too. In vim type :help modeline for more info.)