Modifying the konsole prompt

Every day solutions to every day challenges. + Brilliant stuff

Moderators: b1o, jkerr82508

Forum rules
Please feel free to post your tip it does not have to be advanced. Also ask questions directly related to the tip here. But do not start new threads with questions or ask for help here. That is what the help section is for. forum rules: http://bjoernvold.com/forum/viewtopic.php?f=8&t=568
User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Modifying the konsole prompt

Postby viking60 » 04 May 2010, 01:05

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.
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Modifying the console prompt

Postby dedanna1029 » 22 Oct 2010, 02:34

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).
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Modifying the console prompt

Postby viking60 » 22 Oct 2010, 02:59

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)
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Modifying the console prompt

Postby dedanna1029 » 22 Oct 2010, 03:01

I already know how to use AUR and yaourt, thanks. That will be good for others.
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Modifying the console prompt

Postby viking60 » 22 Oct 2010, 03:04

dedanna1029 wrote: That will be good for others.

That is the whole idea - kind of :D
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Modifying the console prompt

Postby dedanna1029 » 22 Oct 2010, 03:08

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'
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Modifying the console prompt

Postby dedanna1029 » 22 Oct 2010, 04:17

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.
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Modifying the console prompt

Postby dedanna1029 » 22 Oct 2010, 20:11

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.
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Modifying the console prompt

Postby viking60 » 22 Oct 2010, 21:56

Yeah I love that smiley. I use it too!
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Modifying the console prompt

Postby dedanna1029 » 24 Oct 2010, 15:53

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.
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Modifying the console prompt

Postby dedanna1029 » 03 Jul 2011, 22:18

Debating on changing red to purple. Now that, would be cool. My two fav colors. :)
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
dedanna1029
Sound-Berserk
Posts: 8780
Joined: 14 Mar 2010, 20:29
Contact:

Re: Modifying the console prompt

Postby dedanna1029 » 13 Apr 2012, 19:45

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 ]:.
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html


Return to “Tips & Tricks”