Coder Perfect

How do I make Bash scripts stand out in Vim?

Problem

PHP files (vim file.php), HTML files (vim file.html), and so on are automatically highlighted in my Vim editor.

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 correct.sh 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 as.sh) 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 :

or

# vim: filetype=sh

This instructs vim to handle the file as a shell script. (Other things can be adjusted in the modeline as well.) For further information, type:help modeline in vim.)

Answered by bryant

Solution #3

Syntax highlighting is a vim feature, not a vi feature. Try using the vim command first, and then proceed.

: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

Note that even if your ft is already sh, the set command is still required; otherwise, the let will not take effect immediately.

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,

Make a directory for your user runtime. The first item of the ‘runtimepath’ option is usually used. Unix as an example:

:!mkdir ~/.vim

To achieve this, create a vim script file. Example:

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

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 https://stackoverflow.com/questions/2576687/how-to-highlight-bash-scripts-in-vim