gpt4 book ai didi

vim - 在自动获取 .vimrc 时,如何防止全局默认值覆盖 .vimrc 中的 'filetype=vim' 特定设置?

转载 作者:行者123 更新时间:2023-12-04 19:36:31 25 4
gpt4 key购买 nike

上下文

我的 .vimrc 自动获取:

autocmd! BufWritePost ~/.vimrc source ~/.vimrc

I also set defaults for spacing:

set tabstop=2 softtabstop=2 shiftwidth=2 expandtab

Later on, I use the FileType event to override the spacing default above with filetype-specific spacing:

" Syntax of these languages is fussy over tabs & spaces¬
autocmd FileType make setlocal ts=8 sts=8 sw=8 noexpandtab¬
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab¬

" Customisations based on house-style (arbitrary)
autocmd FileType sh setlocal ts=4 sts=4 sw=4 expandtab
autocmd FileType vim setlocal ts=4 sts=4 sw=4 expandtab

问题

我遇到的问题是采购我的 .vimrc 将设置默认值,但不会触发 FileType 事件。这意味着当我对我的 .vimrc 执行 :write 同时编辑具有文件类型特定间距的文件时,文件类型特定的间距得到被默认值覆盖!

更新:正如 Ingo 所指出的,该问题仅出现在 .vimrc 中,不会出现在任何其他文件中。

例子

例如,当我在处理我的 .vimrc 时,最初启用了文件特定的间距 (ts=4 sts=4 sw=4 expandtab) , 但在我对我的 .vimrc 进行一些任意更改后,加载了默认间距 (tabstop=2 softtabstop=2 shiftwidth=2 expandtab) 但我的文件特定间距未加载(因为 FileType 事件不是由写入我的 .vimrc 触发的)留给我默认间距!

我的问题

如何使用文件类型特定的间距覆盖默认间距并在自动采购 .vimrc 时保留这些间距?

如果可能的话,我想在不删除 autocmd 的情况下完成此操作。这可能吗?

我的直觉是,我应该在采购 .vimrc 时以某种方式手动触发 FileType 事件。这可以做到吗?这是推荐的方法吗?

最佳答案

虽然您已经非常清楚地描述了问题,但我怀疑这是否正在发生。

当您正确使用 :setlocal 进行特定于文件类型的设置,并在您的 ~/.vimrc 中使用 :set 时,后者的采购只会影响全局默认值(以及当前缓冲区,即 BufWritePost 事件期间的 .vimrc)。要影响其他缓冲区,您的 .vimrc 中需要一个 :bufdo 命令。

你可以检查一下

:verbose set ts? sts? sw? et?

为了避免你的 .vimrc 设置被全局设置覆盖,你可以附加:

if expand('%:t') ==# '.vimrc'
setlocal filetype=vim
endif

关于vim - 在自动获取 .vimrc 时,如何防止全局默认值覆盖 .vimrc 中的 'filetype=vim' 特定设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25374487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com