.vimrc 的重新加载时间逐渐变慢 [英] Progressively slower reloading time of .vimrc

查看:55
本文介绍了.vimrc 的重新加载时间逐渐变慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 vim 启动时间大约是半秒(用--startuptime"测试),但是在我通过 source 重新加载 vimrc 几次后,它随后变慢了.我没有以系统的方式调试 .vimrc,所以我不确定如何继续.设置 verbose 有助于了解正在发生的事情,我几乎可以肯定 .vimrc 被加载了不止一次.(尤其是 filetype.vimftplugin.vim)每当我按 Ctrl-C 停止挂起时,我都会在 中收到错误filetype.vim,我认为这是因为 vim 大部分时间都在尝试加载 filetype.vim.我看到的最可能的罪魁祸首是 .vimrc 的自动重新加载:

My boot time for vim is about a half second (tested with "--startuptime"), but after I reload vimrc a couple times via source, it gets slower subsequently. I have not debugged .vimrc in a systematic way, so I am not sure how to proceed. Setting verbose helps to kind of see what is going on and I am almost certain that the .vimrc is being loaded more than once. (especially filetype.vim and ftplugin.vim) Whenever I press Ctrl-C to stop the hang, I get an error in filetype.vim, which I think is because vim spends most time trying to load filetype.vim. The most probable culprit I see is the auto reload of .vimrc:

if has("autocmd")
    autocmd bufwritepost .vimrc source $MYVIMRC "auto source vimrc
endif

我怎样才能阻止这种情况发生?

How could I stop this from happening?

推荐答案

这里的罪魁祸首是您对 autocmd 的使用.当您使用

The culprit here is your use of autocmd. When you define an auto-command with

autocmd <Event> <Action>

vim 定义了一个新的自动命令不管现有的命令.因此,当您的 .vimrc 中有几个这样的自动命令并重复获取它(在这种情况下,每次写入)时,您最终会定义成百上千的自动命令,它们都重复执行相同的操作.很快这会导致您的 vim 会话崩溃并减慢您的 vim 会话速度,而这正是您一直注意到的.

vim defines a new auto command regardless of existing ones. So when you have several such auto commands in your .vimrc and repeatedly source it (in this case, for every write), you end up defining hundreds and thousands of auto commands which all do the same thing repeatedly. Very soon this will blow up and slow your vim session, which is what you've been noticing.

您需要做的是将您的自动命令分组,然后使用 autocmd! 清除该组的定义.举个简单的例子:

What you need to do instead, is to group your auto commands and then clear the definitions for that group with autocmd!. As a simple example:

augroup Group1  
    autocmd!
    autocmd <Event1> <Action1>
    autocmd <Event2> <Action2>
    ...    
augroup END

您可以拥有任意数量的组,这样您就可以通过类似的操作/事件来组织它们.

You can have as many groups as you like, which allows you to organize them by similar actions/events.

这篇关于.vimrc 的重新加载时间逐渐变慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