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

£可爱£侵袭症+ 提交于 2019-12-06 12:08:47

问题


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 🗽 '

回答1:


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~




回答2:


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\] '


来源:https://stackoverflow.com/questions/54451605/how-can-i-properly-use-two-character-width-emoji-in-my-bash-prompt

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