bash vim 红色高亮 $( )
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14732651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vim red highlight $( )
提问by Vndtta
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
当我决定将函数移动到 lib 文件时,我正在编写脚本,但是当我打开 lib 文件时,所有$(和连续的)都以红色突出显示,以下是脚本的一些示例
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
文件类型是 conf 但我已将其设置为 .vimrc 中的 sh 语法
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
编辑:感谢您的快速回答,我发现这一行使 vim 匹配*具有语法 sh后面指定的扩展名的文件
au BufReadPost * set syntax=sh
I've also thought that using shebang in the libraries was not allowed, but is a nice solution
我还认为不允许在库中使用 shebang,但这是一个不错的解决方案
Anyway using g:is_bashin .vimrc returns an error of pattern not found
无论如何g:is_bash在 .vimrc 中使用会返回找不到模式的错误
So what I would like to do is as I only write in bash, to vim recognize any file without extension as bash
所以我想做的是因为我只用 bash 编写,vim 将任何没有扩展名的文件识别为 bash
回答by chepner
The syntax file for shactually handles several different kinds of shell syntax: bash, ksh, and plain old sh. Since your conf file isn't recognized as bashor ksh, it falls back to sh. $(...)isn't a valid construct in sh, so it is highlighted as an error.
对于语法文件sh实际上处理的几种不同类型的外壳语法:bash,ksh,和普通的旧的sh。由于您的 conf 文件未被识别为bash或ksh,因此它回退到sh. $(...)不是 中的有效构造sh,因此将其突出显示为错误。
To fix this, you can make sure "g:is_bash" is set for the file, so that the shsyntax script will know your file should be highlighted as bashcode. Please edit your question to include what you added to your .vimrcto make the file use shsyntax highlighting. This will make it easier to suggest the correct way of setting "g:is_bash".
要解决此问题,您可以确保为文件设置了“g:is_bash”,以便sh语法脚本知道您的文件应该作为bash代码突出显示。请编辑您的问题以包含您添加的内容,.vimrc以使文件使用sh语法突出显示。这将使建议设置“g:is_bash”的正确方法更容易。
UPDATE: As Alok commented, you should be able to add the following to the file
更新:正如 Alok 评论的那样,您应该能够将以下内容添加到文件中
#!/bin/bash
to let vimknow the correct syntax highlighting to use as well.
让vim知道正确的语法突出显示也使用。
回答by Andrey Kaipov
In my case, I wanted to preserve #!/bin/shas the shebang line because not every system has /bin/bashavailable.
就我而言,我想保留#!/bin/sh为 shebang 行,因为并非每个系统都/bin/bash可用。
Whereas the original Bourne shell may have not supported the $(...)syntax, most shshells nowadays are POSIX-compliant, and the POSIX spec supports this syntax. For example,
尽管原始的 Bourne shell 可能不支持该$(...)语法,但sh现在大多数shell 都符合 POSIX 标准,并且POSIX 规范支持此语法。例如,
- On Ubuntu,
/bin/shis/bin/dash. - On MacOS,
/bin/shis/bin/bash. - On Alpine,
/bin/shis/bin/ash.
- 在 Ubuntu 上,
/bin/sh是/bin/dash. - 在 MacOS 上,
/bin/sh是/bin/bash. - 在阿尔卑斯山上,
/bin/sh是/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/bashjust for syntax highlighting if we're not going to use any Bashisms.
所有这些都满足 POSIX 规范。传统上,如果我们想编写可移植的 Shell,我们应该将 shebang 行保留为#!/bin/sh. #!/bin/bash如果我们不打算使用任何 Bashisms,我们不应该将它更改为仅用于语法突出显示。
Okay, but what about the erroneous red highlighting? The problem is with Vim interpreting #!/bin/shas a reference to the original Bourne shell from 1979with no support for $(...). Maybe this is a testament to Vim's backwards compatibility, or maybe not enough people care. Here's a related GitHub issuedescribing the same behavior.
好的,但是错误的红色突出显示怎么办?问题在于 Vim 将1979 年的原始 Bourne shell解释#!/bin/sh为不支持. 也许这证明了 Vim 的向后兼容性,或者可能没有足够的人关心。这是描述相同行为的相关 GitHub 问题。$(...)
In any case, the best solution for me was to set let g:is_posix = 1in my config. Interestingly, if you look through Vim's runtime files, it's equivalent to setting let g:is_kornshell = 1.
无论如何,对我来说最好的解决方案是let g:is_posix = 1在我的配置中进行设置。有趣的是,如果你查看 Vim 的运行时文件,它相当于设置let g:is_kornshell = 1.
A brief interesting history on how the Bourne shell was bourne, bourne again as bashas a substitute for /bin/shon Ubuntu, and eventually replaced in favor of dashcan be found at https://askubuntu.com/a/976504.
可以在https://askubuntu.com/a/976504上找到有关 Bourne shell 如何成为 bourne、bourne 再次作为Ubuntu 上bash的替代品/bin/sh并最终被取代的有趣历史。 dash

