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?


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.

  • 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 Aug 1 '13 at 14:29
  • 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 Aug 1 '13 at 16:15
  • If you do :echo $TEMP in gVim when run from Windows Explorer and from Cygwin, do you get different paths? – Costa Aug 1 '13 at 17:18
  • 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 Aug 1 '13 at 19:12
  • 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 Aug 1 '13 at 19:20

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.