Wednesday, May 30, 2012

Re: auto reload of config files

On Wed, 30 May 2012, HarleyPig wrote:

> I'm using the auto reload trick for my .vimrc:
>
> if !exists( "autoload_vimrc" )
>
> let autoload_vimrc = 1
> autocmd BufWritePost .vimrc source $MYVIMRC
>
> endif

A better way to prevent doubling autocmds is to put them in a group
(better mainly because it prevents using a global variable, but it's
also more idiomatic VimL):

aug AutoloadVimrc
au!
au BufWritePost .vimrc source $MYVIMRC
aug END

Or just force the autocmd to be recreated (and don't worry about the
global var):

autocmd! BufWritePost .vimrc source $MYVIMRC

Personally, I prefer the group, since it lets you disable multiple
commands easily. E.g. if you end up using multiple AutoloadVimrc
commands, you can clear them all by issuing:

:au! AutoloadVimrc


> I'd like to auto reload .vimrc when any of my configuration files are
> modified.
>
> I tried adding all of the following to the if section above:
>
> autocmd BufWritePost .vim/after/plugin/misc.vim source ~/.vimrc
> autocmd BufWritePost .vim/after/plugin/misc.vim source $MYVIMRC
> autocmd BufWritePost misc.vim source ~/.vimrc
> autocmd BufWritePost misc.vim source $MYVIMRC
>
> None seem to work.

In what way do they not work? To verify that your .vimrc is being
reloaded, add this to your .vimrc:

echomsg 'Loaded .vimrc'

Then, when you write your .vimrc, check the messages:

:mess

With your original autocmd, writing my vimrc twice yields:

"~/.vimrc" 233L, 6448C written
Loaded .vimrc
"~/.vimrc" 233L, 6448C written
Loaded .vimrc

Also note that your plugins won't be reloaded just because you source
your ~/.vimrc. They're autoloaded at startup. (See :help load-plugins
for the gory details.) From that help text, maybe you also want:

autocmd BufWritePost misc.vim runtime! plugins/**/*.vim


> My ~/.vim directory is actually a symlink to ~/projects/dot_vim/.vim,
> so I don't really want to hardcode the path. However, just to test, I
> even did that. Still no joy.
>
> Ideally, it would be great to just say if any *.vim file in the .vim
> directory is modified then reload $MYVIMRC.
>
> Any pointers?

Beware the dragons. You're bound to run into some plugin or another
that doesn't cleanly apply twice. In my experience, it's not worth the
hassle of debugging the auto-reload autocmd's. Just find better ways to
start Vim or get it back into the state you want.

E.g.
:help :mksession
:help :mkview
:help :loadview

The way I use Vim, I find that the Ctrl-P plugin makes it fast enough to
open whatever per-project files I'm interested in, so that shutting down
Vim and restarting simply isn't much hassle.

--
Best,
Ben

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment