使用 split 命令时 VIM 失去语法高亮

发布于 2024-12-07 08:26:47 字数 412 浏览 0 评论 0原文

因此,我创建了自己的语法突出显示文件,如果只打开一个文件,它效果很好。但是,如果我执行 :split otherFile ,则打开的另一个缓冲区没有语法突出显示。我尝试了各种方法,例如 :syntax on 等。可能是什么问题?

我正在运行 Ubuntu 11.04,64 位版本。

VIM版本:VIM - Vi IMproved 7.3 (2010年8月15日,编译于2011年3月24日07:07:34)

我创建了一个简单的语法高亮文件,并将其放在 ~/.vim/plugin/syntax.vim

的最后一行语法高亮文件是 let b:current_syntax = "sth"。 我没有进行任何类型的连接,例如在 .vimrc 中指定文件位置,语法会自动工作(对于打开的一个文件)。

So I have created my own syntax highlighting file and it works well if there is only one file opened. However, if I do :split otherFile, the other buffer that gets opened does not have syntax highlighting. I have tried various things, like :syntax on etc. What can be the problem?

I am running Ubuntu 11.04, 64-bit version.

VIM version: VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Mar 24 2011 07:07:34)

I have created a simple syntax highlighting file and put it in ~/.vim/plugin/syntax.vim

The last line of the syntax highlighting file is let b:current_syntax = "sth".
I haven't done any kind of wiring, like specifying the file location in .vimrc, the syntax works automatically (for one file opened).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

物价感观 2024-12-14 08:26:47

我最近遇到了这个问题,而且更普遍。也就是说,所有文件类型都会出现该问题。经过一番调试后,我发现文件类型已被正确识别,但对于新的分割,语法不知何故变得未设置。

我仍然不能 100% 确定这是如何发生的,但对于未来的 Google 访问者,我会写下为我解决问题的方法:我移动了 set syntax = on早在我的 .vimrc 中。经过多年的积累,带有 set syntax = on 的行已逐渐下降,直到低于其他一些内容。将其移回(几乎)文件顶部对我来说解决了问题。这是我的 .vimrc 的开头现在的样子:

" First two uncommented lines only useful if you use Tim Pope's vim-pathogen.  
" https://github.com/tpope/vim-pathogen
execute pathogen#infect()
execute pathogen#helptags()

" Most general settings first
set nocompatible            " Vim rather than Vi; implied by this file
syntax on                   " Syntax highlighting on
filetype plugin indent on   " Filetype detection

" ... etc.

I had this problem recently and much more generally. That is, the problem appeared for all filetypes. After debugging a bit, I discovered that the filetype was being properly recognized, but that for new splits, syntax was somehow becoming unset.

I'm still not 100% sure how this happened, but for future Google visitors, I'll write down what fixed the trouble for me: I moved set syntax = on much earlier in my .vimrc. After years of accretion, the line with set syntax = on had drifted down until it was below a number of other things. Moving it back up to (nearly) the top of the file fixed things for me. Here's what the start of my .vimrc looks like now:

" First two uncommented lines only useful if you use Tim Pope's vim-pathogen.  
" https://github.com/tpope/vim-pathogen
execute pathogen#infect()
execute pathogen#helptags()

" Most general settings first
set nocompatible            " Vim rather than Vi; implied by this file
syntax on                   " Syntax highlighting on
filetype plugin indent on   " Filetype detection

" ... etc.
溺ぐ爱和你が 2024-12-14 08:26:47

复活这个,因为我刚刚观察到同样的行为。我找到的解决方案是将 filetype on 设置添加到我的 .vimrc 文件中。 fwiw,我在 syntax on 之后立即添加了它。 syntax on 恰好已经位于我的 .vimrc 文件的顶部。所以,这是我的 .vimrc 的前几行:

1 " Activates Syntax Highlighting
2 syntax on
3 
4 " Filetype on
5 filetype on

Resurrecting this, as I just observed the same behavior. The solution that I found was to add the filetype on setting to my .vimrc file. fwiw, I added it immediately after syntax on. syntax on happened to be at the top of my .vimrc file already. So, here are the first few lines of my .vimrc:

1 " Activates Syntax Highlighting
2 syntax on
3 
4 " Filetype on
5 filetype on
不即不离 2024-12-14 08:26:47

语法文件属于 ~/.vim/syntax/sth.vim,而不是 ~/.vim/plugin/syntax.vim。后者仅在启动时获取一次,这可能就是它仅适用于第一个加载的文件的原因。

为了使您的语法变得活跃,您需要 :setf sth,或者将相应的模型行插入到您的文件中,或者为您的语法编写文件类型检测以自动执行该操作。

Syntax files belong into ~/.vim/syntax/sth.vim, not ~/.vim/plugin/syntax.vim. The latter is sourced only once on startup, that's probably why it only works for the first loaded file.

For your syntax to become active, you need to :setf sth, or insert a corresponding modeline into your files, or write a filetype detection for your syntax to automate that.

比忠 2024-12-14 08:26:47

就我而言,.vimrc 中缺少 syntax=on

In my case syntax=on was missing from .vimrc

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