Cygwin Tips

Installation

  1. Download and run the Cygwin setup.exe program from http://cygwin.com. Choose all default options.
  2. Start the Cygwin shell from the Windows start menu.
  3. Find out where your browser downloads files. It's usually something like c:\Users\yourname\Downloads.

    Now issue the command

    ln -s /cygdrive/c/Users/yourname/Downloads downloads

    In the path name for the download directory, replace c: with /cygdrive/c and all backslashes (\) with forward slashes (/).

    As you type each path segment, type the first few characters, then the Tab key.  That triggers  autocompletion, which makes it easier to type the correct path name.

    Type

    ls downloads

    Do you see the files in your browser's download directory, such as setup.exe? If not, type rm downloads and try the ln -s command again.

    Close the window. You now have a basic Cygwin setup.

apt-cyg

  1. Re-run the Cygwin setup.exe program from http://cygwin.com. Choose all default options until you get to the screen with title “Select Packages”.

    In the search box, type wget. As you type the letters, the box below shows fewer items. By the time that you type the letter t, only a couple are left. Click to expand any + icons until you see a package with name wget. Click once onto the word Skip.

    A version number will appear.

    Now click Next and wait until the installation wizard finishes.

  2. Go to the Start Menu -> All Programs -> Cygwin. Right-click on the Cygwin Bash Shell icon and select “Run as administrator”. Into the command shell, type
    wget http://apt-cyg.googlecode.com/svn/trunk/apt-cyg
    chmod +x apt-cyg
    mv apt-cyg /usr/local/bin 
    apt-cyg install unzip

    Note: If your user account has administrator privileges already, you can ignore all the “as administrator” instructions, and just run everything as yourself.

