问题描述
我正在使用 vim 编辑一些 python 文件,最近偶尔会发生在我将缓冲区保存在 vim 中后语法突出显示消失的情况.我尝试重置 syntax on
和 set filetype=python
但无济于事.我根本不知道是什么导致了这个问题,所以现在我只有最少的诊断信息.但是之前有没有人遇到过这种情况,或者事情会在哪里崩溃?
I'm using vim to edit some python files and recently it occurs sporadically that the syntax highlighting disappears after I save the buffer inside vim. I tried to reset syntax on
and set filetype=python
but to no avail. I have no clue of what causes this problem at all, so right now I have minimal diagnostic info. But has anyone encountered this before, or where could things break down?
推荐答案
这不是解决方案,但在评论中很难写/读.
This is not a solution, but it's hard to write/read in the comments.
我的意思是当我开始修复我自己的 .vimrc
时,我在搞乱语法/突出显示,这就是我注意到它的原因.syntax on
/syntax enable
只是为文件加载做准备(在 BufNewFile/BufRead
中添加了很多 au
).因此,如果某些插件在编写文件时弄乱了语法/高亮设置,则必须再次加载该文件才能使所有魔法"发生,仅设置 filetype
是不够的.执行 :au BufRead
,您将看到启动语法时添加的所有自动命令.但是必须加载该文件才能获取所有设置.
看到这个:> syntax-loading
I meant that I was messing around with syntax/highlighting when I start to fix my own .vimrc
, thats why I noticed it.syntax on
/ syntax enable
is only preparing for file load stuff (adding a lot of au
to BufNewFile / BufRead
). So if some plugin is messing with syntax/highlight settings when writing the file, the file must be loaded again for all "magic" to happen, it's not enough with setting filetype
. Do :au BufRead
and you will see all autocommands added when starting syntax. But the file must be loaded then to get all settigs.
See this:> syntax-loading
如果你不想重新加载文件,试试syntax enable
,我认为这与syntax on
不同.
或者也尝试做 :doautocmd filetypedetect BufRead %
,参见 > autocmd-execute
If you don't want to reload the file, try syntax enable
, I think that is different to syntax on
.
Or try also doing :doautocmd filetypedetect BufRead %
, see > autocmd-execute
我不知道是什么导致了问题,是你吗?如果您添加了一些自动命令,或者使用自己的颜色/语法?
否则,在得到解决方案之前,您或许可以尝试在 .vimrc
的末尾添加 autocmd BufWritePost * <使用上述有效的命令>
,使用 augroup 在这种情况下.
I don't know what is causing the problem, can it be you? If you added some autocommands, or doing own colors/syntax?
Otherwise, until you get a solution, you can perhaps try to add autocmd BufWritePost * <with the commands above that works>
at the end of your .vimrc
, use augroup
in that case.
这是一个例子:
augroup myResetSyntax
au!
autocmd BufWritePost * syntax enable | doautocmd filetypedetect BufRead "%"
augroup END
这篇关于文件保存期间语法高亮随机消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!