MacVim working directory problem

322 views
Skip to first unread message

digitalronin

unread,
Sep 1, 2010, 8:06:32 AM9/1/10
to vim_mac
Hi All

Does anyone know how to prevent MacVim from changing the working
directory when a file is dragged onto it from the Finder?

I usually work by opening MacVim via Terminal, using "mvim" from the
root directory of the project I'm working in. Then I use a combination
of ":e ...whatever" and dragging files onto the MacVim window from the
Finder, to open whatever files I want to work on.

Since the 7.3 update, the behaviour when dragging files from the
Finder is quite different. It changes the working directory so that,
if the project is laid out like this;

file1
somedir/file2

...then if I drag file2 onto the MacVim window to edit it, then I
can't do ":e file1" anymore, because the working directory is now
"somedir"

I've tried putting a "set noautochdir" in my .vimrc file, but that
doesn't seem to make any difference.

Thanks in advance

David


David Patrick Henderson

unread,
Sep 2, 2010, 12:34:39 AM9/2/10
to vim...@googlegroups.com

On 01 Sep 2010, at 05:06, digitalronin wrote:

[snip lengthy description]

> I've tried putting a "set noautochdir" in my .vimrc file, but that
> doesn't seem to make any difference.
>

Have you tried disabling your .(g)vimrc and .vim directory to see if the behavior persists?

Dave
--
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the universe."
-- Albert Einstein

David Salgado

unread,
Sep 2, 2010, 4:48:44 AM9/2/10
to vim...@googlegroups.com
Hi Dave

> Have you tried disabling your .(g)vimrc and .vim directory to see if the behavior persists?

Good thought, and yes it does. Moving all .vim* stuff out of my home
directory and starting macvim again, I get the same behaviour - the
working directory changes whenever I drag a file into the MacVim
window.

David

björn

unread,
Sep 2, 2010, 6:50:30 AM9/2/10
to vim...@googlegroups.com

Try going through all the steps at

http://code.google.com/p/macvim/wiki/ReportingBugs

I cannot reproduce this behavior. If I open Terminal, mvim file1,
open another directory in Finder and drag the file onto the MacVim
window, then :pwd yields the folder of "file1". Please follow the
steps above to the letter -- there really should be no reason why the
working dir should change on a drag and drop. My initial thought was
that it sounds like you have an autocommand or something that causes
this.

Björn

David Salgado

unread,
Sep 2, 2010, 8:16:06 AM9/2/10
to vim...@googlegroups.com
Hi Björn

The behaviour change is more subtle than I thought.

You're right - if I follow your steps exactly, i.e. cd into a
directory and then type "mvim file1" to open the MacVim window to edit
file1, then the working directory stays the same regardless of what
files I drag in from the finder window.

But, what I usually do is cd into my project directory and just type
"mvim" to open a blank window with the working directory set to the
project root. In that case, when I first do :pwd, the working
directory is the project root.

Then, as soon as I drag in a file from a different directory, the
working directory changes. In fact, the working directory seems to
lock to the directory containing whichever file is dragged in first.
Subsequent file drag and drops do not change :pwd

That's the difference from 7.2 - with MacVim 7.2, the working
directory stays at the project root, regardless of what files I drag
in, even if there was no file being edited in the first window.

Now that I know what's happening, I can work around it by always being
careful to type "mvim somefile" to open MacVim, or just make sure I
drag in a file from the project root before I do anything else.

Many thanks for taking the time to look into this.

David

> --
> You received this message from the "vim_mac" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>

björn

unread,
Sep 2, 2010, 8:54:50 AM9/2/10
to vim...@googlegroups.com
On 2 September 2010 14:16, David Salgado wrote:
>
> The behaviour change is more subtle than I thought.
>
> You're right - if I follow your steps exactly, i.e. cd into a
> directory and then type "mvim file1" to open the MacVim window to edit
> file1, then the working directory stays the same regardless of what
> files I drag in from the finder window.
>
> But, what I usually do is cd into my project directory and just type
> "mvim" to open a blank window with the working directory set to the
> project root. In that case, when I first do :pwd, the working
> directory is the project root.
>
> Then, as soon as I drag in a file from a different directory, the
> working directory changes. In fact, the working directory seems to
> lock to the directory containing whichever file is dragged in first.
> Subsequent file drag and drops do not change :pwd
>
> That's the difference from 7.2 - with MacVim 7.2, the working
> directory stays at the project root, regardless of what files I drag
> in, even if there was no file being edited in the first window.
>
> Now that I know what's happening, I can work around it by always being
> careful to type "mvim somefile" to open MacVim, or just make sure I
> drag in a file from the project root before I do anything else.
>
> Many thanks for taking the time to look into this.

