Vim 不一致地语法高亮 bash 文件
当我用 vim 打开一些 bash 脚本文件时,它有时会将它们识别为 conf
文件,没关系,我可以通过使用 将文件类型设置为
。sh
来纠正它: setf sh
太棒了,只是我注意到这并不能完全解决问题:
请注意,shopt
在左侧正确突出显示,但在右侧却没有突出显示,我在右侧手动将文件类型设置为 sh
。
这意味着,当 vim 将文件识别为 bash
或 sh
时,它会将文件类型设置为 sh
,但随后会执行一些我所要求的额外步骤当我手动设置文件类型时,我没有这样做。
任何人都知道这可能是什么,以及我该如何解决它?
When I open some bash script files with vim it sometimes identifies them as conf
files, that's okay, I can just correct that by setting the filetype to sh
with :setf sh
.
That great, except I've noticed that this doesn't fix things entirely:
Notice that shopt
is properly highlighted on the left, but not on the right, where I manually set the filetype to sh
.
This means that when a file is identified as bash
or sh
by vim, it sets the filetype to sh
but then does some extra steps that I'm not doing when I set the filetype manually.
Any one know what that might be, and how I could fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
默认情况下,vim 已经识别许多文件类型。大多数都是通过文件扩展名来工作的,但在这种情况下,vim 也会分析文件的内容来猜测正确的类型。
vim 自动设置特定文件名的文件类型,例如
.bashrc
、.tcshrc
等。但扩展名为 .sh 的文件将被识别为 csh、ksh 或 bash 脚本。为了确定这到底是什么类型的脚本,vim 读取文件的第一行来查看 #!线。如果第一行包含单词
bash
,则该文件被识别为 bash 脚本。如果脚本要直接执行,通常您会看到#!/bin/bash
,对于其他一些 shell 配置文件,您应该使用文件扩展名.bash
。vim 中的帮助也在
:help ft-bash-syntax
中对此进行了解释。您还可以在 .vimrc 中使用let g:is_bash=1
使 bash 语法默认突出显示所有带有filetype=sh
的文件。如果你想看细节,这是在$VIMRUNTIME/filetype.vim
中实现的。vim already recognizes many file types by default. Most of them work by file extensions, but in a case like this, vim will also analyze the content of the file to guess the correct type.
vim sets the filetype for specific file names like
.bashrc
,.tcshrc
, etc. automatically. But a file with a.sh
extension will be recognized as either csh, ksh or bash script. To determine what kind of script this is exactly, vim reads the first line of the file to look at the #! line.If the first line contains the word
bash
, the file is identified as a bash script. Usually you see#!/bin/bash
if the script is meant to be executed directly, for some other shell configuration file you should use the file extensions.bash
.The help in vim explains this as well at
:help ft-bash-syntax
. You can also uselet g:is_bash=1
in your .vimrc to make bash syntax highlighting the default for all files withfiletype=sh
. If you want to look at the details, this is implemented in$VIMRUNTIME/filetype.vim
.事实证明,
syntax/sh.vim
包含 Korn、Bash 和 sh 的特定突出显示,您只需告诉它你正在使用哪个。这分别通过b:is_kornshell
、b:is_bash
和b:is_sh
完成。根据情况,我认为我将使用以下内容:
ftdetect/bash.vim:
Modeline:
Key Mapping
It turns out that
syntax/sh.vim
includes specific highlighting for Korn, Bash and sh, you just have to tell it which you're using. This is done withb:is_kornshell
,b:is_bash
andb:is_sh
respectively.Depending on the situation I figure I'll use the following:
ftdetect/bash.vim:
Modeline:
Key Mapping
与 Peter Coulton 的解决方案类似,并在“文件类型”的“new-filetype”部分中记录了替代方案,Vim 帮助
~/.vim/filetype.vim
文件可以包含以下代码:这种方法具有以下含义:
~/.vim/filetype.vim
目录下有一个~/.vim/filetype.vim
文件,而不是每种文件类型都有一个文件。b:is_bash
变量设置为缓冲区本地变量,而不是通过将其引用为g:is_bash
来设置全局变量。Similar to Peter Coulton's solution and documented as well as an alternative in the section "new-filetype" of the "filetype" Vim help the
~/.vim/filetype.vim
file could contain the following code:This approach has the following implications:
~/.vim/filetype.vim
file instead of one for each file type under the~/.vim/ftdetect
directory.b:is_bash
variable is set local to the buffer as opposed to global by referring to it asg:is_bash
.尝试查看有效的语法设置
(我希望第一个窗口说
bash
,第二个窗口说sh
...?)也尝试使用 synatx 同步进行清理:
一般来说,
需要更多信息
PS。
远景,但其他一些突出显示可能会产生干扰:
Try viewing the effective syntax setting
(I kind of expect the first window to say
bash
, and the second to saysh
...?)Also try mucking with the synatx synchronisation:
In general
for more information
PS.
A long shot, but some other highlighting might be interfering: