Progressively slower reloading time of .vimrc

99封情书 提交于 2019-12-21 07:16:01

问题


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?


回答1:


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

autocmd <Event> <Action>

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.

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.




回答2:


This is my solution that also clears previous mappings:

augroup VimrcGroup
  autocmd!
  " Make changes effective after saving .vimrc. Beware that autocommands are
  " duplicated if .vimrc gets sourced again, unless they are wrapped in an
  " augroup and the autocommands are cleared first using 'autocmd!'
  autocmd bufwritepost $MYVIMRC call OnSavingVimrc()
augroup END

" Avoid infinite loops
if !exists("*OnSavingVimrc")
  function! OnSavingVimrc()
    " Clear previous mappings, they don't go away automatically when sourcing vimrc
    mapclear
    echo "Sourcing Vimrc after saving it"
    source $MYVIMRC
  endfunction
endif


来源:https://stackoverflow.com/questions/15353988/progressively-slower-reloading-time-of-vimrc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!