Good, I'm glad you figured it out.

This behavior of changing the pwd is a feature and not a bug. If you
open a new file (via open panel, by dropping, ...) the pwd is changed
if the window is "unused". A windows is classified as "unused" if:

1. only one tab
2. no changes in buffer
3. buffer has no filename associated with it

In most cases I think this is what you'd expect, but I can see that it
is confusing if you use "mvim" specifically to open a new window in a
certain directory. Not sure what to do about this? (I'm open to
suggestions.)

Björn

David Salgado

unread,
Sep 2, 2010, 9:22:47 AM9/2/10
to vim...@googlegroups.com
Hi Björn

> This behavior of changing the pwd is a feature and not a bug.  If you
> open a new file (via open panel, by dropping, ...) the pwd is changed
> if the window is "unused".

I can see the rationale for that.

Personally I often find myself working on a group of files, then
closing every buffer to have a "clean slate" before starting work on
the next bug/feature in the current project.

So, for me, this behaviour is unfortunate because it means I really
need to always keep at least one file open, so that the working
directory is not changed by the next file I open. That's not a serious
problem, of course, and I don't know if other people work in the same
way as I do, and find the behaviour unwelcome. Maybe it's just me!

> In most cases I think this is what you'd expect, but I can see that it
> is confusing if you use "mvim" specifically to open a new window in a
> certain directory.  Not sure what to do about this?  (I'm open to
> suggestions.)

How about testing for "noautochdir" at the point where the code
decides the window is unused? If noautochdir is set, then don't mess
with pwd. That way, users could have a way of disabling this behaviour
if they don't want it, and noautochdir is a fairly intuitive way to
think about turning it off.

Another possibility that I might look at for my own purposes is to use
a shell script to launch mvim, which first sets an environment
variable to `pwd`, and an autocmd in my .vimrc that chdirs to that
directory whenever a file is opened. That should force the working
directory to stay the same.

David

björn

unread,
Sep 2, 2010, 3:09:36 PM9/2/10
to vim...@googlegroups.com
On 2 September 2010 15:22, David Salgado wrote:
>
>> This behavior of changing the pwd is a feature and not a bug.  If you
>> open a new file (via open panel, by dropping, ...) the pwd is changed
>> if the window is "unused".
>
>> In most cases I think this is what you'd expect, but I can see that it
>> is confusing if you use "mvim" specifically to open a new window in a
>> certain directory.  Not sure what to do about this?  (I'm open to
>> suggestions.)
>
> How about testing for "noautochdir" at the point where the code
> decides the window is unused? If noautochdir is set, then don't mess
> with pwd. That way, users could have a way of disabling this behaviour
> if they don't want it, and noautochdir is a fairly intuitive way to
> think about turning it off.

Hmmm...no that wouldn't work since "autochdir" is off by default so
this would effectively disable this feature.

I think I'll leave things as they are; if more people complain about
it I'll start thinking about a better solution. (It's only a guess,
but I think most people do want the pwd to be set to the first file
they open.)

Björn

David Salgado

unread,
Sep 2, 2010, 3:12:23 PM9/2/10
to vim...@googlegroups.com

Fair enough.

If I get my autocmd workaround done, I'll post it here.

David

On Sep 2, 2010 8:10 PM, "björn" <bjorn.w...@gmail.com> wrote:

On 2 September 2010 15:22, David Salgado wrote:
>

>> This behavior of changing the pwd is a feature ...

>> In most cases I think this is what you'd expect, but I can see that it

>> is confusing if you use...

Hmmm...no that wouldn't work since "autochdir" is off by default so
this would effectively disable this feature.

I think I'll leave things as they are; if more people complain about
it I'll start thinking about a better solution.  (It's only a guess,
but I think most people do want the pwd to be set to the first file
they open.)


Björn

--
You received this message from the "vim_mac" maillist.

Do not top-post! Type your reply ...

J Irving

unread,
Sep 2, 2010, 3:20:17 PM9/2/10
to vim...@googlegroups.com
On Thu, Sep 2, 2010 at 15:09, björn <bjorn.w...@gmail.com> wrote:
> I think I'll leave things as they are; if more people complain about
> it I'll start thinking about a better solution.  (It's only a guess,
> but I think most people do want the pwd to be set to the first file
> they open.)

My ideal would be the ability to fix the cwd for a given window by
opening it on a particular directory. Like this:

