On 05/12/08 09:02, bgold12 wrote:
> I know this thread is long dead, but I've made a minor improvement to
> my choice of solution that I feel is worth mentioning.
>
> The solution I chose previously was:
>
> if (argc() == 0)
>      cd C:\my\preferred\startup\dir
> endif
>
> This works perfectly all the time, when STARTING vim; the only problem
> is if you ever want to re-source your vimrc file while editing (for
> example, to reset your settings to your preferred settings). If you
> had started vim with no arguments (meaning, for example, from a
> shortcut, like I usually do), then argc() will remain 0 for the
> duration of the instance of vim (as far as I know and have tested),
> and so the directory will ALWAYS be reset to your "preferred"
> directory whenever you re-source your vimrc. This is undesirable,
> because usually, once you've gotten into editing files in an instance
> of vim, you don't want to be cd'd to any default directory.
>
> My solution is as follows:
>
> if (bufnr("$") == 1&&  bufname("$") == ""&&  getcwd() == $VIMRUNTIME)
>      cd C:\my\preferred\startup\dir
> endif
>
> With this solution, your directory will only be changed to your
> preferred directory when (A) there is only one buffer, (B) that buffer
> has yet to be named, and (C) the current directory is still the vim
> default installation directory. Note that the first time vim is
> opened, if it was given no arguments (i.e. opened from a shortcut),
> then it will automatically be cd'd out of the vim default installation
> directory and into your preferred directory, disqualifying (C) for the
> remainder of the instance of vim, unless you for some reason cd
> yourself back into that directory, but then in that case presumably
> you would start editing a file with a name, which would disqualify
> (B).

When starting [g]vim from a prompt (a cmd promt in the Dos Box or a 
shell prompt in a Unix-like shell including Cygwin bash), the current 
directory can be anything (I expect most often $HOME or the filesystem 
root rather than $VIMRUNTIME) so in that case your getcwd() test will 
disable the cd to your/preferred/startup/dir/. OTOH you may want to set 
your preferred startup dir as the current directory in your preferred 
shell and your desktop icon (maybe define a desktop icon with a startup 
directory and another one without, to disable the cd command when 
dropping a file on the latter icon).

Another possibility to avoid running a certain part of the vimrc when 
re-sourcing it a 2nd (3rd, etc.) time in a vim session is the way it is 
done in other plugins: in this case, put it near the end:

        if exists(vimrc_started)
                finish
        endif
        let vimrc_started = 1
        " add any first-time-only commands here,
        " until the end of the vimrc.
        cd /my/preferred/startup/directory

Still another possibility would be to set any first-time-only commands 
in an autocommand for the VimEnter event, which is triggered exactly 
once in each session, at the very end of startup; but of course only if 
the +autocmd feature has been compiled-in. (The above ":if" method won't 
work in versions without +eval, where every ":if" is regarded as a 
nestable comment, and ":let" causes an error.)


Best regards,
Tony.
-- 
Back up my hard drive?  I can't find the reverse switch!

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to