←back to thread

756 points speckx | 1 comments | | HN request time: 0.201s | source
1. dcassett ◴[] No.45677391[source]
I find that I like working with the directory stack and having a shortened version of the directory stack in the title bar, e.g. by modifying the stock Debian .bashrc

  # If this is an xterm set the title to the directory stack
  case "$TERM" in
  xterm*|rxvt*)
      if [ -x ~/bin/shorten-ds.pl ]; then
    PS1="\[\e]0;\$(dirs -v | ~/bin/shorten-ds.pl)\a\]$PS1"
      else
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h:   \w\a\]$PS1"
      fi
      ;;
  \*)
      ;;
  esac
The script shorten_ds.pl takes e.g.

  0  /var/log/apt
  1  ~/Downloads
  2  ~
and shortens it to:

  0:apt 1:Downloads 2:~

  #!/usr/bin/perl -w
  use strict;
  my @lines;
  while (<>) {
    chomp;
    s%^ (\d+)  %$1:%;
    s%:.*/([^/]+)$%:$1%;
    push @lines, $_
  }
  print join ' ', @lines;

That coupled with functions that take 'u 2' as shorthand for 'pushd +2' and 'o 2' for 'popd +2' make for easy manipulation of the directory stack:

  u() {
    if [[ $1 =~ ^[0-9]+$ ]]; then
      pushd "+$1"
    else
      pushd "$@"
    fi
  }

  o() {
    if [[ $1 =~ ^[0-9]+$ ]]; then
      popd "+$1"
    else
      popd "$@" # lazy way to cause an error
    fi
  }