% mvim ~/ProjectDir
% mvim .
etc.

But I'm not that bothered.

cheers, J

björn

unread,
Sep 2, 2010, 3:28:01 PM9/2/10
to vim...@googlegroups.com
On 2 September 2010 21:20, J Irving wrote:

> On Thu, Sep 2, 2010 at 15:09, björn wrote:
>> I think I'll leave things as they are; if more people complain about
>> it I'll start thinking about a better solution.  (It's only a guess,
>> but I think most people do want the pwd to be set to the first file
>> they open.)
>
> My ideal would be the ability to fix the cwd for a given window by
> opening it on a particular directory.  Like this:
>
> % mvim ~/ProjectDir
> % mvim .
> etc.

This actually would not change the pwd since supplying a directory
name causes Vim to browse the directory with netrw...but I get what
you mean. ;-)

Björn

J Irving

unread,
Sep 2, 2010, 3:31:19 PM9/2/10
to vim...@googlegroups.com

Right, I notice it doesn't, I'm just saying it would in the world
where my pet unicorn barfs rainbows :)

cheers, J

Alec Thomas

unread,
Oct 1, 2010, 9:26:01 PM10/1/10
to vim_mac
On Sep 3, 5:12 am, David Salgado <da...@digitalronin.com> wrote:
> Fair enough.
>
> If I get my autocmd workaround done, I'll post it here.
>
> David
>
> On Sep 2, 2010 8:10 PM, "björn" <bjorn.winck...@gmail.com> wrote:
>
> On 2 September 2010 15:22, David Salgado wrote:
>
>
>
> >> This behavior of changing the pwd is a feature ...
> >> In most cases I think this is what you'd expect, but I can see that it
> >> is confusing if you use...
>
> Hmmm...no that wouldn't work since "autochdir" is off by default so
> this would effectively disable this feature.
>
> I think I'll leave things as they are; if more people complain about
> it I'll start thinking about a better solution.  (It's only a guess,
> but I think most people do want the pwd to be set to the first file
> they open.)

Hi Björn,

There's a long discussion about this issue on this PeepOpen bug
tracker issue: http://github.com/topfunky/PeepOpen-Issues/issues#issue/62

PeepOpen is an external application launched from vim. It offers up a
nice efficient file dialog. Once a file is selected PeepOpen tells
MacVim to open the file, which is where it hits this issue.

For reference, here's how I typically use MacVim in order of
frequency:

1. Open it from the dock, then ":cd <project-root>", then use PeepOpen
to open various files under the project root.
2. Cd into the project directory from the command line then run mvim
(though since this change I now run "mvim .").
3. Open MacVim by clicking or right-clicking on files (typically
source I've downloaded to check out) in Finder.

Here are some suggestions that might help (any of which would solve
this issue for PeepOpen users specifically):

1. Disable the feature as soon as any command is typed into MacVim at
all.
2. Disable the feature if the user launches MacVim via mvim.
3. Add a MacVim-specific option to disable the feature.

Alec

PS. Thanks for your work on MacVim, it is fantastic :)

björn

unread,
Oct 2, 2010, 1:01:59 PM10/2/10
to vim...@googlegroups.com

I see that something has to change...but I'm still not sure how. The
current behavior is confusing to say the least -- when I change it I
want it to be completely obvious what is going on. I'd rather avoid
introducing another option for this.

Under what circumstances _should_ the pwd be set?

Well, if new files open in a new window then I guess it should always
be set when opening a file (?), so the question only really applies to
when you've set MacVim to open files in the "current" window. Maybe
with this option set the pwd should never be touched? At least this
is consistent (I think your suggestions 1 & 2 may lead to some
confusion however).

Does anybody have comments on this (please make yourselves heard)?

Let me rephrase this slightly: is there anybody out there who has got
MacVim set to open files in the "current" window and who wants the pwd
to be set when they open a new file? (If so, when?)

> PS. Thanks for your work on MacVim, it is fantastic :)

I'm glad you like it!


Björn

David Salgado

unread,
Oct 3, 2010, 11:04:44 AM10/3/10
to vim...@googlegroups.com
> Does anybody have comments on this (please make yourselves heard)?

My preference would be to never mess with the pwd if the window is already open.

If opening the file opens a new window, then I think it makes sense to
set the pwd to the directory containing the file.

David

björn

unread,
Oct 3, 2010, 11:57:29 AM10/3/10
to vim...@googlegroups.com

I agree. This is the most intuitive behavior. I've pushed a patch already:

