Vim Tips Wiki

Use cygwin shell

Redirected from VimTip814

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 814 Printable Monobook Previous Next

created November 3, 2004 · complexity intermediate · author Jamie Sanderson · version 5.7

By default, Vim on Windows uses the "Command Prompt" as its shell. If you have Cygwin installed ( you may want to use one of its shells instead, such as bash. This also makes all of the programs installed under Cygwin available for text processing.

The following settings may be included in a startup script to use bash as your shell. I have these commands in my gvimrc file in the installation directory.

set shell=C:/cygwin/bin/bash
set shellcmdflag=--login\ -c
set shellxquote=\"

I had problems with parts of the /etc/profile not being executed, but I didn't want to add -i (interactive) to the shellcmdflag because this caused the shell to always open in my home directory. I prefer that it opens in the directory containing the file being edited. However, without that part of /etc/profile running, the path wasn't set up properly. To get around this, I added the following line to /etc/profile:


I added this to my .bashrc:

if [ -z "$RANPROFILE" ]; then

Newer versions of the /etc/profile installed with Cygwin may behave differently.


I found that

set shellcmdlfag=--login -c

Didn't work too well for me, as every time I ran a shell command from gvim it would run it in my cygwin home directory rather than the directory associated with the current gvim buffer [That is what --login is supposed to do, if you don't want that, leave it off.].

So I replaced it with the following, I also had to add some cygwin bin directories to the path in my .bashrc file

  let &shell='C:/cygwin/bin/bash.exe' . ' --rcfile c:/cygwin/home/' . $USERNAME . '/.bashrc ' . '-i '
  set shellcmdflag=-c
  set shellxquote=\"

vimdiff (in gvim) doesn't seem to work with cygwin as default shell

Go to cygwin setup, and install editors->vim,

It understands /cygdrive/c/ syntax, the regular Vim does not.

I use Cygwin along with the Windows version gVim. It's true that this version of gVim doesn't understand the "cygdrive" syntax, so you have to use the cygpath command to convert your paths before it will work.

Here is a bash script I wrote that translates the paths and launches gvim as a background process. I added an alias to my .bash_profile so I generally forget it's there. There are probably better ways to do this, but it works for me:

while [ -n "$1" ]; do
 if [ -n "`echo $1 | grep '^-'`" ]; then
  ARG=`cygpath --absolute --windows "$1"`
 # Escape spaces in arg
 ARG=`echo $ARG | sed -e 's/\\\\/\\\\\\\\/g' -e 's/ /\\\\ /g'`
bash -c "$CMD" &

Added by Siddhant [Start]:

Some optimizations/improvments could be done in above script as follows (I am using cygwin 0.17-11):

1. enclose the arg in "" instead of using sed to replace \ to \\

2. use case..esac instead of if..else (grep '^-') to check first character of the arg

3. using eval instead of "bash -c"

4. remove --absolute from cygwin, otherwise current path gets added in non-path arg and error comes, e.g.

keytool.exe -list -keystore myKeys -storepass myPass


keytool.exe -list -keystore "D:\Siddhant\myKeys" -storepass "D:\Siddhant\myPass"

5. add "bg" option to run process in background

6. add usage option :-)


if [[ $1 = "bg" ]];then

if [[ $# -eq 0 ]]; then
   echo "usage: $0 [bg] <progarm> [args]"
   exit 0


while [ -n "$1" ]; do
   case "$1" in
     [+-]*) ARG=$1;;
     *) ARG=\"`cygpath -w "$1"`\";;
   CMD="$CMD $ARG";

#echo $CMD $BG
eval $CMD $BG

Save the above in an script say and make aliases as follows

alias gvim=' bg gvim'

alias java=' java'

I gave a try to on also but did not like much :-(

Added by Siddhant [End]:

These mappings allow for opening explorer, windows command prompt, and cygwin bash in the directory of the current file. They should all work with directories that have evil spaces in them...

"******* current file directory commands WINDOWS **********************
"%:p:h:8 gets the current file's directory and :8 is what puts it
"into dos short form

"open explorer in the current file's directory
map ,e :!start explorer %:p:h:8<CR>

"open windows command prompt in the current file's directory
map ,c :!start cmd /k cd %:p:h:8<CR>

"open cygwin bash in the current file's directory
map ,b :!start bash --login -i -c 'cd `cygpath "%:p:h:8"`;bash'<CR>

"******* end current file directory commands WINDOWS ******************

Also on Fandom

Random Wiki