sagotsky.github.io

home tags

RC files

28 Jul 2008 bash

I’ve lately found myself posting and dissecting my beloved .rc files. I suppose it’s time to document them ad nauseum so I can refer to the post later. I went to all the trouble to keep them synced up over CVS, so I might as well discuss the files themselves.

Note that I’m going to be limited myself to the settings that are interesting or unique. No sense in posting export PAGER=’less’.

.alias (as sourced in .bashrc)

alias e='emacs -nw'

Okay, so this one isn’t actually interesting at all. I just thought it was worth mentioning how lazy I am. nano is n and vim is v. Moving on.

alias tf='tail -f'

Haven’t seen this one posted anywhere else. It’s pretty straightforward for what it does. For some odd reason my head treats reading the last n lines from a file, and following all lines written to a file as two separate commands. As such, tail -f needed its own command. I use this one all the damn time.

alias sudo='A=aliassudo '

One of my favorites. This one preserves all my aliases in sudo. So “sudo e file.txt” will actually work. I haven’t a clue why there’s a variable called A. I didn’t have a clue when I wrote it either. But it works.

alias su='su -m'

This also preserves my settings, but it does so when I become root. This is important in conjunction with my prompt (see .bashrc). Note that this is not always desirable so I end up running /bin/su to ditch the -m.

function lw() {
  if [ $# -gt 0 ] ; then
    for bin in $@ ; do ls -lh `which $bin` ; done
  else
    echo "lw (ls which) requires more than 0 arguments"
      fi
}

Ahh, lastwhich. Great for anyone using java on different machines. I often found myself using which to find the path of a binary I was running, only to find it was a symlink to some other binary (usually the case when several versions of an app are installed). Then I have to put the last command in back ticks so that the location of the symlink becomes the argument for ls -lh. Ugh. lw takes any number of arguments, runs which on them, and runs ls -lh on that. Now I just have to figure out how to make it recurse (ie handle /usr/bin/java -> /etc/alternatives/java -> /usr/bin/gij-4.2).

function x() {
  for a in $@
    do if [ -f $a ] ; then
      case $a in
        *.tar.bz2) tar -xvjf $a ;;
        *.tar.gz) tar -xvzf $a ;;
        # snip snip
        *) echo "cannot extract" ;;
      esac
    fi
  done
}

Yeah, I finally gave in and made an extractor function. So shoot me. At least having this around will remind me that bash has case switches.

.bashrc The most important thing in my .bashrc is the prompt. Bright flashy prompts with the entire contents of gkrellm annoy the hell out of me. Mine is just

DEFAULT='0;39m'
PROMPT_COLOR=`echo $DEFAULT`

First I set some code variables. 0; 39m is the default color from the terminal. Not sure why I had to echo to get PROMPT_COLOR to set. I do these as variables for two reasons. One is because I hate reading 0; 39m in my $PS1 – this is just easier. The important reason is …

if [ ${UID} -eq 0 ]; then
PROMPT_COLOR='37;41m'
fi

Remember what I said about root getting his own color? This is where that magic happens. Now if I tell $PS1 to include ${PROMPT_COLOR} I get white on red, but only as root.

if [ -z "$debian_chroot" -a -r /etc/debian_chroot ]; then
  debian_chroot=$(cat /etc/debian_chroot)
fi

As the proud owner of a 64bit proc, I am often forced to use a 32bit chroot environment. To keep track of which terms are and aren’t in the chroot, I put a file called /etc/debian_chroot inside my chroot. The contents is the name of the chroot. That gets read into a variable which goes in my prompt so that instead of user@host I get user@host.chroot.

MYPROMPT='<\T> \[\e[${PROMPT_COLOR}\]\u@\h\[\e[${DEFAULT}\]${debian_chroot:+.$debian_chroot}:\w\$ '
PS1="$MYPROMPT"

Here’s where we set $PS1. Why a separate variable? I have one shenanigan:

case $TERM in
  xterm*|aterm|screen)
  PS1="\[\033];\u@\h\007\]$MYPROMPT"
  ;;
esac

For most terminals we use $MYPROMPT as $PS1. These three terminals get another string prepended. What that extra bit of text does is set the title of the window to user@host. Not all terminals support this, which is why I had to add it to the ones that would. Where this comes in handy is with ssh. Now I have the titlebar of each xterm running ssh tell me where I’ve ssh’ed to.

Finally, .screenrc. This one I stole from a coworker. I see a lot of people pimping xterm alternatives that allow for tabs. I’ve never seen the point as screen does tabs (and more) for you.

caption always "%{= ..}%-w%{= BW}%n %t%{-}%+w %-= @%H - %D %d %M - %c"

This setting sets the bottom line of your screen session to display the numbers and names of each of your windows in the screen session. It effectively shows your tabs. And it does this in any modern terminal emulator. It also shows the date.