Test if vim just opened with no file

21 views
Skip to first unread message

bgold12

unread,
Oct 8, 2008, 3:05:21 AM10/8/08
to vim_use
Hey, is there way to test if vim was just started with no file?
Actually, I should start from the beginning:

I usually like to start vim by clicking on a shortcut on my Windows
taskbar, which means it'll start with an empty [No Name] buffer. In
this case, I would like my vimrc file to automatically set the
directory to a preferred start-up directory. But obviously when I
start vim with a file specified, either from the command line or from
double-clicking on a vim-associated file, I want the current directory
to be the directory the opened file is in, so I don't want my vimrc to
set the directory in that case. Any suggestions?

Thanks.

Tony Mechelynck

unread,
Oct 8, 2008, 3:42:28 AM10/8/08
to vim...@googlegroups.com

Method 1. Change the properties of your desktop shortcut so that it loads

gvim --cmd "cd C:\my\preferred\startup\dir"

Method 2. There's nothing obvious in wanting Vim to always set the
current directory to the directory of the current file: 'autochdir'
(q.v.) defaults to off, and as a matter of fact I don't set it on.
However if you do, I think it doesn't matter which directory you :cd to
in your vimrc, whenever you enter a file (including the first file on
the command-line) Vim should change to the file's directory. So in that
case I think you could just write the following into your vimrc:

set autochdir
cd C:\my\preferred\startup\dir


Best regards,
Tony.
--
People need good lies. There are too many bad ones.
-- Bokonon, "Cat's Cradle" by Kurt Vonnegut, Jr.

Henrik Öhman

unread,
Oct 8, 2008, 6:19:19 AM10/8/08
to vim...@googlegroups.com

You need to do exactly one thing to get this behaviour: In your shortcut, change the value of 'Start in:' to 'your\prefered\startup\directory'. When starting vim from the command line, it should use the current directory, and likewise when you right-click and 'Edit' or 'Edit with Vim' in Explorer. This is how my gvim works; there's no need to mess around with 'autochdir' or '--cmd'.

Henrik.

bgold12

unread,
Oct 8, 2008, 9:47:13 AM10/8/08
to vim_use
Hey, thanks for your suggestions.

I'd rather not change the properties of the shortcut, because that
would mean I'd have to make sure each vim shortcut has the right
properties, so using gvim --cmd or changing the 'Start in' property is
not what I want. I also tried using autochdir, but that didn't work;
vim still executed the cd C:\... line in my vimrc and I always started
in that same directory.

But I've found a pretty good solution; I know that when vim opens
without a file, it defaults to the installation directory, which in my
case is "C:\Program Files\Vim\vim72" at the moment. So in my vimrc I
just added a test:

if (getcwd() == "C:\\Program Files\\Vim\\vim72")
cd C:\my\preferred\directory
endif

So now whenever I open vim with a file, it stays in the file's
directory, but when I open vim without a file, it starts in whatever
directory I want.

There are 2 problems with this solution: (1) If I ever change the
installation directory (say if I install a new version of vim, which I
obviously will at some point), then I'd have to change the if
statement to match the new installation directory. Actually, if
someone knows a way of testing the installation/default start-up
directory from vim, then that would solve this problem. (2) If I ever
for some reason want to edit a file in the installation directory, vim
would cd to my preferred directory instead of staying in the file's
directory. But I don't see why I would want to do that.

fritzophrenic

unread,
Oct 8, 2008, 10:24:18 AM10/8/08
to vim_use
For problem 1, you want the $VIMRUNTIME variable.

For problem 2...if you set autochdir, you can just do :e to re-edit
the file and it will change back where it needs to go.

Autochdir seems to be a little flakey...I need to :e every now and
then to get it to work, but I haven't been able to determine a
reliable way to make it fail.

Dennis Benzinger

unread,
Oct 8, 2008, 10:36:57 AM10/8/08
to vim...@googlegroups.com

You probably can write a function that hooks into the VimEnter event
(:help autocommand-events) and then checks if there is only one buffer
(:help bufnr()) and if this buffer is a unnamed buffer (:help bufname()).


HTH,
Dennis Benzinger

Andy Wokula

unread,
Oct 8, 2008, 10:58:46 AM10/8/08
to vim...@googlegroups.com
bgold12 schrieb:

You can get the number of file arguments:
:h argc()

--
Andy

bgold12

unread,
Oct 8, 2008, 11:15:35 PM10/8/08
to vim_use
Wow, all these methods worked. I'm actually going to go with the
argc() test, because it's the simplest:

if (argc() == 0)
cd C:\...
endif

On Oct 8, 10:58 am, Andy Wokula <anw...@yahoo.de> wrote:
> bgold12schrieb:

Ben Schmidt

unread,
Oct 8, 2008, 11:32:58 PM10/8/08
to vim...@googlegroups.com
> Method 2. There's nothing obvious in wanting Vim to always set the
> current directory to the directory of the current file: 'autochdir'
> (q.v.) defaults to off, and as a matter of fact I don't set it on.
> However if you do, I think it doesn't matter which directory you :cd to
> in your vimrc, whenever you enter a file (including the first file on
> the command-line) Vim should change to the file's directory. So in that
> case I think you could just write the following into your vimrc:
>
> set autochdir
> cd C:\my\preferred\startup\dir

You don't need autochdir, and may not want it (e.g. because you don't
want it changing after opening the first file, but only when opening the
first file). Also, the Windows version automatically does that (changes
for the first file, but doesn't set autochdir).

:help vim-default-editor
:help send-to-menu

Ben.


Tony Mechelynck

unread,
Oct 9, 2008, 12:20:26 AM10/9/08
to vim...@googlegroups.com

See the other replies. The simplest seems to be

if !argc()


cd C:\my\preferred\startup\dir

endif

Best regards,
Tony.
--
"God gives burdens; also shoulders"

Jimmy Carter cited this Jewish saying in his concession speech at the
end of the 1980 election. At least he said it was a Jewish saying; I
can't find it anywhere. I'm sure he's telling the truth though; why
would he lie about a thing like that?
-- Arthur Naiman, "Every Goy's Guide to Yiddish"

bgold12

unread,
Dec 5, 2008, 3:02:58 AM12/5/08
to vim_use
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).

On Oct 8, 11:20 pm, Tony Mechelynck <antoine.mechely...@gmail.com>
wrote:

Tony Mechelynck

unread,
Dec 5, 2008, 7:42:43 AM12/5/08
to vim...@googlegroups.com
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!

Reply all
Reply to author
Forward
0 new messages