Search code examples
windowsvimcygwingit-bash

Setting Cygwin shell path on Vim for Windows


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?


Solution

  • 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.)