Re: sourcing vimrc files
On 5/14/2006 8:41 PM, Gerald Lai wrote: > Encase your function like this: > > if !exists("*Source_vimrc") > function Source_vimrc() > ... > endfunction > endif Thanks, Gerald. This worked perfectly. Also, thanks to everyone else for their suggestions. I appreciate the feedback and read up on the suggested commands, but this ended up being the easiest. :-) -- Jared
Re: sourcing vimrc files
On Sun, May 14, 2006 at 06:41:39PM -0700, Gerald Lai wrote: > > 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. Those are all good suggestions (although testing exists("*Source_vimrc") seems a little complicated). One more possibility is to put all function definitions, along with anything else that never needs to be re-done, below a few lines like these: " Skip the rest of the file if it has already been re-loaded. if exists("s:deja_vu") finish endif let s:deja_vu = 1 function Source_vimrc() " no need for the !, this should never be read twice. ... BTW, another post on this thread pointed out that your vimrc file knows its own name. As of vim 7.0, this name is also stored in the $MYVIMRC environment variable. HTH --Benji Fisher
Re: sourcing vimrc files
Hello, Jared <[EMAIL PROTECTED]> wrote: > I'd like to map a hotkey to re-source my vimrc files (system + user). I > originally tried this simple approach: > > nmap :source $VIM\vimrc > \ :source $VIM\_vimrc > \ :source $HOME\_vimrc Your .vimrc knows its own filename. Here is an exerpt from my .vimrc (vimrc_core.vim actually). " --- >% -- " VIM - Editing and updating the vimrc: {{{ " As I often make changes to this file I use these commands " to start editing it and also update it: let vimrc=expand(':p') :VimrcHelp ' ,vu = "update" by reading this file [N] nnoremap ,vu :source =vimrc :VimrcHelp " ,ve = vimrc editing (edit this file) [N] nnoremap ,ve :call OpenVimrc() function! s:OpenVimrc() if (0==strlen(bufname('%'))) && (1==line('$')) && (0==strlen(getline('$'))) " edit in place exe "e ".g:vimrc else exe "sp ".g:vimrc endif endfunction " }}} " --- >% -- HTH, -- Luc Hermitte http://hermitte.free.fr/vim/
Re: sourcing vimrc files
On Sun, May 14, 2006 at 07:49:11PM -0500, Jared wrote: > I'd like to map a hotkey to re-source my vimrc files (system + user). I > originally tried this simple approach: > > nmap :source $VIM\vimrc > \ :source $VIM\_vimrc > \ :source $HOME\_vimrc > > But that failed because source aborts when the file doesn't exist. You could probably use :runtime instead of :source. :help :runtime [...] -- with kind regards Thor Andreassen
Re: sourcing vimrc files
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
Re: sourcing vimrc files
On 5/14/06, Jared <[EMAIL PROTECTED]> wrote: I'd like to map a hotkey to re-source my vimrc files (system + user). I originally tried this simple approach: nmap :source $VIM\vimrc \ :source $VIM\_vimrc \ :source $HOME\_vimrc Try :silent! source But that failed because source aborts when the file doesn't exist. I then modified it to a function call, like so: function! Source_vimrc() if filereadable($VIM.'\vimrc') source $VIM\vimrc endif if filereadable($VIM.'\_vimrc') echo "test1" source $VIM\_vimrc echo "test2" endif if filereadable($HOME.'\_vimrc') source $HOME\_vimrc endif endfunction nmap :call Source_vimrc() 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. You're asking for an inf. recursion having it source itself. Put it in a file in your plugin directory. 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. -- Jared