| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Monday, October 19, 2009

Slick BASH Prompt

UPDATED! 30 October 2009 - I found out how to stop the annoying line wrapping bugs where input wouldn't wrap properly and would over-write part of the prompt if you deleted the input. The code below has been updated.

I had seen some really awesome BASH prompts a few months ago and decided it was time to buckle down and "pimp my prompt". I have to say that I'm VERY happy with how it turned out!
The horizontal line between the directory and the 24hr clock on the right edge of the screen will dynamically resize depending on the width of the terminal. If you'd like to incorporate this into your BASH prompt, it's as easy as this:

  1. gedit ~/.bashrc

  2. comment out the lines that start with "PS1="
    from

    PS1=blahblahblah

    to

    #PS1=blahblahblah

  3. Copy/paste the code below into your .bashrc
    hostn=$(hostname -s)
    usern=$(whoami)

    #black="\e[0;30m"
    #lgreen="\e[32;1m"
    #white="\e[1;37m"
    #lgray="\e[1;30m"
    #lgray="\e[1;32m"
    #dgray="\e[1;30m"
    #uwhite="\e[4;37;1m"
    #uwhite="\e[37m"
    #ublack="\e[4;30m"

    userc="\[\e[1;30m\]"
    timec="\[\e[1;30m\]"
    typec="\[\e[1;30m\]"
    dirc="\[\e[1;30m\]"
    nc="\[\e[0m\]"

    function prompt {

    ctime=$(date +%R)
    prmpt="-[${usern}@${hostn}][${PWD}][${ctime}]"
    cols=$(tput cols)
    let FILLS=${cols}-${#prmpt}
    LINE=""

    if [[ "$PWD" =~ "/home/$usern" ]]
    then
    let FILLS=$FILLS+5+${#usern}
    fi

    for (( f=0; f<$FILLS; f++ )) do LINE=$LINE"\e(0q\e(B" done PS1="$nc\[\e(0l\e(B\]<$userc\u$nc@$userc\h$nc><$dirc\w$nc>${LINE}<$timec${ctime}$nc>\n"
    PS1=$PS1"\[\e(0\]m\[\e(B\]<$typec\$$nc>\[\e(0\]q\[\e(B\]<" } PROMPT_COMMAND=prompt


TIP: If you hack this up, try to put as much of the code OUTSIDE the function {} as possible to reduce the time it takes your terminal to render the prompt.

1 comments:

X4lldux said...

nice post. it helped me to set prompt to what I wanted.