Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim red highlight $( )

Tags:

bash

vim

sh

ubuntu

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

like image 395
Vndtta Avatar asked Feb 06 '13 15:02

Vndtta


1 Answers

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,

  • On Ubuntu, /bin/sh is /bin/dash.
  • On MacOS, /bin/sh is /bin/bash.
  • On Alpine, /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.

like image 156
Andrey Kaipov Avatar answered Oct 01 '22 08:10

Andrey Kaipov