home

Cool .bashrc Tricks

Prompt

Terminal programmes such as xterm, rxvt and konsole understand special escape sequences to set text colours and the contents of the title bar (i.e. the window name) and in case of the great konsole the text in the tab button. This has been described here on the old KDE-Rocks page before. But the code there was not optimal because it used two external sed calls and the prompt hadn’t been redrawn on C-l, C-a or C-b. This code here has been improved with the kind help of Michael Henry, a fellow bash hacker of Maryland, United States.

A konsole window with a fancy prompt.
A konsole window with a fancy prompt.
# set defaults
PROMPT_COMMAND=
PS1="\u@\h \w $ "
PS2="> "
PS3="#? "
PS4="+"

# special handling for colour terminals
case $TERM in

    xterm*|screen)
	PROMPT_COMMAND='
           pcpwd=${PWD/$HOME/\~}
           [[ "${#pcpwd}" -gt 25 ]] && pcpwd="..${pcpwd: -25}"
           titlebar="\033]0;${USER}@${HOSTNAME} ${PWD/$HOME/~}\007"
           tabname="\033]30;${HOSTNAME}\007"
           echo -ne "$titlebar$tabname"
           green="\033[32m"; orange="\033[33m"; cyan="\033[36m"; off="\033[m";
           PS1="$green\u$orange@$green\h $cyan${pcpwd}$orange \$ $off"
        '
        ;;

    dumb)
	PROMPT_COMMAND=
	PS1="\u@\h \w $ "
	;;

    *)
        ;;

esac

Notes/explanations:

Console Colours

A handy routine that prints out most of the possible combinations of colours. Many thanks to Roland Stahn for enhancing the script and pointing out a comprehensive reference (http://en.wikipedia.org/wiki/ANSI_escape_code).

#!/bin/bash
for fgcol in 30 31 32 33 34 35 36 37 -1; do
  for bold in 0 1; do
    for bgcol in 40 41 42 43 44 45 46 47 -1; do
	# bg + fg
	[[ ! $fgcol -eq -1 ]] && [[ ! $bgcol -eq -1 ]] && \
	    echo -ne "\033[${bold};${fgcol};${bgcol}m\\\\033[${bold};${fgcol};${bgcol}m\033[0m "
	# only fg (last column)
	[[ ! $fgcol -eq -1 ]] && [[   $bgcol -eq -1 ]] && \
	    echo -ne "\033[${bold};${fgcol}m\\\\033[${bold};${fgcol}m   \033[0m "
	# only bg (last two rows)
	[[   $fgcol -eq -1 ]] && [[ ! $bgcol -eq -1 ]] && \
	    echo -ne "\033[${bold};${bgcol}m\\\\033[${bold};${bgcol}m   \033[0m "
	# only bold
	[[   $fgcol -eq -1 ]] && [[   $bgcol -eq -1 ]] && \
	    echo -ne "\033[${bold}m\\\\033[${bold}m      \033[0m "
    done
    echo
  done
done
# eof

A routine to open files in The One True Editor

export EDITOR='emacs -nw'
alias emacsnox='DISPLAY= /usr/bin/emacs -nw'
function e()
{
    if [ $# -lt 1 ]; then
	echo "No file specified you have, think before you must."
    else
	if emacsclient -n "$1" >/dev/null 2>&1; then
	    echo "Alrite, opened $1 in the Holy GNU Emacs."
	else
	    echo "There's no Holy GNU Emacs running here.. starting one."
	    exec emacs -f server-start "$1" &
	fi
    fi
}

Various

# default printer
export PRINTER=HPG56

# locale settings
export LANG=de_CH
export LC_ALL=de_CH
export LC_CTYPE=de_CH

# make rm interactive by default (recommended)
alias rm='rm -i'

# colour ls and ls shortcuts
alias ls='ls --color=auto -p'
alias ll='ls -l'
alias la='ls -la'

# quickly check out a CD
alias mcd='mount /cdrom && pushd . && cd /cdrom'
alias umcd='popd && umount /cdrom && eject /dev/cdrom'

# Sets the search path for LaTeX files to the current directroy,
# ~/LaTeX (including sub-directories) and the system defaults.
export TEXINPUTS=".:~/LaTeX//:"

# KDE tricks
alias khtml='kfmclient openURL'
alias konq='kfmclient openURL'
alias k='kfmclient exec'
alias kmv='kfmclient move'
alias kcp='kfmclient copy'

# makes less clever
export LESSCHARSET=latin1
eval $(lesspipe)

# enscript options
export ENSCRIPT="--word-wrap"

# a must-have
export BROWSER=firefox

References

Visitors to this page so far.