Tuesday, August 21, 2018

Re: Understanding automatic vimrc sourcing

On Tue, Aug 21, 2018 at 12:22 PM, Oleksii Vilchanskyi
<oleksii.vilchanskyi@gmail.com> wrote:
> Hello,
>
> * What I want:
>
> Be able to do :w after editing $MYVIRC and have vim source the new
> version of config. Basically, just execute `source %` automatically.
>
> * What I have tried (all examples are launched with `vim -u testvimrc`):
>
> So, this works without any error, as supposed:
>> set nocompatible
>> filetype plugin indent on
>> syntax on
>>
>> augroup vimrc
>> autocmd!
>> autocmd BufWritePost testvimrc source testvimrc | echom "Reloaded"
>> augroup END
>
> 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
>
> But at the same time it doesn't abort and I see:
>> Reloaded testvimrc
> , so it looks like the new vimrc gets sourced regardless?..
>
> * My questions:
> 1) Why doesn't this work if wrapped into a function, but does, if
> written out directly?
> 2) Why, if an error occurs, I still see "Reloaded testvimrc"?
> 3) Does it have anything to do with reentrancy?
> 4) Is it possible to make it work through calling a function (since I
> would want to do the same for gvimrc (as well as vimrc), and don't want
> to have repetitive code, instead using a reusable function code)?
>
> Thank you in advance.
> --
> Regards,
> Oleksii Vilchanskyi
> PGP:0x8D3A0E046BDE941F2A53867CE3FD952D48C0B338

See :help E127

To replace a function with an existing name, you need an exclamation
mark after :function. This can be dangerous. IIUC, what happened to
you is that (without an exclamation) Vim gave the error message, did
not replace the function, and later executed the _old_ version of that
function. If you happened not to change that part of your vimrc, it
can be just what you want; but beware of shooting yourself in the
foot.

The approved way to reload your vimrc after changing it is to shutdown
Vim then restart it. Any other method is full of corner cases which
can be hard to debug.

Best regards,
Tony.

--
--
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

---
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 vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment