Wednesday, February 2, 2011

Re: Sourcing an arbitrary vimscript right after all vimrc files have been executed?Z

Reply to message «Re: Sourcing an arbitrary vimscript right after all vimrc
files have been executed?Z»,
sent 21:09:50 02 February 2011, Wednesday
by Benjamin R. Haskell:

> " careful of 'special' chars in filename (including spaces)
> vim --cmd 'let g:extrasource='./path/to/extra.vim
It won't work: you forgot to quote /path/to/extra.vim

I personally would have written that either as
vim --cmd 'leg g:extrasource="/path/to/extra.vim"'
or, if I know that I will use this too often:

function vim-so()
{
emulate -LR zsh
vim --cmd "let g:extrasource=${(qqq)1}" ${@[2,-1]}
}
. This is safe because (qqq) will enclose argument into double quotes with
proper C escaping which is accepted by vim.

Original message:
> On Wed, 2 Feb 2011, Dun Peal wrote:
> > Hi.
> >
> > I read about the -S command line flag, as well as the --cmd flag.
> >
> > Problem is, I need to source an arbitrary vimscript right after all
> > vimrc files have been read, but before the first file has been read.
> > So --cmd is too early for me, while -S is too late.
> >
> > What I need is a flag --foo, such that:
> > vim --foo bar.vim
> >
> > Would act as if the following line has been appended to ~/.vimrc:
> > source bar.vim
> >
> > Does such a thing exists?
>
> Here's what I do to set up an 'alpine' (mail client) mode (essentially
> adds an extra conditionally-sourced file at the end of .vimrc):
>
> At end of .vimrc:
>
> if exists("g:alpine")
> " do alpine-specific stuff
> endif
>
> Then, invoke Vim as: vim --cmd 'let g:alpine=1'
>
> Modified for sourcing an arbitrary file:
>
> At end of .vimrc:
>
> if exists("g:extrasource")
> exe "source ".g:extrasource
> endif
>
> Invoke as:
> " careful of 'special' chars in filename (including spaces)
> vim --cmd 'let g:extrasource='./path/to/extra.vim

No comments: