Sunday, August 03, 2008

Cygwin Command Prompt

Linux shells use the PS1 variable to determine what the command prompt should look like. This is the command prompt I use in my shell:

export PS1="\[\e]2;\u@\H - ${workSpaceName} - \w\a\e[32;1m\n\s\v - \u@\H - ${workSpaceName} - \w\n\d - \@ > \e[0m"

It gives me a prompt like this:

-bash3.2 - RobertMarkBram@orson - SCJP - /cygdrive/d/WorkSpaces/scjp
Sun Aug 03 - 11:52 PM >
And gives my shell this window title (also used in the alt+tab display). RobertMarkBram@orson - SCJP - /cygdrive/d/WorkSpaces/scjp

Edit 7/08/2008 12:35:39 PM: see my comments below.

IBM's page, Tip: Prompt magic helped me build it.

You can also use the PROMPT_COMMAND variable. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt. It isn't the same as the PS1 variable, because the PROMPT_COMMAND string is actually executed, whereas PS1 is parsed to form a string.

For example, this:

export PROMPT_COMMAND='echo -ne "\nnice prompt"'

Gives me this (in combination with my PS1):

nice prompt
-bash3.2 - RobertMarkBram@orson - SCJP - /cygdrive/d/WorkSpaces/scjp
Sun Aug 03 - 11:58 PM >

The Linux BASH syntax : prompt variable can give you a bit more detail about what you can put there.

Inputrc for bash history completion using up/down arrows

The bashrc file stores key mappings. Use your own bashrc by putting export INPUTRC=~/.inputrc in your .bash_profile or .bashrc

This page, Creating the /etc/inputrc File and this page, Super-useful inputrc give some useful advice about things you can put in those files.

One of things I most often find myself doing is searching my command line history. I frequently use the cursor up and down to scroll through my most recent commands. Often, I want to re-use a particular grep or find that I used recently, but I don't remember the specifics of it. With the text below in your .inputrc, you can type in the first few letters, say gr or f and press the cursor keys and it will scroll through your command history, showing the commands that began with those characters. Nice. :)

# By default up/down are bound to previous-history
# and next-history respectively. The following does the
# same but gives the extra functionality where if you
# type any text (or more accurately, if there is any text
# between the start of the line and the cursor),
# the subset of the history starting with that text
# is searched (like 4dos for e.g.).
# Note to get rid of a line just Ctrl-C
"\e[B": history-search-forward
"\e[A": history-search-backward

$if Bash
  # F10 toggles mc on and off
  # Note Ctrl-o toggles panes on and off in mc
  "\e[21~": "mc\C-M"

  #do history expansion when space entered
  Space: magic-space
$endif

# Include system wide settings which are ignored
# by default if one has their own .inputrc
$include /etc/inputrc

Source: Inputrc for bash history completion using up/down arrows

Cygwin and Putty - a wonderful combination!

Updates
16/07/2009 12:33:04 PM - added reference from David Jones' blog entry: PuttyCyg on how to set up PuttyCyg.

I have been using Cygwin for many years now and I find it extremely useful. It is a Linux on Windows tool that lets me do amazing things with a few shell scripts. Putty is a terminal that is also very powerful, and a pleasure to use. :)

Today I found PuttyCyg which allows me to use PuTTY as a local Cygwin terminal.

It is easy to setup, just extract the Zip and create a shortcut with putty.exe as a target and "-cygterm -" as a parameter.
-- David Jones' blog entry: PuttyCyg.