Search code examples
linuxbashvimsyntax-highlightingvim-syntax-highlighting

How to highlight Bash scripts in Vim?


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.


Solution

  • 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.