Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I properly use two-character-width emoji in my bash prompt?

I want to use an American flag emoji in my bash prompt (i.e. PS1 environment variable). However, the American flag emoji causes the terminal cursor to offset an extra character to the right.

πŸ‡ΊπŸ‡Έ is comprised of two unicode characters, πŸ‡Ί and πŸ‡Έ. I believe terminal is converting this to a mono-spaced emoji character (the flag), yet still allocating space for two characters. How can I achieve my expected cursor position?

I want:
πŸ‡ΊπŸ‡Έ Desktop akirna πŸ—½ ls|

I get:
πŸ‡ΊπŸ‡Έ Desktop akirna πŸ—½ ls | << weird space offset before cursor

My ~/.bash_profile is:

export PS1='πŸ‡ΊπŸ‡Έ  \W \u πŸ—½ '
like image 877
Andrew Kirna Avatar asked Oct 17 '25 09:10

Andrew Kirna


1 Answers

Updated Answer

The way your are setting the prompt is not evaluating the escape characters. Add a $ before the string to make it evaluate the escape codes:

pompt$ export PS1='XY \x08: '
XY \x08: echo "Well that didn't work..."

Should become:

pompt$ export PS1=$'XY \x08: '
XY: echo "Escape code success!"

(See Charles Duffy's comment on this answer for why I removed export.)

The example above sets the prompt to the characters X, Y, [space], [backspace], [colon] resulting in a displayed prompt of just "XY:".

On my system, the flag is rendered as two characters (πŸ‡Ί and πŸ‡Έ), so I cannot verify this, but I think adding a backspace (\x08) should work for you:

PS1=$'πŸ‡ΊπŸ‡Έ  \\W \\u πŸ—½\x08'

Notes about edits

My original answer suggested using a sub-shell as follows:

export PS1=$(printf "XY \x08")

Many thanks to Charles Duffy for his input~

like image 114
Josh Avatar answered Oct 19 '25 23:10

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!