On Sun, 14 May 2006, Jared wrote:

[snip]
Longer and more complicated, but now I can check to see if the file exists
before sourcing it.  However, this also causes a problem: when I try to
source the file containing this function, it gives me an error saying that
it cannot replace the function because it is currently in use.  If I remove
the !, it still gives me an error because it already exists.

Any ideas how to work around this?  Or if you have a different/better way of
doing this, I'm certainly open to suggestions.  :-)

This is on Windows XP.  Thanks.

Encase your function like this:

if !exists("*Source_vimrc")
  function Source_vimrc()
  ...
  endfunction
endif

The reason you're getting the error is because you're sourcing the file
(vimrc) that produced/will override the function (Source_vimrc) you're
using at the moment.

A few words of advice to source your vimrc cleanly:

  1. Define your functions in vimrc with "function!" so that when
     sourced again, the functions will be overwritten.

  2. Keep your autocmds in an augroup so that if it's sourced again, you
     can delete the augroup and redefine it instead of adding more of
     the same autocmds.

  3. If you're using the FuncUndefined autocmd, delete those functions
     that have been defined before.

For examples 2 & 3,

     augroup vimrc
       "delete augroup
       autocmd!
       ...
       silent! delfunction MyFuncInOtherFile
       autocmd FuncUndefined MyFuncInOtherFile source 
$HOME/.vim/autoload/MyFuncInOtherFile.vim
       ...
       autocmd ...
       ...
     augroup END

HTH :)
--
Gerald

Reply via email to