Emacs

  1. Point your browser to http://ftp.gnu.org/pub/gnu/emacs/windows/. Pick emacs-xy.z-bin-i386.zip, with the highest available xy.z.
  2. Open a new Cygwin shell as a regular user. Type
    unzip downloads/emacs-xy.z-bin-i386.zip
    chmod +x emacs-*/bin/*.exe

    Here xy.z is the Emacs version number of your download.

  3. Start Emacs with the command
    emacs-*/bin/emacs &

    In the Emacs menu, select Options -> C-x/C-c/C-v Cut and Paste (CUA) and Options->Save Options. That changes the cut/copy/paste commands to the familiar Ctrl-X/Ctrl-C/Ctrl-V.

    Now select File -> Open File... from the menu and select the file named .emacs. Note the period (.) before the filename.

    If there is no such file, hit the following key combination instead:

    [Control-X][Control-F]~/.emacs[Enter]

    That is, hold down Control and X, let go of X but not of Control, type F, let go of both, then type the eight characters ~/.emacs and then hit the Enter key. (If you already have a ~/ after hitting [Control-X][Control-F], you can just enter the six characters .emacs and then Enter.)

    At the end, add the following line:

    (prefer-coding-system 'utf-8-unix)

    Just paste it in with Ctrl+V.

    Select Emacs-Lisp -> Evaluate Buffer from the menu. Then Select File -> Save.

    Now select File -> Visit New File... from the menu and type in .bash_profile into the file dialog. (If the file exists, just select it.) Note the period (.) before the filename.

    Add the following lines:

    export PS1='\w\$ '
    export EMACS_HOME=~/emacs-xy.z
    export PATH=$PATH:$EMACS_HOME/bin

    Again, xy.z is the Emacs version number. Paste in the commands and fix up the xy.z

    Save the file and close the shell. From now on, you can simply start Emacs with

    emacs filename &

Java

  1. Download and install the JDK in the usual way.
  2. Start the Cygwin shell from the Windows start menu.
  3. Open a Cygwin shell as a regular user. Run
    emacs .bash_profile &

    Add the following environment variable at the bottom of the file, before the export PATH statement:

    export JAVA_HOME=/cygdrive/c/Program\ Files/Java/jdkversion/

    where you should replace version with the version of your Java installation. Make sure to put a \ before each space in the path name. Then add $JAVA_HOME/bin to the path, like this:

    export PATH=$PATH:$EMACS_HOME/bin:$JAVA_HOME/bin

    Save the file.

  4. Run
    source .bash_profile
    javac -version

    That's javac, not java.

    If you get the correct version number, you are done.

Scala

  1. First install Java.
  2. Download the Linux/Unix/Mac OS X/Cygwin version of Scala.
  3. Start a Cygwin shell and run
    tar xvfz downloads/scala-version.tgz
  4. Run
    emacs .bash_profile

    Add the following environment variable at the end of the file, before the export PATH statement:

    export SCALA_HOME=~/scala-version

    where version is the Scala version.

    Add $SCALA_HOME/bin to the PATH.

    export PATH=$PATH:$EMACS_HOME/bin:$JAVA_HOME/bin:$SCALA_HOME/bin

    Save the file.

  5. To verify the installation, start a Cygwin shell as a regular user and type
    scala

    You should get a shell prompt. Type

    6*7

    and hit Enter. Type Ctrl+D after you see the answer.

  6. Run the following:
    cd scala-version/misc/scala-tool-support/emacs
    apt-cyg install make
    make

    In Emacs, load ~/.emacs and add the following at the end:

    (add-to-list 'load-path "/home/yourname/scala-version/misc/scala-tool-support/emacs")
    (require 'scala-mode-auto)
        

Mercurial

  1. Start a Cygwin shell as administrator and run
    apt-cyg install openssh mercurial

    Close the shell.

  2. As a regular user, run
    emacs ~/.hgrc &

    Put these lines into the file and save it.

    [ui]
    username=Your name <username@mailserver.com>
    merge=internal:merge

    Of course, use your actual name and email address.

  3. Make a public SSH key. First check if you already have one.
    ls ~/.ssh

    If you get a directory listing showing (among others) a file id_rsa.pub, then skip this step. Otherwise, run

    ssh-keygen -t rsa -C "Your name <username@mailserver.com>"

    (of course, replacing Your name with your name and username@mailserver.com with your email address). Accept the default for the file name. When prompted for a passphrase, you can enter a passphrase, which is more secure, but you have to enter it whenever you use the key. Or just hit Enter to skip this.

    Do not change the name of the key pair that is being generated. It should be id_rsa and id_rsa.pub.

  4. When asked for your public key, use the command
    cat ~/.ssh/id_rsa.pub

    Copy the text (starting with ssh-rsa and ending with Your name <username@mailserver.com> and submit it to your instructor or lab administrator. This key is needed to allow you access to the Mercurial server.

    Do not give out the private key (id_rsa).

Ruby

  1. Start a Cygwin shell as administrator and run
    apt-cyg install ruby crypt
  2. To verify the installation, start a Cygwin shell as a regular user and type
    irb

    You should get a shell prompt. Type

    6*7

    and hit Enter. Type Ctrl+D after you see the answer.

Rails

  1. Start a Cygwin shell as administrator and run
    apt-cyg install make gcc4 sqlite3 libsqlite3_0 libsqlite3-devel
  2. Look at the RubyGems site and check what the latest version is. Here I assume it's 1.8.10.

    Still as administrator, run

    wget http://production.cf.rubygems.org/rubygems/rubygems-1.8.10.tgz
    tar xvfz rubygems-1.8.10.tgz
    cd rubygems-1.8.10
    ruby setup.rb
    cd ..
    rm rubygems-1.8.10.tgz
    rm -rf rubygems-1.8.10
  3. Still as administrator, run
    gem install rails sqlite3-ruby
  4. Now open a Cygwin shell as a regular user. Run
    rails new firstapp
    cd firstapp
    rails server

    Point your browser to http://localhost:3000. If you get the Rails greeting screen, the installation was successful.

Finding Your Way Around Cygwin and Windows

  1. c:\somedir\somefile.ext in Windows is /cygdrive/c/somedir/somefile.ext in Cygwin
  2. The Cygwin home directory ~ is c:\cygwin\home\username in Windows. Therefore, somedir/somefile.ext in Cygwin is c:\cygwin\home\username\somedir\somefile.ext.

    Practice the correspondence by running ls somedir or ls /cygdrive/c/somedir in Cygwin and looking at the same directory in Windows Explorer.

  3. Try to avoid file names with spaces. If you must have a space (e.g. in the dreaded Program\ Files), escape it with a \.
  4. 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.

  5. 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
  6. If you use Notepad to edit Unix files (such as .bash_profile), it adds DOS style line endings (i.e. \r\n instead of \n). That will confuse bash and other Cygwin programs. If it happened to you, you can fix the file by running
    dos2unix filename

    Inside Emacs, you can turn DOS style line endings into Unix style ones by running

    C-x RET f unix RET

    That is, Ctrl-X followed by the Enter key, the letter f, the four letters u n i x and another Enter. Once a file has Unix style line endings, Emacs will keep it so.

Finding Your Way Around Emacs

  1. In the Emacs documentation, C- denotes the Ctrl key and M- denotes the Alt key (called Meta way back when...).
  2. To terminate just about any command, hit C-g (i.e. Ctrl-G).
  3. Try incremental search. C-s, then the string you want to search for. As you type, searching happens. To get to the next match, hit C-s again. C-g cancels.
  4. Sometimes, the screen gets split in two, with a message showing in one half. C-x 1 gets rid of the split screen.
  5. Use C-x k to close a “buffer” that you no longer need.
  6. If you need multiple windows (e.g. for copy/paste or comparing), use File -> Split Window (C-x 5 2).
  7. Use the menus and note the keyboard shortcuts next to them. Ever so often, make it a point to learn a keyboard shortcuts (e.g. C-x C-s to save a file). Even though they seem to make little sense, the shortcuts become second nature and save you time.
  8. One of the best features of Emacs is shell mode. This is very handy when you work with an interpreter for some programming language (Scala, Ruby, Scheme, whatever). Keep two windows. Run the interpreter in one, and keep a file in which you save the commands that worked. Emacs matches parentheses, which is a life-saver with Lisp-style languages. M-p and M-n go to the previous and next command.

    In Cygwin, Emacs shell mode is a DOS shell. That's no good. To fix that, open the  ~/.emacsfile. Add this mumbo-jumbo (everything in the gray box). Quit and restart Emacs.

    Type M-x shell (that is, Alt+X followed by the letters shell and the Enter key). Voila, a Cygwin shell inside Emacs.

  9. Many programming languages have their own Emacs support that require you to edit your .emacs file. For example, to add Emacs support to Scala, add the following:
    (add-to-list 'load-path "/path/to/scala/misc/scala-tool-support/emacs")
    (require 'scala-mode-auto)

    Turn it on, then select all text in a Scala file and hit Tab to see auto-indenting.

Tips, Tricks, and Troubleshooting

  1. You need to run apt-cyg and gem as an administrator. When you are done installing software, close the shell and launch another shell as a regular user.
  2. For better command line recall, make a file ~/.inputrc containing
    # Makes PgUp, PgDn search history
    "\e[5~": history-search-backward
    "\e[6~": history-search-forward

    Then you can type the first few letters of a command that you typed previously, followed by the Page Up key. For example, javac followed by Page Up is the last invocation of javac. Hit Page Up again to get the next one. Once you have one that is close to what you want, edit it and hit Enter.

  3. 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 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.

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

  5. 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
  6. If some program mysteriously fails to run, or you get an error message about a library that cannot load, try cygcheck, such as cygcheck ruby or cygcheck /path/to/libfoo.so.
  7. Here are more tips by a sophisticated Cygwin user.
  8. And here are some useful Emacs tips.
  9. 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.