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

南笙酒味 提交于 2019-12-04 15:29:37

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~

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\] '
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!