Andrew Kirna
Andrew Kirna

Reputation: 1396

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 πŸ—½ '

Upvotes: 4

Views: 3415

Answers (2)

Josh
Josh

Reputation: 51

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~

Upvotes: 2

mcfedr
mcfedr

Reputation: 7975

I worked around this by converting the character to hex, and then putting zero width markers around the second part of the character

so for πŸ‡ΊπŸ‡Έ we get

PS1='\xf0\x9f\x87\xba\[\xf0\x9f\x87\xb8\] '

Upvotes: 0

Related Questions