Coder Perfect

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.

However, when I type vim file and then create a Bash script inside of it, it does not highlight it.

How do I make Vim recognize it as a Bash script?

I begin typing #!/bin/bash at the top of the file, but nothing happens.

Asked by never_had_a_name

Solution #1

Are you using the extension for the shell script? Vim’s automatic syntax selection is nearly entirely reliant on the detection of file names (extensions). Vim will not automatically shift to the right syntax just because you started typing a script in a specific language if the file doesn’t have a syntax specified (or has the wrong syntax).

The command:set syn=sh will enable shell-script syntax highlighting as a temporary fix.

Answered by Mark Rushakoff

Solution #2

The answers so far are true in that you can identify the file type by looking at the extension (such or a shebang line (such as #!/bin/bash). If you don’t have one of these, you can still identify the file type by adding a modeline comment to the top or bottom of your file.

For example, you could include the following note to the top of your file to identify a script without an extension as a shell script:

# vim: set filetype=sh :


# vim: filetype=sh

This instructs vim to handle the file as a shell script. (Other things can be adjusted in the modeline as well.) In vim type :help modeline for more info.)

Answered by bryant

Solution #3

Actually syntax highlighting is a feature of vim not vi. Try using vim command and then do

:syntax on.

Answered by Gaurav Khare

Solution #4

I was looking for a way to highlight bash syntax, not POSIX shell, when I came across this response. Simply setting ft=sh (or equivalent) causes the file to be highlighted for POSIX shell, which highlights a lot of syntax that is legitimate in bash in red. To get bash highlighting, follow these steps:

" Set a variable on the buffer that tells the sh syntax highlighter
" that this is bash:
let b:is_bash = 1
" Set the filetype to sh
set ft=sh

If your ft is already sh, the set command is still required; otherwise, the let will not take effect right away.

You can make this a global default by setting the variable g:is bash = 1.

The manual page I needed to find was :help ft-sh-syntax, which covers this as well as how to trigger highlighting in other shell dialects.

Answered by Thanatos

Solution #5

Vim may also detect file types by inspecting their contents (for example, if the first line has a bash shebang), as seen in the following excerpt from the filetype.txt help file:

If you can only tell what type of file you have by looking at its contents,

Create your user runtime directory. You would normally use the first item of the ‘runtimepath’ option. Example for Unix:

:!mkdir ~/.vim

Create a vim script file for doing this. Example:

if did_filetype()   " filetype already set..
    finish      " ..don't do these checks
if getline(1) =~ '^#!.*\<mine\>'
    setfiletype mine
elseif getline(1) =~? '\<drawing\>'
    setfiletype drawing

More examples can be found in $VIMRUNTIME/scripts.vim. In your user runtime directory, save this file as “scripts.vim.” For instance, in Unix:

:w ~/.vim/scripts.vim

There is no need to restart Vim for the detection to operate.

Because your scripts.vim is loaded before the default file type checks, your rules take precedence over the default rules in $VIMRUNTIME/scripts.vim.

Answered by Matteo Riva

Post is based on