Sunday, October 21, 2012

A Proper Cygwin Environment

I've been spending more time in Cygwin lately, after having decided to wean myself from Ye Olde Windows Command Prompt. (Actually, I've been using 4NT since back in the day when it was called 4DOS and ran as a replacement for COMMAND.COM.) As with anything else I use, "out of the box" is just a starting point, and I've spent some time creating a proper Cygwin environment. Below is a description of my setup.

Mapping Cygdrive

By default, Cygwin mounts all Windows drives under the /cygdrive/ mount point. Who wants to look at and type that all day? Certainly not me. Edit /etc/fstab and replace the existing line:
none /cygdrive cygdrive binary,posix=0,user 0 0
with:
none / cygdrive binary,posix=0,user 0 0 
Now I can type cd /c/Users/ instead of cd /cygdrive/c/Users.


Mintty

Cygwin normally runs in a standard Windows command window. There are better ways to run Cygwin, and Mintty is one of the best. Bring up an Explorer window, navigate to cygwin/bin, and find mintty.exe. Make a shortcut and move it someplace more convenient. To make Mintty read your ~/.bash_profile you'll need to edit the command line parameters. Right-click your shortcut and select Properties. Add -e /bin/bash --login to the end of the command line. Now bash will run your ~/.bash_profile when you start Mintty.


Colors and Fonts

(Colors and fonts are an awfully subjective matter, but this section is as much about how to set things up as it is about which colors and fonts I like.)

After a bit of initial doubt, I've become a fan of the Solarized color scheme. A tip of the hat to Ethan for creating a very readable color scheme that can be applied to just about any application. So let's Solarize Cygwin.


Mintty

I have much love for Mintty, but its default colors are boring. To Solarize them, go to mintty-colors-solarized. Select either .mintty--solarized-dark or .mintty--solarized-light and insert the contents of this file into your ~/.minttyrc file. (Which may not yet exist.) You might also add the line Term=xterm-256color if you intend to use Solarized in vim; see below for more on that.

dircolors (ls)

Solarized directory colors can be found at dircolors-solarized. I've put the contents of dircolors.ansi-dark into my ~/.dir_colors. Set up an alias for ls and evaluate dircolors in ~/.bashrc:
eval `dircolors ~/.dir_colors`
...
alias ls="ls --color" 


vim

I'm sure Emacs is wonderful, I just never learned it. Terribly sorry, but you'll have to go elsewhere to get your Solarized Emacs love. Now, for those of you still reading, go to vim-colors-solarized and get colors/solarized.vim. Put this file into ~/.vim/colors. Edit (create if necessary) ~/.vimrc and ensure it contains:
syntax enable
set hlsearch
set background=dark
let g:solarized_termcolors=256
let g:solarized_termtrans=1
colorscheme solarized
That wasn't so hard, was it?

Fonts

There are plenty of monospaced fonts available on the web, some very good. I have been going back and forth between Consolas and Adobe's recently released Source Code Pro. Both are very well designed; I find them to be equally readable, and I'm pretty picky about fonts. Another very good font is Proggy, which I used for years. At the moment I'm using Consolas at 9 point bold, but that may change....

August 9 2013 Update


Customizing ~/.bashrc for Fun and Profit


Google "customizing bashrc" and you can go crazy; there are a lot of examples out there. This page has a bunch if you have a few minutes of idle time to waste. Here's a pro tip: don't go crazy adding a bunch of aliases, functions, and custom Bash configuration variables. Instead, add things a little at a time, over time, as you discover a need for them (where "need" can be a pretty low barrier, i.e. I've added an alias after spending a couple minutes figuring out a complicated command because I know I'll need it again). Okay, here are the more important bits of my ~/.bashrc, broken up into bite-sized pieces:

# Return immediately if we are not interactive
[ -z "$PS1" ] && return


The first thing we do is exit if we're in a non-interactive shell. This is kind of important. (And if you intend to use Cygwin/Bash in earnest you will need to understand the difference between an interactive and non-interactive shell.) This next block is dedicated to setting the window title and Bash prompt.

# get current git branch name
function git_branch {
    export gitbranch=[$(git rev-parse --abbrev-ref HEAD 2>/dev/null)]
    if [ "$?" -ne 0 ]
      then gitbranch=
    fi
    if [[ "${gitbranch}" == "[]" ]]
      then gitbranch=
    fi
}

# set usercolor based on whether we are running with Admin privs
function user_color {
    id | grep "Admin" > /dev/null
    RETVAL=$?
    if [[ $RETVAL == 0 ]]; then
        usercolor="[0;35m";
    else
        usercolor="[0;32m";
    fi
}

# set TTYNAME
function ttyname() { export TTYNAME=$@; }

