[英]How to prevent global defaults from overriding 'filetype=vim' specific settings in .vimrc when automatically sourcing .vimrc?
I have my .vimrc
automatically being sourced with: 我的
.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: 稍后,我使用
FileType
事件覆盖上面的间距默认值,具有特定于文件类型的间距:
" 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
The problem I am having is that sourcing my .vimrc
will set the default values, but will not trigger the FileType
event. 我遇到的问题是,获取我的
.vimrc
将设置默认值,但不会触发FileType
事件。 This means that when I do a :write
to my .vimrc
while editting a file with filetype-specific spacing , the filetype-specific spacings get overridden by the defaults! 这意味着当我执行a
:write
在编辑具有特定于文件类型的间距的文件时 :write
我的.vimrc
,特定于文件类型的间距会被默认值覆盖!
UPDATE : As pointed out by Ingo, the issue only arises in .vimrc
and does not occur in any other files. 更新 :如Ingo所指出的,该问题仅出现在
.vimrc
,并且不会出现在任何其他文件中。
As an example, when I am working on my .vimrc
, initially the file-specific spacing ( ts=4 sts=4 sw=4 expandtab
) is enabled, but after I make some arbitrary change in my .vimrc
, the default spacing ( tabstop=2 softtabstop=2 shiftwidth=2 expandtab
) is loaded but my file-specific spacing is NOT loaded (since the FileType
event was not triggered by a write to my .vimrc
) leaving me with the default spacing! 例如,当我在
.vimrc
上工作时,最初启用了特定于文件的间距( ts=4 sts=4 sw=4 expandtab
),但是在我的.vimrc
进行了一些任意更改后,默认间距( tabstop=2 softtabstop=2 shiftwidth=2 expandtab
)已加载,但我的文件特定间距未加载(因为FileType
事件未被写入我的.vimrc
触发)让我保留默认间距!
How can I override the default spacing with filetype specific spacings and preserve these even when auto-sourcing .vimrc
? 如何使用特定于文件类型的间距覆盖默认间距,并在自动采购
.vimrc
时保留这些间距?
If possible, I would like to accomplish this without getting rid of the autocmd's. 如果可能的话,我想在不摆脱autocmd的情况下实现这一目标。 Is that possible?
那可能吗?
My hunch is that I should somehow manually trigger the FileType event when sourcing .vimrc
Can this be done and is this the recommended approach? 我的预感是,我应该以某种方式在采购时手动触发FileType事件
.vimrc
可以这样做,这是推荐的方法吗?
Though you've very clearly described the problem, I doubt that this is happening. 虽然你已经非常清楚地描述了这个问题,但我怀疑这是否正在发生。
As you're correctly using :setlocal
for the filetype-specific settings, and :set
in your ~/.vimrc
, the sourcing of the latter will only affect the global defaults (and the current buffer, which is your .vimrc
during the BufWritePost
event). 当你使用得正确
:setlocal
为特定文件类型设置和:set
在您~/.vimrc
,后者的采购只会影响全局默认值(和当前的缓冲区,这是你.vimrc
期间BufWritePost
事件)。 To affect other buffers, there would need to be a :bufdo
command in your .vimrc
. 要影响其他缓冲区,需要在
.vimrc
使用:bufdo
命令。
You can check this with 你可以用它来检查
:verbose set ts? sts? sw? et?
To avoid that your .vimrc
settings are overridden with the global settings, you can append this: 要避免使用全局设置覆盖
.vimrc
设置,可以附加以下内容:
if expand('%:t') ==# '.vimrc'
setlocal filetype=vim
endif
I ended up solving this issue by grouping the sourcing autocmd
with another autocmd
with the same event 我最终通过将采购
autocmd
与另一个具有相同事件的autocmd
分组来解决此问题
" Reload changes to .vimrc automatically
autocmd BufWritePost ~/.vimrc source ~/.vimrc | setlocal filetype=vim
I had tried this earlier, but I was also using the AirlineRefresh
command for vim-airline as such: 我之前尝试过这个,但我也在使用Vim-airline的
AirlineRefresh
命令:
autocmd BufWritePost ~/.vimrc source ~/.vimrc | AirlineRefresh | setlocal filetype=vim
which was giving me this error: 这给了我这个错误:
E488: Trailing characters: AirlineRefresh | setlocal filetype=vim
Switching the order of AirlineRefresh
and set local filetype=vim
fixed the error and results in the desired behavior: 切换
AirlineRefresh
的顺序并set local filetype=vim
修复了错误并导致所需的行为:
autocmd BufWritePost ~/.vimrc source ~/.vimrc | setlocal filetype=vim | AirlineRefresh
:bufdo e
will do it but I couldn't just add it to my .vimrc
. :bufdo e
会这样做,但我不能把它添加到我的.vimrc
。 It will also remove syntax highlighting which is not so good. 它还将删除不太好的语法突出显示。
:h bufdo
execute the command in each buffer in the buffer list :h bufdo
在缓冲区列表中的每个缓冲区中执行命令
:h :e
re-edit the current file :h :e
重新编辑当前文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.