How to invoke Windows' gvim in a Windows environment from within Cygwin?

3

1

When I give a gvim command in Cygwin, I want it to invoke the gvim I've installed in my Windows.

I tried writing a function in my .bashrc called gvim that invokes the gvim.exe in my Program Files folder, using cygpath and all that, but the problem is, that apparently invokes the gvim with Cygwin's environment; :echo $HOME prints C:\cygwin\home\Sundar instead of C:\Users\Sundar, and :diffsplit reports failures in creating temporary files which I assume are related to the same. So, how can I invoke gvim from Cygwin but make it as if it was invoked through, say, the Start menu?

I tried changing the command to pass the gvim path to cmd.exe /c, but that too somehow sets up the same environment. What do I need to do to make this work?

sundar - Reinstate Monica

Posted 2013-07-31T20:02:20.440

Reputation: 1 289

Answers

3

gVim.exe looks for a HOME variable when it is run. In your case, the Cygwin environment has its own HOME var set, so it is using that.

You could change the HOME value in your environment, but that could affect other applications. Then again, you might want that. In case you don't want that and assuming your Cygwin shell is using the Bash shell, since you've already suggested in your question that you're looking to create an alias or function, I would recommend setting the var only for that command instance, which Bash allows you to do by just setting it right before the command separated by a space...

VAR=abc command

The above runs command as if VAR was set to abc, regardless of what it might actually be. Thus...

alias gvim='HOME=/cygdrive/c/Users/Sundar /path/to/gvim.exe'

... or...

gvim() { HOME=/cygdrive/c/Users/Sundar /path/to/gvim.exe $1; }

...would allow you to run the gVim executable with the HOME var set to your preferred path, without otherwise disturbing your Cygwin environment.

Costa

Posted 2013-07-31T20:02:20.440

Reputation: 491

As an aside, it might be worth your while to use cygstart to run the executable, rather than calling it directly... HOME=/cygdrive/c/Users/Sundar cygstart /path/to/gvim.exe – Costa – 2013-08-01T14:29:26.133

I didn't know about cygstart, that's hugely useful, thanks. I didn't want the gVim process dependent on the presence of the Cygwin, that's one reason I mentioned "as if it was invoked through the Start menu". Regarding the environment, I'm trying to find out which all I need to set in addition to $HOME. I tried setting TEMP and TMP but am still getting the "Cannot read or write temp files" and "Cannot create diffs" errors. – sundar - Reinstate Monica – 2013-08-01T16:15:49.783

If you do :echo $TEMP in gVim when run from Windows Explorer and from Cygwin, do you get different paths? – Costa – 2013-08-01T17:18:46.967

Strangely, no, in both cases $TEMP and $TMP are both "C:\Users\Sundar\AppData\Local\Temp". But diffsplit works when run from Explorer but not when run from cygwin using "env TMP=C:/Temp/ TEMP=C:/Temp/ HOME=C:/Users/Sundar/ /usr/bin/cygstart 'C:\Program Files (x86)\Vim\vim73\gvim.exe'" (also looks like these values of TMP and TEMP are getting overwritten by Vim automatically!) – sundar - Reinstate Monica – 2013-08-01T19:12:49.840

Hmm, perhaps it's a permissions difference then. Maybe your Cygwin instance is running as a different user, or maybe the shortcut or shell you're running from Explorer are being run as Administrator. That's the best I can theorize without being able to get my hands on your workstation. Hope you figure it out! – Costa – 2013-08-01T19:20:06.293

Phew... After running the diffsplit commands dozens of times (since the command window vanishes so quickly), I was able to see that running from windows uses cmd.exe /c whereas running from cygwin uses bash -e. Turns out it's the value of the set shell variable that was the culprit. I now have to track down how that's getting set and what else is screwed up like this, but at least this mystery is solved. :) – sundar - Reinstate Monica – 2013-08-01T20:16:36.373

0

I found an easier solution that tracking down all the environment variables required, though it has one limitation. The solution is to give explorer.exe filename.ext at the Cygwin command line, where ext is an extension that is already associated with gVim under Windows. This opens gVim in a completely Windows context as far as I can see, $HOME is set to C:\Users\Sundar automatically, :diffsplit works with no error, etc. The one limitation should be obvious now - you need to have an existing file with a gVim-associated extension handily available, or this won't work.

Update: While googling regarding this, I came across www.commandlinefu.com/commands/view/2729 (down right now, text-only cache at http://webcache.googleusercontent.com/search?strip=1&q=cache:http%3A%2F%2Fwww.commandlinefu.com%2Fcommands%2Fview%2F2729 ), and found that explorer.exe 'C:\Program Files (x86)\Vim\vim73\gvim.exe' opens gvim reliably, without having to rely on file associations. I'm now looking for a way to pass arguments to the gvim opened this way.

sundar - Reinstate Monica

Posted 2013-07-31T20:02:20.440

Reputation: 1 289