How can you customize your terminal bash prompt with smiley faces?

11

6

I'm trying to figure out how I can customize my terminal's bash prompt to use smiley faces. What I want (as seen in the example blow) is for the cwd to be separated from the prompt by a \n and show a green smiley face if the command succeeded, and a red sad face if it failed.

Any ideas?

This was inspired by a Peepcode screencast.

Example

Josh Smith

Posted 2011-12-16T02:07:49.767

Reputation: 351

Answers

14

After spending about a half hour playing around with andhrimnir's code and doing further research, I finally got what I wanted.

PS1="\w \`if [ \$? = 0 ]; then echo -e '\[\e[01;32m\]\n\xE2\x98\xBA'; else echo -e '\[\e[01;31m\]\n\xE2\x98\xB9'; fi\` \[\e[01;34m\]\[\e[00m\]"

You can find a list of emoticons here and then convert them to the 3-digit byte code you see after the newline character.

To get the cwd, all I had to do was use \w. You could also show the current user by doing \u@\w, which would output something like joshsmith@~.

Josh Smith

Posted 2011-12-16T02:07:49.767

Reputation: 351

6

It appears that the smiley face shown above is unicode character 0x263a. So you'll need a unicode-capable terminal (Not sure if terminal.app supports this, I imagine it does though).

Here's sample code that prints a green smiley face for return codes of 0 and red frown faces otherwise.

PS1="\[\e[01;32m\]\u@\h \[\e[01;34m\]\W \`if [ \$? = 0 ]; then echo -e '\[\e[01;32m\]:)'; else echo -e '\[\e[01;31m\]:('; fi\` \[\e[01;34m\]$\[\e[00m\]"

Credit goes to Fingel on the Arch forums (he posted it here).

jake-low

Posted 2011-12-16T02:07:49.767

Reputation: 372

Awesome work on the if/then sample. I'm curious how to do the newline, remove the $, actually use the Unicode symbol, and make it show the full cwd. – Josh Smith – 2011-12-16T02:27:06.850

...and an hour later, answered my own question (thanks to you!). – Josh Smith – 2011-12-16T03:26:15.677

1Glad I could help! There's a lot of info in the thread at the link I posted above if you want to get deeper into custom $PS1 stuff. – jake-low – 2011-12-16T04:24:10.013

Yeah, half of my research started at that thread. Super helpful. Also, for anyone that wants to go more in-depth on the command line, Peepcode has a great advanced screencast. – Josh Smith – 2011-12-21T18:14:12.887