Re: FW: Understanding automatic vimrc sourcing

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Re: FW: Understanding automatic vimrc sourcing

Christian Brabandt
Forwarding to vim_use.

<[hidden email]> wrote:

On 8/22/18 7:59 AM, Christian Brabandt wrote:

> On Di, 21 Aug 2018, Oleksii Vilchanskyi wrote:
>> However, if I try wrapping it in a function, E127 occurs:
>>> set nocompatible
>>> filetype plugin indent on
>>> syntax on
>>> augroup vimrc
>>> autocmd!
>>> autocmd BufWritePost testvimrc call s:ReloadRC("testvimrc")
>>> augroup END
>>> function! s:ReloadRC(rc)
>>> exec 'source ' . a:rc | echom "Reloaded " . a:rc
>>> endfunction
>> Whenever I do :w, I see:
>>> E127: Cannot redefine function <SNR>1_ReloadRC: It is in use
> Yes, what happens when you execute the function is, you reload your
> .vimrc which will redefine your function, while the function is
> executed. This is not allowed and causes the error message. Either do
> not use a function, or define your function inside e.g.
> ~/.vim/plugins/reload.vim or similar.

Thank you, the very day I sent the mail people on IRC showed me an
example of how this can look as a plugin (basically, copy all the
information regarding that into a plugin and put it under the guard),
just as you suggested. So, I ended up with this:

> if &compatible || exists('g:loaded_rc_auto_reload')
> finish
> else
> let g:loaded_rc_auto_reload = 1
> endif
> function! s:ReloadRC() abort
> execute 'source ' . $MYVIMRC
> redraw! | echomsg 'Reloaded ' . $MYVIMRC
> if has('gui_running')
> execute 'source ' . $MYGVIMRC
> redraw! | echomsg 'Reloaded ' . $MYGVIMRC
> endif
> endfunction
> augroup vimrc_auto_reload
> autocmd!
> autocmd BufWritePost $MYVIMRC call s:ReloadRC()
> autocmd BufWritePost $MYGVIMRC call s:ReloadRC()
> augroup EN
Although with the code above, I have noticed that whenever I run gvim,
neither vimrc nor gvimrc get sourced when I edit gvimrc, but both get
sourced if I edit vimrc. With terminal vim it's the same -- if I edit
vimrc, it gets sourced (and only it, since it's terminal vim), if I edit
gvimrc, nothing gets sourced. So, I suspect there is some issue with
> autocmd BufWritePost $MYGVIMRC call s:ReloadRC()
, looks like the condition is not getting triggered, or I don't
understand something.

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

You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit