My Ultimate Cygwin Setup

I sat down today to do some programming, and I got a little bit distracted improving my environment, but I think that where I’ve got to is quite good, so I’ll share it with you. So, what are my requirements?

  • a unix-like console with
  • an easy mechanism for installing packages
  • git, python, and other useful things
  • summary information about my git repos, at the command prompt
  • auto-completion for git
  • shortcuts to frequently used folders, and some other conveniences

A unix-like console on Windows

Have you heard of Cygwin? Cygwin is great. Cygwin is unix on Windows. Sometimes I think Microsoft should just buy Cygwin, and make it a first-class citizen of Windows. Installing Cygwin is easy, but the base install doesn’t come with many packages; you can re-run the installer to add more, but if you’re like me and you don’t save your downloads to disk, it can be a bit of a pain. Get the installer here. Don’t finish the installation just yet because…

An easy mechanism for installing packages

You’ll want to install a couple of extra packages required to install apt-cyg. I learned how to install it here. You’ll want to install cygwin with the following extra packages, to make sure you can run apt-cyg:

  • wget
  • tar
  • bzip2
  • subversion
  • vim

Once Cygwin is up and running do the following:

$ svn --force export http://apt-cyg.googlecode.com/svn/trunk/ /bin/
$ chmod +x /bin/apt-cyg

If you’re running the x86_64 version of Cgywin (which I recommend if you’re on 64-bit Windows), then you’ll also want to open up /bin/apt-cyg in a text editor:

$ vim /bin/apt-cyg

and change the following two lines

98:  wget -N $mirror/setup.bz2 to wget -N $mirror/x86_64/setup.bz2
105: wget -N $mirror/setup.ini to wget -N $mirror/x86_64/setup.ini

Now you’re ready to install some more useful tools!

Git, Python and other useful things

bash-completion – auto-complete for bash
ca-certificates
– allows your Cygwin environment to validate SSL certificates
curl – a useful command-line tool for accessing urls, similar to wget, but more powerful
git – a distributed version control tool
git-svn – lets git play nice with SVN
python – an interpreted programming language. I like to write shell scripts with it
python-setuptools – people use this when distributing their python stuff
rsync – handy tool for synchronizing stuff from one place to another (especially over the internet)

You should be able to install all of these with apt-cyg, it’ll handle all the dependencies:

$ apt-cyg install bash-completion$ apt-cyg install ca-certificates
$ apt-cyg install curl
$ apt-cyg install git
$ apt-cyg install git-svn
$ apt-cyg install python
$ apt-cyg install python-setuptools
$ apt-cyg install rsync

[Update]
I forgot to mention ncurses, a library for writing text-base interfaces. I use it to create cls (see my update lower down).

$ apt-cyg install ncurses

Summary information about my git repos, at the command prompt

I have been using posh-git inside Windows Powershell, The thing I like about posh-git is that it gives you an overview of your repository status at your command prompt, like so:

D:\Users\Guy\Programming\git\bitlyWin8 [master +5 ~9 -0 | +0 ~0 -6]>

Telling you which branch you are on, and giving you a summary of both tracked, and un-tracked changes. However, this can make my prompt quite slow in large repositories, because it takes quite a while to run the script which generates it, but I still want something like it in cygwin. What I have found, although less detailed than posh-git, is nice and quick, and it gives me a simple indicator of what I need to do with my repository. There is a script called git-prompt.sh which can do some nice things with your prompt, so let’s go ahead and get that. We’ll be adding a few shell scripts that get run when you open a cygwin terminal window, and I like to keep these in a bin folder inside my home folder. We’ll download the shell scripts to there:

$ cd ~
$ mkdir bin
$ cd bin
$ wget 
https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh

Now edit your .bash_profile to add git command prompting to your bash sessions. The top of the git-prompt.sh file has a good explanation on the options, which are controled by environment variables.

source ~/bin/git-prompt.sh

# Show if there are unstaged (*) and/or staged (+) changes
export GIT_PS1_SHOWDIRTYSTATE=1

# Show if there is anything stashed ($)
export GIT_PS1_SHOWSTASHSTATE=1

# Show if there are untracked files (%)
export GIT_PS1_SHOWUNTRACKEDFILES=1

# Show how we're tracking relative to upstream
export GIT_PS1_SHOWUPSTREAM="verbose"

# Save the old command prompt, and set the new one to  <number of commands issued> user:directory (branch <status symbols>)$ eg: 167 Guy:Arduino (master u=)
PS1_OLD=${PS1}
export PS1='\[33[1;34m\]\!\[33[0m\] \[33[1;35m\]\u\[33[0m\]:\[33[1;35m\]\W\[33[0m\] \[33[1;92m\]$(__git_ps1 "(%s)")\[33[0m\]$ '
export PS1='\[33[1;34m\]\!\[33[0m\] \[33[1;35m\]\u\[33[0m\]:\[33[1;35m\]\W\[33[0m\] \[33[1;92m\]$(__git_ps1 "(%s)")\[33[0m\]\$ '