# Set prompt and window title
inputcolor='[0;37m'
cwdcolor='[0;34m'
gitcolor='[1;31m'
user_color

# Setup for window title
export TTYNAME=$$
function settitle() {
  p=$(pwd);
  let l=${#p}-25
  if [ "$l" -gt "0" ]; then
    p=..${p:${l}}
  fi
  t="$TTYNAME $p"
  echo -ne "\e]2;$t\a\e]1;$t\a";
}

PROMPT_COMMAND='settitle; git_branch; history -a;'
export PS1='\[\e${usercolor}\][\u]\[\e${gitcolor}\]${gitbranch}\[\e${cwdcolor}\][$PWD]\[\e${inputcolor}\] $ '
}


There's rather a lot going on here. The important lines are PROMPT_COMMAND and export PS1. PROMPT_COMMAND is a command run by Bash after each command returns. When you type ls <Enter>, the PROMPT_COMMAND is run just after ls finishes executing, before the prompt is displayed. My prompt command (1) sets the window title, (2) gets the current Git branch, and (3) flushes the history. Setting the window title takes a little work. I'm setting it to the $TTYNAME variable followed by the last 25 characters of the current working directory. By default, $TTYNAME is the term's pid, but I can use e.g. ttyname SomeProject to change it to something more meaningful. Why go to all this trouble? So that when I have multiple terms open and want to switch between them, I can see the window title when I use Alt-Esc or hover over the taskbar mintty icons. Instead of randomly switching between terms all named "-bash" and hoping I pick the right one.

PS1 aka The Bash Prompt

There are a lot of interesting, cool, informative, funny, unique examples of Bash prompts on the web. Here's mine as set in the above export PS1 command:

export PS1=\[\e${usercolor}\][\u]\[\e${gitcolor}\]${gitbranch}\[\e${cwdcolor}\][$PWD]\[\e${inputcolor}\] $

And here's an example of what it looks like:




Let me break that down a little. The first part, "twheeler", is my username (\u), printed in yellow when I'm a normal user and purple when I open a term as an administrator (right-click on your mintty icon and select Run As Administrator...). Just so I know when I'm a god on the machine. The user_color function sets the usercolor variable to the right ANSI color sequence based on my privileges, and it's then echoed in the prompt with ${usercolor}.

Next is the current Git branch I'm working in, if the current directory is part of a Git project. The git_branch function, called by PROMPT_COMMAND, helps set this up, and it's echoed with ${gitcolor}.

Finally I print the current directory with $PWD and end it all with a string and space.

The next part of my .bashrc sets up some history settings:

# Bash history settings
export HISTFILESIZE=1000000
export HISTSIZE=100000
export HISTCONTROL=ignorespace
export HISTIGNORE='ls:history:ll'
export HISTTIMEFORMAT='%F %T '
# Append to bash_history instead of overwriting
shopt -s histappend

For another take on Cygwin setup check out this page.

11 comments:

Richard Kulesus said...

Thanks so much for sharing this! I've really struggled switching from OS X and *nix at home to Cygwin at work. You helped me at least make the Cygwin experience easier to see and use. Thanks for the instruction on VIM and LS in particular!

JehĂș said...

Thank you so very much.
I'm about the only person at work that uses cygwin, and this has allowed me to understand slightly better what's going on.

Please keep sharing your insights on cygwin.

NitKart said...

This is so much awesome. Especially the solarized scheme for vim. Working in it is a more pleasurable affair now. Thanks a lot!

Josh said...

Thank you very much for your post! Very helpful! There does seem to be a small typo though in your .bashrc example. I believe the last curly brace is supposed to be right after the user_color function.

Pierre said...

very usefull, thanks !

Just as a (very) minor note : your .bashrc is hardcoded to solarized darck, as you set the prompt color (and I have not yet found a way to switch the color to the normal solarized light prompt color).

Steve Bennett said...

Thomas, thanks for these customization files. I work with Cygwin regularly and I've always partially customized it but never had the full solarized colors set up. This was very helpful!

Patrick Reynolds said...

Hi, thanks for writing this. I've been trying to get it to work, having followed all instructions to the letter, and still ls is just outputting shades of grey. Is there some setting or variable I can check to try to debug this? Thanks again.

Patrick Reynolds said...

Please ignore my last comment; it was a goofy error on my part. All works very well!

Stefan said...

Thomas,

thanks for this great post which really gave my cygwin and vim a boost!

anyhow pasting your bashrc script I am getting the bash error:

t command not found

any advice?

Thomas Wheeler said...

Hi Stefan, I don't think I have enough information to be able to help. Can you provide more details about what you are doing?

Pietro Grandi said...

Thank so much for sharing those informations: you made my day man!!!