Vim 不一致地语法高亮 bash 文件

发布于 2024-12-05 04:33:11 字数 480 浏览 11 评论 0原文

当我用 vim 打开一些 bash 脚本文件时,它有时会将它们识别为 conf 文件,没关系,我可以通过使用 将文件类型设置为 sh 来纠正它: setf sh

太棒了,只是我注意到这并不能完全解决问题:

并排的两个文件,具有不同的突出显示

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

这意味着,当 vim 将文件识别为 bashsh 时,它会将文件类型设置为 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:

Two files side-by-side with different highlighting

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风吹短裙飘 2024-12-12 04:33:11

默认情况下,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 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.

徒留西风 2024-12-12 04:33:11

事实证明, syntax/sh.vim 包含 KornBashsh 的特定突出显示,您只需告诉它你正在使用哪个。这分别通过 b:is_kornshellb:is_bashb:is_sh 完成。

根据情况,我认为我将使用以下内容:

ftdetect/bash.vim:

au BufRead,BufNewFile *bash* let g:is_bash=1
au BufRead,BufNewFile *bash* setf sh

Modeline:

# vim:let g:is_bash=1:set filetype=sh:

Key Mapping

nmap <silent> <leader>b :let g:is_bash=1<cr> :setf sh<cr> 

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 with b:is_kornshell, b:is_bash and b:is_sh respectively.

Depending on the situation I figure I'll use the following:

ftdetect/bash.vim:

au BufRead,BufNewFile *bash* let g:is_bash=1
au BufRead,BufNewFile *bash* setf sh

Modeline:

# vim:let g:is_bash=1:set filetype=sh:

Key Mapping

nmap <silent> <leader>b :let g:is_bash=1<cr> :setf sh<cr> 
悲喜皆因你 2024-12-12 04:33:11

与 Peter Coulton 的解决方案类似,并在“文件类型”的“new-filetype”部分中记录了替代方案,Vim 帮助 ~/.vim/filetype.vim 文件可以包含以下代码:

if exists("did_load_filetypes")
  finish
endif

augroup filetypedetect
  au! BufRead,BufNewFile *bash* let b:is_bash = 1 | setfiletype sh
augroup END

这种方法具有以下含义:

  1. ~/.vim/filetype.vim 目录下有一个 ~/.vim/filetype.vim 文件,而不是每种文件类型都有一个文件。
  2. 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:

if exists("did_load_filetypes")
  finish
endif

augroup filetypedetect
  au! BufRead,BufNewFile *bash* let b:is_bash = 1 | setfiletype sh
augroup END

This approach has the following implications:

  1. There is one ~/.vim/filetype.vim file instead of one for each file type under the ~/.vim/ftdetect directory.
  2. The b:is_bash variable is set local to the buffer as opposed to global by referring to it as g:is_bash.
じее 2024-12-12 04:33:11

尝试查看有效的语法设置

:windo echo b:current_syntax

(我希望第一个窗口说 bash,第二个窗口说 sh...?)

也尝试使用 synatx 同步进行清理:

:windo syn sync fromstart
:windo syn sync minlines=300

一般来说,

:he syn-sync

需要更多信息


PS。

远景,但其他一些突出显示可能会产生干扰:

:windo se @/=''
:match none
:2match none
:3match none

Try viewing the effective syntax setting

:windo echo b:current_syntax

(I kind of expect the first window to say bash, and the second to say sh...?)

Also try mucking with the synatx synchronisation:

:windo syn sync fromstart
:windo syn sync minlines=300

In general

:he syn-sync

for more information


PS.

A long shot, but some other highlighting might be interfering:

:windo se @/=''
:match none
:2match none
:3match none
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文