编辑 vim 中的 bash 脚本,<<< 括号导致颜色损坏

Editing bash script in vim, <<< brackets cause colour corruption

我继承了几个 bash 脚本,它们使用以下语法将字符串读入数组:

read -a arr <<<$line

但是,这会导致 vim 中的颜色格式被破坏。有人可以建议快速修复吗?

更新:

忽略脚本的内容,但注意“<<<”字符后的颜色变化(即 echo 语句为紫色):

如果缺少 #!/bin/bash shebang 行,就会发生这种情况。 Vim 将脚本解释为纯 sh 而不是 bash。 <<< 是 bash 主义。

来自sh.syntax

" trying to answer the question: which shell is /bin/sh, really?
" If the user has not specified any of g:is_kornshell, g:is_bash, g:is_posix, g:is_sh, then guess.
if !exists("g:is_kornshell") && !exists("g:is_bash") && !exists("g:is_posix") && !exists("g:is_sh")
 let s:shell = ""
 if executable("/bin/sh")
  let s:shell = resolve("/bin/sh")
 elseif executable("/usr/bin/sh")
  let s:shell = resolve("/usr/bin/sh")
 endif
 if     s:shell =~ 'bash$'
  let g:is_bash= 1
 elseif s:shell =~ 'ksh$'
  let g:is_kornshell = 1
 elseif s:shell =~ 'dash$'
  let g:is_posix = 1
 endif
 unlet s:shell
endif

" Here Strings: {{{1
" =============
" available for: bash; ksh (really should be ksh93 only) but not if its a posix
if exists("b:is_bash") || (exists("b:is_kornshell") && !exists("g:is_posix"))
 syn match shRedir "<<<"        skipwhite       nextgroup=shCmdParenRegion
endif

看起来 vim 支持 sh 语法文件中的 <<< 结构,但只有当它认为文件是 bash 脚本时才会识别它。使 vim 认为该文件是 bash:

的一些方法
  1. 给它一个 #!/bin/bash 的 shebang,而不仅仅是 #!/bin/sh(如果文件无论如何使用 bash 特定的构造,这是最佳实践)

  2. 在您正在编辑的系统上有 /bin/sh 是指向 bash (meh)

  3. 的符号链接
  4. let g:is_bash = 1 添加到您的 .vimrc 以使 vim 假定 shell 脚本默认为 bash。

为了让其他人更容易知道问题出在哪里,我用 vim 7.4.640OS X Yosemite 10.10.5 上做了一个快速测试,下面是屏幕截图:

正如其他答案所指出的,使用 #!/bin/bash 作为 shebang 行解决了我的问题。