You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

autocmd无法识别*.ott文件,手动设置filetype才触发语法高亮的问题

.ott文件Vim语法高亮失效问题的解决

核心原因:默认文件类型规则冲突

.ott是OpenDocument文本模板的标准扩展名,Vim自带的文件类型检测规则中,已经将.ott关联到了odt(OpenDocument文本)类型,这会覆盖你自定义的ott类型设置。而.ot不是标准扩展名,没有默认规则,所以你的自定义设置能正常生效。

验证冲突的步骤

打开.ott文件后,执行以下命令查看当前filetype的设置来源:

:verbose set filetype?

如果输出显示filetype=odt且指向Vim自带的ftdetect脚本路径,即可确认是默认规则覆盖了你的配置。

解决方法

方法1:提升自定义规则优先级

将你的ftdetect/ott.vim移动到~/.vim/after/ftdetect/(Neovim对应~/.config/nvim/after/ftdetect/)目录下。after目录下的脚本会在所有默认规则之后加载,强制覆盖默认关联。

方法2:强制设置filetype

修改自动命令,加上!强制覆盖已有的filetype设置:

au BufRead,BufNewFile *.ott set filetype=ott!

方法3:先取消默认关联

在vimrc中先清除.ott的默认文件类型关联,再设置自定义规则:

au BufRead,BufNewFile *.ott set filetype=
au BufRead,BufNewFile *.ott set filetype=ott

原始配置代码

au BufRead,BufNewFile *.ott set filetype=ott
" a.ott does not get highlighted
" Replace *.ott with *.ot, then a.ot gets highlighted

内容的提问来源于stack exchange,提问作者Li-yao Xia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 00:27:08