I was writing a script when I decided to move the functions to a lib file, but when I open the lib file all the $(
and the consecutive )
are red highlighted, here are some examples of the script
TAB="$(printf '\t')"
percent=$(echo "scale=2; $number/$total*100" | bc | sed -e 's/\.[[:digit:]]*//g')
if [[ -z $(grep $site/post $max_lim) ]];then
The filetype is conf but I've set it as sh syntax in .vimrc
Any idea of what is happenning?
Thank you
Edit: Thanks for the quick answers, I found that this line makes vim match the files with the extension specified behind the *
with the syntax sh
au BufReadPost * set syntax=sh
I've also thought that using shebang in the libraries was not allowed, but is a nice solution
Anyway using g:is_bash
in .vimrc returns an error of pattern not found
So what I would like to do is as I only write in bash, to vim recognize any file without extension as bash
In my case, I wanted to preserve #!/bin/sh
as the shebang line because not every system has /bin/bash
available.
Whereas the original Bourne shell may have not supported the $(...)
syntax, most sh
shells nowadays are POSIX-compliant, and the POSIX spec supports this syntax. For example,
/bin/sh
is /bin/dash
./bin/sh
is /bin/bash
./bin/sh
is /bin/ash
.All of which satisfy the POSIX spec. Traditionally, if we'd like to write portable Shell, we should leave the shebang line as #!/bin/sh
. We shouldn't change it to #!/bin/bash
just for syntax highlighting if we're not going to use any Bashisms.
Okay, but what about the erroneous red highlighting? The problem is with Vim interpreting #!/bin/sh
as a reference to the original Bourne shell from 1979 with no support for $(...)
. Maybe this is a testament to Vim's backwards compatibility, or maybe not enough people care. Here's a related GitHub issue describing the same behavior.
In any case, the best solution for me was to set let g:is_posix = 1
in my config. Interestingly, if you look through Vim's runtime files, it's equivalent to setting let g:is_kornshell = 1
.
A brief interesting history on how the Bourne shell was bourne, bourne again as bash
as a substitute for /bin/sh
on Ubuntu, and eventually replaced in favor of dash
can be found at https://askubuntu.com/a/976504.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With