Vim 不一致的语法高亮显示 bash 文件 [英] Vim inconsistently syntax highlighting bash files

查看:40
本文介绍了Vim 不一致的语法高亮显示 bash 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用 vim 打开一些 bash 脚本文件时,它有时会将它们标识为 conf 文件,没关系,我可以通过将文件类型设置为 sh 并使用 <代码>:setf sh.

太好了,但我注意到这并不能完全解决问题:

请注意,shopt 在左侧正确突出显示,但在右侧没有突出显示,我手动将文件类型设置为 sh.

这意味着当一个文件被 vim 识别为 bashsh 时,它会将文件类型设置为 sh 但随后会执行一些额外的操作手动设置文件类型时未执行的步骤.

有人知道这可能是什么吗,我该如何解决?

解决方案

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中实现的.

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?

解决方案

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 use let g:is_bash=1 in your .vimrc to make bash syntax highlighting the default for all files with filetype=sh. If you want to look at the details, this is implemented in $VIMRUNTIME/filetype.vim.

这篇关于Vim 不一致的语法高亮显示 bash 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