New terminal prompt

2010/06/22 at 7:58 pm (2010, Linux, Planet Gentoo) (, , )

When I was preparing to use Cucumber I watched a screen cast. I saw a cute promt there: it was single-char green happy face if last command was successful (returned 0) or single-char red sad face if it wasn’t. This motivated me to change my own prompt which was default prompt for Gentoo:

login@hostname /full/path/to/current/dir $

It wasn’t bad but it could get really long if I was deep in directory structure and I really know my login and host… My current promt is

current_dir HH:MM :)

If last command was succesful and

current_dir HH:MM :(

if it failed. I changed prompt for just my user by adding to ~/.bashrc

export PS1="\[\e[34m\]\W \[\e[0m\]\`if [ \$? = 0 ]; then
echo \[\e[32m\]\A \:\)\[\e[0m\];
else
echo \[\e[31m\]\A \:\(\[\e[0m\]; fi\` "

And a screenshot so you can compare how much space I’m saving when I’m in some deep directory:

Advertisements

4 Comments

  1. ahenobarbi said,

    My friend asked me if a question:
    Your promt calls “echo”, doesn’t it overwrite “$?” ?

    In case anyone worries: it doesn’t. I checked.

  2. kristjan said,

    Mine is similar, but adds a nose (-) when you have background processes (e.g. firefox &):
    kristjan@vstr ~ :)
    vs.
    kristjan@vstr ~ :-)

    The code for it isn’t very pretty though:

    prompt_command()
    {
    prev_exit=$?
    local JOBS=`jobs`
    PS1=”\[33[01;32m\]\u@\h\[33[01;34m\] \w :`if [[ “” != “$JOBS” ]]; then echo -ne “-“; fi; if [[ $prev_exit != 0 ]]; then echo “(“; else echo “)”; fi`\[33[00m\] ”
    }
    if [[ $- = *i* ]]; then
    PROMPT_COMMAND=”prompt_command; history -a;”
    else
    PS1=”\[33[01;32m\]\u@\h\[33[01;34m\] \w \$\[33[00m\] ”
    fi

    in /etc/bash/bashrc

  3. SuperFlo said,

    You should definitely use PROMPT_COMMAND to call a function of your own instead of using if/else and echoes in an export.

    My prompt outputs nothing if the previous command returned 0, and outputs the status code in bold red when the command fails, I use it since a few years, it’s funny to notice cool error codes such as 42 :)

  4. Gentoo: My bash prompt | Jeremy's Weblog said,

    […] seems to be a semi-meme going around on some of the planets I […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: