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:
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
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.
# Show if there are unstaged (*) and/or staged (+) changes
# Show if there is anything stashed ($)
# Show if there are untracked files (%)
# Show how we're tracking relative to upstream
# 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=)
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:
curl https://github.com/git/git/raw/master/contrib/completion/git-completion.bash -OL
And then to add it to your .bash_profile
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'
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'
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).
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:
- 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.
- This StackOverflow post on apt-cyg.
- This StackOverflow post on clearing the screen.
- This StackOverflow post on cygstart.