Cygwin Tips

  1. Download and run the Cygwin installer from http://cygwin.com
  2. Keep the Cygwin installer (setup.exe) on your desktop. You'll need it occasionally to install additional items.
  3. Do not use the ridiculous Windows shell. Install and use rxvt. It is in the installer under Shells. Then edit c:\cygwin\Cygwin.bat. Comment out
    bash --login -i
    (by adding REM in front of it) and add the following line below:
    rxvt -sl 5000 -fn "Lucida Console-14" -cr firebrick2 -e bash --login -i
  4. c:\somedir\somefile.ext is /cygdrive/c/somedir/somefile.ext in Cygwin
  5. The home directory ~ is c:\cygwin\home\username
  6. Try to avoid file names with spaces. If you must have a space (e.g. in the dreaded Program\ Files), escape it with a \.
  7. Programs written for Windows have Windows-style command-line arguments. For example, the javac compiler on Windows will not know anything about /cygdrive/c. Thus, it is
    javac c:/somedir/SomeClass.java

    (usually, forward slashes work in Windows—it's always worth trying) or

    javac c:\\somedir\\SomeClass.java

    Of course, Cygwin commands (such as rm) don't know anything about Windows-style filenames.

    rm /cygdrive/c/somedir/SomeClass.class

    Always ask yourself whether the command you are using has been written for Cygwin or for Windows.

  8. Windows programs use a semicolon for path separators, but you need to escape it since in bash the semicolon is a statement separator.
    java -cp .\;archive.jar some.package.SomeClass
  9. You cannot use Notepad to edit Unix files (such as .bash_profile). Notepad insists on using DOS style line endings (i.e. \r\n instead of \n). If you have a professional programming editor, use that and tell it to use Unix line endings. Otherwise install EmacsW32. After the installation, select Options -> C-x/C-c/C-v Cut and Paste (CUA) and Options->Save Options.
  10. Put bash customizations into ~/.bash_profile, i.e. c:\cygwin\home\username\.bash_profile. For example, I dislike the two-line default prompt. To change it, I add the following setting:
    PS1='\w\$ '
  11. Cygwin will pick up the Windows environment variables when it starts, but do not bother with the ridiculous Windows way of setting them. Set up the PATH and other environment variables in ~/.bash_profile
    export PATH=$PATH:/cygdrive/c/Program\ Files/java/jdk1.6.0_13/bin

    Here you use a colon, not a semicolon because the PATH is consumed by Cygwin.

  12. When you define an environment variable, ask yourself who consumes it. The PATH is consumed by Cygwin, so it uses /cygdrive/c. For example,
    export PATH=$PATH:/cygdrive/c/ant/bin

    What about the path elements that were picked up at startup? Run echo $PATH to see how they have been converted.

    What about a variable such as ANT_HOME? Is it consumed by a Windows executable or by a bash shell script? As it happens, ant is a shell script, so you use

    export ANT_HOME=/cygdrive/c/ant

    On the other hand, the CLASSPATH is consumed by javac and java, which are Windows executables. They require Windows path names.

    What if a variable is consumed both by a bash script and a Windows program? See the next two entries.

  13. In a script, you sometimes need to convert between Cygwin and Windows path names. Use the cygpath command:
    cygpath -w ~/somedir/somefile.ext

    returns the Windows equivalent of a Unix path, and

    cygpath c:\\somedir\\somefile.ext

    returns the Cygwin equivalent of a Windows path.

    Add the --path option to convert paths (i.e., convert between colons and semicolons).

  14. You sometimes need to clean up a bash script written for sane operating systems. First, detect Cygwin:
    # detect Cygwin
    cygwin=false;
    case "`uname`" in
      CYGWIN*) cygwin=true;
    esac

    Then use cygpath as needed:

    if $cygwin; then
      SOMEVAR=`cygpath --path -w $SOMEVAR`
    fi
  15. rxvt is a fine shell, but if you have to do a lot of cut and paste, the shell inside Emacs is better. Type M-x shell (i.e. Alt+X s h e l l Enter). Use M-p and M-n to go to the previous and next line.
  16. Yikes, that shell is a DOS shell. Select Options -> W32 Options -> w32-shell -> customize -> Value menu -> cygwin -> Save for future sessions.
  17. If you use Prolog, Scala, ML, or some other exotic programming language, chances are that there is an Emacs mode for it. Installing that requires you to edit the .emacs customization file. In Windows 7, it is under c:\Users\username\AppData\Roaming.
  18. Is all of this too much trouble? Remember, it is your choice of operating system. Not ready to ditch Windows? If you have enough RAM and hard disk space, you can always install VirtualBox and run Ubuntu in a VM.