Auto completion for git

We already installed bash-completion and now all we need to do is add a script that supplies completion functions for git. To download the scritpt:

cd ~/bin
curl https://github.com/git/git/raw/master/contrib/completion/git-completion.bash -OL

And then to add it to your .bash_profile

source ~/bin/git-completion.bash

Shortcuts to frequently used folders, and some other conveniences

Cygwin maps your Windows filesystem to /cygdrive/<drive letter>/ but this can be a bit tedious to get to, so I’ve created some shortcuts. You might like to break these out into a separate file if you end up with heaps of them, but I’ve only got a few for now. Open up your .bash_profile and add the following things:

alias cdp='cd /cygdrive/d/Users/Guy/Programming'
alias cda='cd /cygdrive/d/Users/Guy/Documents/Arduino'
alias cdc='cd /cygdrive/c'
alias cdd='cd /cygdrive/d'

[Update]
Cygwin doesn’t have cls out of the box. But with ncurses installed, you can use the commmand tput clear to do the same thing. I aliased it to cls: (thanks mikyra)

alias cls='tput clear'

[Update 2]
There’s a terminal command open in OSX that is quite nice, it essentially does a shell execute on whatever you pass it. Cygwin has something similar called cygstart, but that’s not a nice name. (thanks erichui).

alias open='cygstart'

I’ve also added an alias to reload my bash profile (so that later on I can edit it, and the see my changes easily).

alias reload='source ~/.bash_profile'

Finally, I like to add that bin folder I created to the path:

export PATH="${HOME}/bin:${PATH}"

Acknowledgements

  1. Taylor McGann’s blog was useful in showing me how to do a bunch of the git prompt, git completion, and fancy bash profile stuff.
  2. This StackOverflow post on apt-cyg.
  3. This StackOverflow post on clearing the screen.
  4. This StackOverflow post on cygstart.

9 thoughts on “My Ultimate Cygwin Setup

  1. I had a question about this. What’s all the crazy stuff with the brackets and stuff…

    When I use one of the export lines below, i get a bunch of what appears to be exactly what is defined in PS1. As opposed to what you have in your “eg: 167 Guy:Arduino (master u=)”. Am I missing something or do i have to modify the “\[33[1;34m\] ……” to be something more accurate?

    # Save the old command prompt, and set the new one to user:directory (branch )$ eg: 167 Guy:Arduino (master u=)
    PS1_OLD=${PS1}
    export PS1=’\[33[1;34m\]\!\[33[0m\] \[33[1;35m\]\u\[33[0m\]:\[33[1;35m\]\W\[33[0m\] \[33[1;92m\]$(__git_ps1 “(%s)”)\[33[0m\]$ ‘
    export PS1=’\[33[1;34m\]\!\[33[0m\] \[33[1;35m\]\u\[33[0m\]:\[33[1;35m\]\W\[33[0m\] \[33[1;92m\]$(__git_ps1 “(%s)”)\[33[0m\]\$ ‘

    1. Hi Josh,

      The crazy square brackets etc are a series of escape sequences which control the color of your prompt. I think I used this as a starting point. Hope that helps.

      Cheers,

      Guy.

  2. Maybe it is already known, but I thought it’s useful to mention that Ctrl + L is a built-in shortcut for the cygwin terminal to clear the screen! 🙂 Nice setup!
    Cheers,
    Adrian

  3. Great post! I was wondering how to install packages from within cygwin. However, I have some suggestions for clarity that might help out others. I kept getting md5 checksum errors, so I also had to comment out another part of “/bin/apt-cyg” by typing “/” in vim to perform a search for ” MD5 sum did not match, exiting”. The line below reads “exit 1”. Just put a “#” in front to comment it out. The final should look like this

    echo MD5 sum did not match, exiting
    #exit 1

    I also like to indicate that I bypassed the function by editing the echo line like this

    echo –bypased– MD5 sum did not match, exiting
    #exit 1

    1. alias cyg-get=”C:/Users/Default.Default-PC/Downloads/setup-x86.exe -q -P”
      cyg-get

      Are the commands I used for getting packages installed while in Cygwin
      You might need to modify it for your needs though, sorry if I was way late on giving you help though.

  4. I used to think the same way about Cygwin 64 bit until I started finding that packages I took for granted were missing. I thought I was going mad before realising that some packages in Cygwin 32 bit don’t exist in 64 bit. Went back to Cygwin 32 bit and everything was right again. Yes, it’s nice to embrace the new tech, but if you want to get things done, stick with Cygwin 32 bit.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s