25 February 2008 11:31 PM

fancy bash promptI have looked for this for a long time: how to have good fancy bash prompt. I found several example made in 1996 or 1998 that I had to tweak a lot.

But this was tricky and never work well, for example when I wanted to search a command back in the history using the arrows (up and down) or the CTRL-R key binding, there was a column problem.
If I wanted to change some chars or words in the command I picked in the history, they were not where I saw them, also sometimes a part of the prompt was hidden by the command. Not really useful.

Recently, I gave a try to BashStyle-ng to see how it could tweak my bash prompt. And I was surprised to see an option to have an “elite” prompt (which was the name of the old prompt I tweaked before). So I choose this one and see that it needs some tweak (again) to looks like what I wanted.
I search for the function in BashStyle code, found it and start to make some changes, and was happy to see that my history bug doesn’t come up. After a while (not so long), I find a way to have it exactly how I wanted.
So here’s the first part of my new .bashrc heavily inspired by bashstyle :
Note : As the special chars that draw the corners and line won’t appear on a web page, I have put the file in attachement at the bottom of the post.

#!/bin/bash
	
  1. based on a function found in bashtstyle-ng 5.0b1
  2. Original author Christopher Roy Bratusek (http://www.nanolx.org)
  1. Last arranged by ayoli (http://ayozone.org) 2008-02-04 17:16:43 0100 CET

    function pre_prompt {
    newPWD=”${PWD}”
    user=”whoami”
    host=$(echo -n $HOSTNAME | sed -e “s/[\.].//”)
    datenow=$(date “%a, %d %b %y”)
    let promptsize=$(echo n “-($user@$host ddd, DD mmm YY)—-(${PWD})—-” \ | wc -c | tr d ” ”)
    let fillsize=${COLUMNS}
    ${promptsize}
    fill=””
    while [ ”$fillsize” -gt “0” ]
    do fill=”${fill}—”
    let fillsize=${fillsize}-1
    done
    if [ ”$fillsize” lt “0” ]
    then let cutt=3
    ${fillsize} newPWD=”...$(echo -n $PWD | sed -e “s/\(^.\{$cutt\}\)\(.
    \)/\2/”)”
    fi

    }

    PROMPT_COMMAND=pre_prompt

    export black=”\[\033[0;38;5;0m\]”
    export red=”\[\033[0;38;5;1m\]”
    export green=”\[\033[0;38;5;2m\]”
    export yellow=”\[\033[0;38;5;3m\]”
    export blue=”\[\033[0;38;5;4m\]”
    export magenta=”\[\033[0;38;5;55m\]”
    export cyan=”\[\033[0;38;5;6m\]”
    export white=”\[\033[0;38;5;7m\]”
    export coldblue=”\[\033[0;38;5;33m\]”
    export smoothblue=”\[\033[0;38;5;111m\]”
    export iceblue=”\[\033[0;38;5;45m\]”
    export turqoise=”\[\033[0;38;5;50m\]”
    export smoothgreen=”\[\033[0;38;5;42m\]”

    PS1=”$green??($coldblue\u@\h \$(date \”%a, %d %b %y\”)$green)?\${fill}?($coldblue\$newPWD\
    $green)???\n$green($coldblue\$(date \”
    %H:%M\”) \$$green)?>$white ”


Note : As the special chars that draw the corners and line won’t appear on a web page, I have put the file in attachement at the bottom of the post.

The second part is more about the aliases and other settings of my shell :

		
  • bash_history settings: size and no duplicates and no lines w/ lead spaces
    exportHISTCONTROL=”ignoreboth”
    export HISTSIZE=1024

  • aliases #############################################
  • enable color support of ls and also add handy aliases eval `dircolors -b` alias ls=’ls—color=auto’ alias dir=’ls—color=auto—format=vertical’ alias vdir=’ls—color=auto—format=long’
  • some more ls aliases alias ll=’ls -lhX’ alias la=’ls -A’ alias ldir=’ls -lhA |grep ^d’ alias lfiles=’ls lhA |grep ’ #alias l=’ls -CF’
  • To see something coming into ls output: lss
  • alias lss=’ls -lrt | grep $1’
    1. To check a process is running in a box with a heavy load: pss
    alias pss=’ps -ef | grep $1’
    1. usefull alias to browse your filesystem for heavy usage quickly
    alias ducks=’ls -A | grep -v -e ’\’‘\.\.$’\’’ |xargs -i du -ks {} |sort -rn |head -16 | awk ’\’’{print $2}’\’’ | xargs -i du -hs {}’
    1. cool colors for manpages
    alias man=”TERMINFO=~/.terminfo TERM=mostlike LESS=C PAGER=less man”

    ##########################################################

    1. enable programmable completion features (you don’t need to enable
    2. this, if it’s already enabled in /etc/bash.bashrc).
      if [ -f /etc/bash_completion ]; then . /etc/bash_completion
      fi

    3. CDPATH initialisation
    4. CDPATH=.:~:/media
    download bashrc.txt (backup your ~/.bashrc, then download this using right clic and “Save link as” then put it in your home and rename it to .bashrc).

    8 Responses to “Bash : fancy prompt and improvements”

    1. Don Fenice says:

      Tres interessant ces alias, notamment l’alias ducks.

      Pour les scan de fichiers du repertoire courant j’utilise cet alias, moins poussez que le ducks mais assez utile :
      alias lh=’ls -sShr1’

    2. ayoli says:

      I found this ducks alis a few years ago on a website. Actually, I don’t it much. I even not sure that it does the trick right.

    3. ajeffreys says:

      Thanks for this, just what I was looking for :)

    4. It doesn't matter says:

      “imp?ovments”?
      My english writing skills sux, but a spelling error in the title is too much =P

    5. KIAaze says:

      Hi,
      I improved your ducks alias a little bit, so that it handles filenames with spaces correctly. :)

      alias ducks=’find . -maxdepth 1 -mindepth 1 -print0 | xargs -0 -n1 du -ks | sort -rn | head -16 | cut -f2 | xargs -i du -hs {}’

    6. KIAaze says:

      Grr, what’s wrong with quotes in the comments?

      [code]
      alias ducks=’find . -maxdepth 1 -mindepth 1 -print0 | xargs -0 -n1 du -ks | sort -rn | head -16 | cut -f2 | xargs -i du -hs {}’
      [/code]

    7. KIAaze says:

      Well, anyway, just fix the single-quotes and it should work.
      Here’s why I decided to replace “ls” with a “find” command:
      http://mywiki.wooledge.org/ParsingLs

    8. ayoli says:

      @KIAaze: thank you for your input, I’ll try your ducks improvement.

    Leave a Reply

    preload preload preload