http://github.com/b4winckler/macvim/commit/c96d4dcc8e5b65bf989efa7e1b86f262974f935c

Björn

David

unread,
Oct 3, 2010, 10:17:38 AM10/3/10
to vim_mac
Bjorn,

Thanks for making this wonderful product. It's better than any other
IDE I have used in the past.

I don't think I understand the full scope of this problem. I just know
the symptoms. I was wondering if this behavior could be turned into a
variable of some kind? Then we could allow different types of behavior
in terms of when to set the pwd.

Thanks.

David

On Oct 2, 1:01 pm, björn <bjorn.winck...@gmail.com> wrote:

Kyle Lippincott

unread,
Oct 3, 2010, 8:38:24 PM10/3/10
to vim...@googlegroups.com
I imagine those people are already the type of people who change directories on BufEnter (I know at least one).

My $.04 (stupid currency devaluing): Personally I never switch directories, but that's because I almost always run vim from the command line if I'm in a situation where I'd want to (specifically: I do lots of coding on linux.  I use the command line version of vim a lot.  In MacVim, I rarely care what directory I'm in since it's rare that I have a tree of files that I need to navigate.. if the file has siblings, I'll just automatically use %:h/ to get to them).

I think the proposed solution is probably the best one.  It doesn't bother users like me (who don't care what pwd is), and it shouldn't bother the BufEnter directory changers, since BufEnter should still fire (after you've modified the pwd), and you're probably setting it to what they want anyway.
 

> PS. Thanks for your work on MacVim, it is fantastic :)

I'm glad you like it!


Björn
--
You received this message from the "vim_mac" maillist.

Brian McKee

unread,
Oct 4, 2010, 8:50:12 AM10/4/10
to vim...@googlegroups.com


On Sun, Oct 3, 2010 at 8:38 PM, Kyle Lippincott <spec...@gmail.com> wrote:
if the file has siblings, I'll just automatically use %:h/ to get to them

Hmmm?  I didn't follow that part of your comment...

--
Hey, it's your computer.... isn't it?

björn

unread,
Oct 4, 2010, 9:55:09 AM10/4/10
to vim...@googlegroups.com
On 3 October 2010 16:17, David <dda...@gmail.com> wrote:
>
> I don't think I understand the full scope of this problem. I just know
> the symptoms. I was wondering if this behavior could be turned into a
> variable of some kind? Then we could allow different types of behavior
> in terms of when to set the pwd.

I'd like to try and avoid introducing more options. What different
types of behavior do you want?

Currently (after the latest patch) it goes like this when you open one
or more files (not using the "mvim" script):

1. If this causes a new window to appear on screen then the working
directory is set to the first file that is being opened (they are
sorted alphabetically because Finder does not "remember" the order in
which you select them).

2. Otherwise (i.e. you have gone into the prefs and set MacVim to open
new files in the "current" window, or you're dragging to a window) the
current directory is not touched.

The old behavior was broken because the pwd would change even if you
had manually gone in and changed it (e.g. via a ":cd .." command).

If you open files using the "mvim" script the working directory is the
same as Terminal's working directory.

If you open a new empty window (e.g. via Cmd+N, or when starting
MacVim) the working directory will be set to your home directory.


Björn

Kyle Lippincott

unread,
Oct 4, 2010, 4:01:58 PM10/4/10
to vim...@googlegroups.com
Ah, sorry.  Normally when coding (the only time I care about the current directory of vim), I'll be working with a tree of files, something like:

library1/src/foo.cpp
library1/src/foo.h
library2/client/rpc_interface.py

I want to be able to type:
:e library1/src/foo.cpp     or
:e library2/client/rpc_interface.py

However, if I'm already in foo.cpp, I automatically type:
:e %:h/foo.h 

So by siblings I meant "files in the same directory as the file I'm currently editing".  I actually do this for subdirectories as well (say there's a 'library1/src/testing' directory, then it's %:h/testing/blah.cpp).  To explain what this is doing, I believe the help topic in vim for the description of these is 'filename-modifiers', and % is the filename of the current buffer.

--

Brian McKee

unread,
Oct 5, 2010, 6:46:00 PM10/5/10
to vim...@googlegroups.com


On Mon, Oct 4, 2010 at 4:01 PM, Kyle Lippincott <spec...@gmail.com> wrote:

:e %:h/foo.h 
 
 To explain what this is doing, I believe the help topic in vim for the description of these is 'filename-modifiers', and % is the filename of the current buffer.


Thank you.  That's something I wasn't familiar with.

Brian
Reply all
Reply to author
Forward
0 new messages