9

I am attempting to set up Vim on Windows, using Git bash (Cygwin) as the shell environment. I'm getting an error about temporary files which I'm 99% sure is related to the fact that the shell can't be loaded (i.e. the "!" commands don't work).

In my _vimrc file I've tried setting the shell option to be various things, none of which work. My cygwin/git bash command path is C:\Program Files\Git\bin\sh.exe, and the space seems to be causing problems. I've tried the following, without success:

:set shell=C:\Program Files\Git\bin\sh.exe " Error = Unknown options: Files/Git/bin/sh.exe :set shell=C:/Program Files/Git/bin/sh.exe " Error = Unknown options: Files/Git/bin/sh.exe :set shell="C:/Program Files/Git/bin/sh.exe" " Error when running a command: shell option is empty :set shell=C:/Program\ Files/Git/bin/sh.exe " Error when running a command: 'C:/Program' is not recognized as an internal or external command... 

Does anyone know how I can set a shell path that contains a space?

2 Answers 2

11

You almost got it right. You need to escape each surrounding ". Instead of escaping a space, windows requires you to wrap the string in quotes. But vim interprets the quote as a comment. So you need to escape the double quote. You also need a backslash to escape the space.

See an example in :h 'shell' (note the single quotes around shell is necessary to get help on the shell option. Otherwise you get help on the shell ex command.)

:set shell=\"C:\Program\ Files\Git\bin\sh.exe\" 

You can check the value by typing:

:echo &shell 

As a side note though - I recommend not using bash/sh inside the win32 gvim. Many plugins test for has("win32") and construct shell commands for cmd.exe rather than the &shell value. As a result, these plugins will fail if the shell is not cmd.exe. Ideally these plugins would test the &shell value rather than using has("win32"). So even though I call win32 gvim from inside cygwin, I always set the shell back to cmd.exe or better $COMSPEC using this snippet in my vimrc.

if has("win32") || has("win64") || has("win16") "I do other stuff in here... "Then only inside this if block for windows, I test the shell value "On windows, if called from cygwin or msys, the shell needs to be changed to cmd.exe if &shell=~#'bash$' set shell=$COMSPEC " sets shell to correct path for cmd.exe endif endif 

Edit: As @LucHermitte mentions in the comment, plugins that don't work on win32 gvim when shell is set to bash or sh should be fixed. I agree... but in my experience this was not always feasible. So I always set the shell back to $COMSPEC (cmd.exe usually). If you don't have any issues with your plugins, then that's great and ignore my side note.)

Sign up to request clarification or add additional context in comments.

4 Comments

I do use the win32 version of vim with cygwin, and bash as the &shell, and I have no problems so far. Faulty plugins can be fixed, and the authors shall be notified of the corrections they have to operate to have their plugins portable.
@LucHermitte - You're right that faulty plugins should be fixed. I have reported issues in the past, but could not get the author to accept my pull request. If you have no issues with your plugins, then using cygwin bash as the shell inside win32 gvim is nice. I was just saying that my experience was that I had to reset the &shell.
Thanks both for your input. @darcyparker I'm selecting your answer as the correct one, purely because it addresses the specific problem I had with escaping spaces, but it's an interesting point raised about the correctness of using has() for detecting the shell.
When I try this i.e. :set shell=\"C:\Program\ Files\Git\bin\sh.exe\" I get 'C:\Program' is not recognized as an internal or external command, operable program or batch file.', using Windows 7 and vim 7.4. Does it work for others?
1

I'm using the following definitions to run external cygwin related executables from win32-gvim: https://github.com/LucHermitte/vim-system-tools/blob/master/plugin/system_utils.vim#L467 (the other shell options are also quite important)

But, I recommend you to put your cygwin entry-point into your %PATH%. This will be the easiest way.

2 Comments

The example doesn't really show how to set the shell to a path with a space. Adding to %PATH% probably works... then you don't need to define the full path to the shell's executable. But this is more of a workaround.
Indeed, this is a workaround. My usual approach would have been something like let &shell=FixPathName('c:/path with spaces/and/so/on/bash').

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.