Page 1 of 2

Modifying the konsole prompt

Posted: 04 May 2010, 01:05
by viking60
In the terminal or konsole I have often wanted to change the look and function. There is color prompt etc.. but it is fairly limited.
Here is a way:
in your home directory:

Code: Select all

nano .bashrc

This will pop .bashrc with a content that could looke like this:

Code: Select all

# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
   . /etc/bashrc
fi


or PSI= etc etc.

Now comment out everything in the file with # so it looks like this:

Code: Select all

# .bashrc

# User specific aliases and functions

# Source global definitions
#if [ -f /etc/bashrc ]; then
#   . /etc/bashrc
#fi


Now copy this in to the file below the lines you commented out:

Code: Select all

bash_prompt_cmd() {
RTN=$?



smiley() {
    if [ $1 == 0 ] ; then
echo ":)"
    else
echo ":("
       fi
    }
smileyc() {
    if [ $1 == 0 ] ; then
echo $GREEN
else
echo $RED
       fi
    }
 if [ $(tput colors) -gt 0 ] ; then
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
RST=$(tput op)
fi
smiley=$(smiley $RTN)
smileyc=$(smileyc $RTN)
        local CY="\[\e[1;31m\]" # Each is 12 chars long
        local BL="\[\e[1;34m\]"
        local WH="\[\e[1;37m\]"
        local BR="\[\e[0;33m\]"
        local RE="\[\e[1;31m\]"
        local PROMPT="${CY}$"
        [ $UID -eq "0" ] && PROMPT="${RE}#"

        # Add the first part of the prompt: username,host, and time
        local PROMPT_PWD=""
        local PS1_T1="$BL.:[ $CY`whoami`@`hostname` $BL: $CY\t $BL:$CY "
        local ps_len=$(( ${#PS1_T1} - 12 * 6 + 6 + 4 )) #Len adjust for colors, time and var
        local PS1_T2=" $BL]:.\n\[\$smileyc\]\$smiley\[$RST\] "
        local startpos=""

        PROMPT_PWD="${PWD/#$HOME/~}"
        local overflow_prefix="..."
        local pwdlen=${#PROMPT_PWD}
        local maxpwdlen=$(( COLUMNS - ps_len ))
        # Sometimes COLUMNS isn't initiliased, if it isn't, fall back on 80
        [ $maxpwdlen -lt 0 ] && maxpwdlen=$(( 80 - ps_len ))

        if [ $pwdlen -gt $maxpwdlen ] ; then
                startpos=$(( $pwdlen - maxpwdlen + ${#overflow_prefix} ))
                PROMPT_PWD="${overflow_prefix}${PROMPT_PWD:$startpos:$maxpwdlen}"
        fi
        export PS1="${PS1_T1}${PROMPT_PWD}${PS1_T2}"
smiley=$(smiley $RTN)
smileyc=$(smileyc $RTN)
        local CY="\[\e[1;31m\]" # Each is 12 chars long
        local BL="\[\e[1;34m\]"
        local WH="\[\e[1;37m\]"
        local BR="\[\e[0;33m\]"
        local RE="\[\e[1;31m\]"
        local PROMPT="${CY}$"
        [ $UID -eq "0" ] && PROMPT="${RE}#"

        # Add the first part of the prompt: username,host, and time
        local PROMPT_PWD=""
        local PS1_T1="$BL.:[ $CY`whoami`@`hostname` $BL: $CY\t $BL:$CY "
        local ps_len=$(( ${#PS1_T1} - 12 * 6 + 6 + 4 )) #Len adjust for colors, time and var
        local PS1_T2=" $BL]:.\n\[\$smileyc\]\$smiley\[$RST\] "
        local startpos=""

        PROMPT_PWD="${PWD/#$HOME/~}"
        local overflow_prefix="..."
        local pwdlen=${#PROMPT_PWD}
        local maxpwdlen=$(( COLUMNS - ps_len ))
        # Sometimes COLUMNS isn't initiliased, if it isn't, fall back on 80
        [ $maxpwdlen -lt 0 ] && maxpwdlen=$(( 80 - ps_len ))

        if [ $pwdlen -gt $maxpwdlen ] ; then
                startpos=$(( $pwdlen - maxpwdlen + ${#overflow_prefix} ))
                PROMPT_PWD="${overflow_prefix}${PROMPT_PWD:$startpos:$maxpwdlen}"
        fi
        export PS1="${PS1_T1}${PROMPT_PWD}${PS1_T2}"
}
PROMPT_COMMAND=bash_prompt_cmd

This is my favorite. It gives a nice and clean overview where you are in your navigation and it has a smiley system. Green smile for the right code, and if you enter the wrong code you get a red smiley.

I have tested it both in Arch (Installed cope-git first) and Mandriva.
It looks like this:

If you want the same (or a different) effect on the root prompt; just create .bashrc in /root and put the content there.
If you like it let me know. Or post your favourite!
(PS this is not going to look great in Mandriva as root user)
Good luck.

Re: Modifying the console prompt

Posted: 22 Oct 2010, 02:34
by dedanna1029
viking60 wrote:(Installed cope-git first)

Code: Select all

# pacman -S cope-git
-->error: 'cope-git': could not find or read package



This is as root:

Code: Select all

# ls
Desktop  Downloads  Xorg.0.log.txt  depmod.txt   dmesg2.txt  xorg.conf.new


Code: Select all

# nano .bashrc

(no response, as it's not there).

Re: Modifying the console prompt

Posted: 22 Oct 2010, 02:59
by viking60
Oh sorry it is in aur :oops: So you will have to use yaourt cope-git. (For instructions on installing yaourt look elsewhere in this forum or better: Find it in the Arch wiki and follow the instructions)

Re: Modifying the console prompt

Posted: 22 Oct 2010, 03:01
by dedanna1029
I already know how to use AUR and yaourt, thanks. That will be good for others.

Re: Modifying the console prompt

Posted: 22 Oct 2010, 03:04
by viking60
dedanna1029 wrote: That will be good for others.

That is the whole idea - kind of :D

Re: Modifying the console prompt

Posted: 22 Oct 2010, 03:08
by dedanna1029
Now when I pull up a terminal, this comes up with it:

Code: Select all

bash: /home/dedanna/.bashrc: line 47: syntax error near unexpected token `time'
bash: /home/dedanna/.bashrc: line 47: `for colors, time and var'

Re: Modifying the console prompt

Posted: 22 Oct 2010, 04:17
by dedanna1029
It is still doing it, even after the cope-git install.

Code: Select all

bash: /home/dedanna/.bashrc: line 47: syntax error near unexpected token `time'
bash: /home/dedanna/.bashrc: line 47: `for colors, time and var'


Having packaged and come across many syntax errors, I have learned that when it says there is one, there is one. I think the code for .bashrc needs some work - I will get to it when I can. For now, I'm outta here, and off to sleep.

Re: Modifying the console prompt

Posted: 22 Oct 2010, 20:11
by dedanna1029
I have fixed the script. Line #67 was the issue; "#Len adjust for colors, time and var" should have been brought down to the next line, and I have done so, inserted it into .bashrc, and am now able to run terminal correctly with this:

Code: Select all

bash_prompt_cmd() {
RTN=$?



smiley() {
    if [ $1 == 0 ] ; then
echo ":)"
    else
echo ":("
       fi
    }
smileyc() {
    if [ $1 == 0 ] ; then
echo $GREEN
else
echo $RED
       fi
    }
if [ $(tput colors) -gt 0 ] ; then
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
RST=$(tput op)
fi
smiley=$(smiley $RTN)
smileyc=$(smileyc $RTN)
        local CY="\[\e[1;31m\]" # Each is 12 chars long
        local BL="\[\e[1;34m\]"
        local WH="\[\e[1;37m\]"
        local BR="\[\e[0;33m\]"
        local RE="\[\e[1;31m\]"
        local PROMPT="${CY}$"
        [ $UID -eq "0" ] && PROMPT="${RE}#"

        # Add the first part of the prompt: username,host, and time
        local PROMPT_PWD=""
        local PS1_T1="$BL.:[ $CY`whoami`@`hostname` $BL: $CY\t $BL:$CY "
        local ps_len=$(( ${#PS1_T1} - 12 * 6 + 6 + 4 )) #Len adjust for colors, time and var
        local PS1_T2=" $BL]:.\n\[\$smileyc\]\$smiley\[$RST\] "
        local startpos=""

        PROMPT_PWD="${PWD/#$HOME/~}"
        local overflow_prefix="..."
        local pwdlen=${#PROMPT_PWD}
        local maxpwdlen=$(( COLUMNS - ps_len ))
        # Sometimes COLUMNS isn't initiliased, if it isn't, fall back on 80
        [ $maxpwdlen -lt 0 ] && maxpwdlen=$(( 80 - ps_len ))

        if [ $pwdlen -gt $maxpwdlen ] ; then
                startpos=$(( $pwdlen - maxpwdlen + ${#overflow_prefix} ))
                PROMPT_PWD="${overflow_prefix}${PROMPT_PWD:$startpos:$maxpwdlen}"
        fi
        export PS1="${PS1_T1}${PROMPT_PWD}${PS1_T2}"
smiley=$(smiley $RTN)
smileyc=$(smileyc $RTN)
        local CY="\[\e[1;31m\]" # Each is 12 chars long
        local BL="\[\e[1;34m\]"
        local WH="\[\e[1;37m\]"
        local BR="\[\e[0;33m\]"
        local RE="\[\e[1;31m\]"
        local PROMPT="${CY}$"
        [ $UID -eq "0" ] && PROMPT="${RE}#"

        # Add the first part of the prompt: username,host, and time
        local PROMPT_PWD=""
        local PS1_T1="$BL.:[ $CY`whoami`@`hostname` $BL: $CY\t $BL:$CY "
        local ps_len=$(( ${#PS1_T1} - 12 * 6 + 6 + 4 ))
   #Len adjust for colors, time and var
        local PS1_T2=" $BL]:.\n\[\$smileyc\]\$smiley\[$RST\] "
        local startpos=""

        PROMPT_PWD="${PWD/#$HOME/~}"
        local overflow_prefix="..."
        local pwdlen=${#PROMPT_PWD}
        local maxpwdlen=$(( COLUMNS - ps_len ))
        # Sometimes COLUMNS isn't initiliased, if it isn't, fall back on 80
        [ $maxpwdlen -lt 0 ] && maxpwdlen=$(( 80 - ps_len ))

        if [ $pwdlen -gt $maxpwdlen ] ; then
                startpos=$(( $pwdlen - maxpwdlen + ${#overflow_prefix} ))
                PROMPT_PWD="${overflow_prefix}${PROMPT_PWD:$startpos:$maxpwdlen}"
        fi
        export PS1="${PS1_T1}${PROMPT_PWD}${PS1_T2}"
}
PROMPT_COMMAND=bash_prompt_cmd


Which produces:

Code: Select all

.:[ dedanna@dedanna.rocks.net : 13:07:32 : ~ ]:.
:)

It's just unfortunate that colors can't be shown in the forum's code tags.

:A :arrogant:

Now, if I could just get the syntax right for gtkpacman to be fixed, I'd do real good. Have been unable to find the maintainer's page to report the bug on it.

Re: Modifying the console prompt

Posted: 22 Oct 2010, 21:56
by viking60
Yeah I love that smiley. I use it too!

Re: Modifying the console prompt

Posted: 24 Oct 2010, 15:53
by dedanna1029
It even does it in tty if one is logged into it as their own user. Haven't implemented this for root, so it doesn't do it there. Try it. Hit Ctrl+Alt+F2 ... log in as your user.

Re: Modifying the console prompt

Posted: 03 Jul 2011, 22:18
by dedanna1029
Debating on changing red to purple. Now that, would be cool. My two fav colors. :)

Re: Modifying the console prompt

Posted: 13 Apr 2012, 19:45
by dedanna1029
dedanna1029 wrote:Debating on changing red to purple. Now that, would be cool. My two fav colors. :)

Giving it a shot now in Linux Mint 12.
The issue being that of course, there's no cope-git in the repos. :(

I'm not sure, but I don't think Linux Mint needed the cope-git?

Code: Select all

:( echo "My name is Ded's Tilda"
My name is Ded's Tilda
.:[ dedanna@dedanna : 13:24:48 : ~/Documents ]:.
:) echo "See my pretty prompt"
See my pretty prompt
:) echo $PROMPT
.:[ dedanna@dedanna : 13:25:17 : ~/Documents ]:.