How to highlight Bash scripts in Vim?

My Vim editor automatically selects PHP files ( vim file.php ), HTML files ( vim file.html ), etc.

But when I type: vim file and write a Bash script inside it, it does not 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 does not make it work.

+51
linux bash vim syntax-highlighting vim-syntax-highlighting
Apr 05 2018-10-10T00:
source share
11 answers

Are you passing the shell script a .sh extension correctly? Vim's automatic syntax selection is almost entirely based on file name (extension) detection. If the file does not have a set of syntax (or it is the wrong syntax), Vim will not automatically change to the correct syntax just because you started to enter a script in a given language.

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

+56
Apr 05 2018-10-10T00:
source share

The answers are still true that you can use the extension (e.g. .sh ) or the shebang string (e.g. #!/bin/bash ) to identify the file type. If you do not have one of them, you can still specify the file type manually using the modeling comment at the top or bottom of your file.

For example, if you want to identify a script without extension as a shell script, you can add this comment to the top of the file:

 # vim: set filetype=sh : 

or

 # vim: filetype=sh 

This will tell vim to treat the file as a shell script. (You can also set other things in modeline. In the vim :help modeline for more information.)

+21
Aug 31 2018-12-12T00:
source share

Vim can also determine file types by checking their contents (for example, if the first line contains shebang), here is a quote from the help filetype.txt :

If your file type can only be detected by checking the contents of the file

Create your working user folder. Usually you use the first element of the runtimepath option. Example for Unix:

 :!mkdir ~/.vim 

Create a vim script file for this. 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 

See $ VIMRUNTIME / scripts.vim for more examples. Write this file as "scripts.vim" in the user directory. For example, for Unix:

 :w ~/.vim/scripts.vim 

Detection will work immediately, there is no need to restart Vim.

Your scripts.vim file is loaded prior to checking for default file types, which means that your rules will override the default rules in $ VIMRUNTIME / scripts.vim.

+12
Apr 05 '10 at 8:02
source share

In fact, syntax highlighting is a feature of vim not vi. Try using vim command and then run

:syntax on .

+10
Feb 10 '15 at 10:01
source share

vim can detect top comment

try this line at the top of the file

 #!/bin/sh 
+5
Dec 05 '14 at 7:41
source share

Once you add shebang to the top of the file, save it and reload it (for example :w|e ), and the syntax coloring can hit.

See also Vim, incompatible with syntax highlighting of bash files , the accepted answer may also help.

+1
May 15 '15 at 10:12
source share

I came up with this answer, looking specifically for how to isolate the bash syntax and not the POSIX shell. Just executing set ft=sh (or the equivalent) will cause the file to be allocated to the POSIX shell, which leaves a lot of syntax that is valid in bash , highlighted in red. To highlight bash:

 " 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 if your ft already sh , you still need the set command; otherwise let does not take effect immediately.

You can make this global by default by making the variable global, i.e. let g:is_bash = 1 .

:help ft-sh-syntax is the manual page I should have found; he explains this and how to initiate the release of other shell fragrances.

+1
Mar 01 '17 at 22:29
source share

vim already recognizes many file types by default. Most of them work with file extensions, but in this case, vim will also analyze the contents of the file to guess the correct type.

vim sets the file type for specific file names such as .bashrc , .tcshrc , etc. automatically. But a file with a .sh extension will be recognized as a csh, ksh or bash script. To determine which type of script this is, vim reads the first line of the file to look at #! line.

If the first line contains the word bash , the file is identified as a bash script. Usually you see #!/bin/bash if it is assumed that the script will be executed directly, but for the shell configuration file using simple # bash will work.

If you want to see the details, this is implemented in $VIMRUNTIME/filetype.vim .

0
Sep 16 2018-11-11T00:
source share

Probably the easiest way to get syntax highlighting in a new file is to simply reload it after writing the shebang line. Simple :w :e will write out the file, reload it and interpret the shebang string just written to provide you with the appropriate syntax highlighting.

0
Jan 24 '14 at 10:32
source share

If you already know the file type before opening the script, or if you create a new script without an extension that is shared, you can pass it to vim on the command line as follows:

 vim -c 'setfiletype sh' path/to/script vim -c 'setfiletype python' path/to/script 
0
Aug 20 '17 at 18:40
source share

When you create a new file, only file name discovery comes into play; content detection ( #!/bin/bash ) does not apply if you enter it after creating a new buffer.

It makes sense to just make :set ft=bash for the first time, and the next time you edit it, #!/bin/bash will automatically set the correct file type.

-one
Aug 31 '12 at 23:00
source share



All Articles