Snazzier Command Line

Posted on Tuesday, February 10, 2015


I have my Linux prompt slightly tweaked.  I use.


PS1="\h:\w> "










This gives me the server name:path then a ">" at the end.





Last night I saw a snazzier one I liked it looked like this.





(I wish I had taken a photo)

It looks like it uses colors and UTF-8 characters.    Let's see if I can replicate what he did.








Testing for UTF-8


First I want to make sure my command line can output UTF-8.  I am pretty sure it can, but I have never specifically tested it.


Run this command


    > echo -e '\xe2\x82\xac'


If you get back


You are good!





Update PS1 with UTF-8

Run this command as a test, to update PS1 with UTF-8


    > export PS1="\h: € > "





And it worked


You can enter the UTF hex code rather than €, but it’s a pain look at this site http://unix.stackexchange.com/questions/25903/awesome-symbols-and-characters-in-a-bash-prompt [2]

PS1=$'\\[\e[31m\\]\xe2\x88\xb4\\[\e[0m\\]\n\xe2\x86\x92 \xe2\x98\xbf \\~ \\[\e[31m\\]\xe2\x98\x85 $? \\[\e[0m\\]'



Seems like a pain in the neck, I am just going to paste in the Unicode character and hope for the best.








Finding Unicode Characters


I need to find which Unicode character he was using.  I found a few sites that can help you find a Unicode character.





I am using Shapecather 




Draw a shape, click recognize…






And it finds Unicode characters that are close to what you drew! 









I found this one

  \u276f


To use it I ran this command



    > export PS1="\h::\w❯❯❯"






Looks pretty close!

I just need to add some color







Colors


I have found a few sites that talk show some very complex examples


Some more basic sites showing how to set color http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html [8]

I think it's easier to show how to do this by example



    > export PS1="\[\033[34m\]\h::w❯❯❯"


Makes it blue











The blue comes from


    \[\033[34m\]



Non-printing escape sequences have to be enclosed in \[\033[  and \]

In this case 34m is the color

Some other colors

Color
Code
Blue
34
Red
31
Green
32
White
1;37
Yellow
1;33
Light Gray
37
Light Red
1;31





If you want to have more than one color, place the new color at the point where you want the color to be changed.

For example..



    > export PS1="\[\033[1;31m\]\h::\w\[\033[34m\]❯❯❯"


Gives me








Here is what I came up with



    > export PS1="\[\033[1;37m\]\[\033[41m\][\h]\[\033[30m\]\[\033[0m\]\w \[\033[1;32m\]\[\033[1;33m\]\[\033[1;31m\]\[\033[30m\]"


And here is the result





That red seems a little harsh how about black



    > export PS1="\[\033[1;37m\]\[\033[40m\][\h]\[\033[30m\]\[\033[0m\]\w \[\033[1;32m\]\[\033[1;33m\]\[\033[1;31m\]\[\033[30m\]"









One last tweak


I am almost there but there is one more tweak I would like to do.  I saw this example


    > export PS1="\n\[\e[1;30m\][$$:$PPID - \j:\!\[\e[1;30m\]]\[\e[0;36m\] \T \[\e[1;30m\][\[\e[1;34m\]\u@\H\[\e[1;30m\]:\[\e[0;37m\]${SSH_TTY:-o} \[\e[0;32m\]+${SHLVL}\[\e[1;30m\]] \[\e[1;37m\]\w\[\e[0;37m\] \n\$ "



Which results in this




A new line after the bit of information displayed.



I like to display the full path of the location where I am at, which often results in headaches like this




Making entering commands a pain of course I can use \ at the end to get back but still it’s a pain.  Why not add the carriage return automatically?


    > export PS1="\[\033[1;37m\]\[\033[40m\][\h]\[\033[30m\]\[\033[0m\]\w  \n \[\033[1;32m\]\[\033[1;33m\]\[\033[1;31m\]\[\033[30m\]"





I think I can live with that!  Wait username might be nice to have..
I added \u@ and changed \h to \H.   \h is the hostname up the first "." found in the hostname and \H is the full hostname.


    > export PS1="\[\033[1;37m\]\[\033[40m\][\u@\H]\[\033[30m\]\[\033[0m\]\w  \n \[\033[1;32m\]\[\033[1;33m\]\[\033[1;31m\]\[\033[30m\]"




There we go I think I like that!


Now to update my .profile file to make sure it automatically loads each time.
Or for my own servers I set it in /etc/bash.bashrc so all users would get it.
I also updated roots own .bashrc file so now its easier to see that I am room.




Custom command prompts are very useful and eye catching when doing a presentation!







One last thing


Grrr stupid windows!

I tend to shift between OS X, Windows, and Linux several times a day.
Windows is only tolerable because of Cygwin.

When I try to adjust my command line on Cygwin I get the following.




The font I am using is Lucida Sans Typewriter




This font, on my windows 7 box, does not support this Unicode character.



Because of the color and black background I tweaked my command line setup once again.  For Cygwin I came up with this


    > export PS1="\[\033[1;30m\]\[\033[42m\][\u@\H]\[\033[30m\]\[\033[0m\]\w  \n \[\033[1;32m\]\[\033[1;33m\]\[\033[1;31m\]\[\033[1;37m\]"


(You may need to change the characters to the correct Unicode one, I am not sure how this post will show up on the web.




That gives me the look I want, but not the right Unicode characters showing up. 





The Font Fix


I need to fix the font to get this working the way I want it to.


Fixing the font is a pain check out this post I made on how to do it at http://www.whiteboardcoder.com/2015/02/fontforge.html

Or you can see the video I made at http://youtu.be/eDPC1e546wg



Once you fix the font

Bam it works now!







References

[1]        How to find out if a terminal supports UTF-8
                Accessed 01/2015
[2]        Awesome symbols and characters in a bash prompt
                Accessed 01/2015
[3]        Welcome to the Unicode Symbol Map
            http://panmental.de/symbols/info.htm
            Accessed 01/2015
[4]        Shapecatcher
           http://shapecatcher.com/
            Accessed 01/2015
[5]        Unicode Emoticons
           http://unicodeemoticons.com/
            Accessed 01/2015
[6]        Color Bash Prompt
            Accessed 01/2015
[7]        Crazy powerful bash prompt
            Accessed 01/2015
[8]        Colours
            Accessed 01/2015




1 comment:

  1. The best windows command font is BY FAR: Consolas. On nix my favorite is Terminus :)

    ReplyDelete