Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add unicode characters to Zsh prompt?

Tags:

zsh

zshrc

I'd like to add unicode characters to my Zsh prompt to customise the theme.

I've read articles like this one (https://scriptingosx.com/2019/07/moving-to-zsh-06-customizing-the-zsh-prompt/) that explain that you need to change the PROMPT variable, which I have tried. However, when it comes to printing Unicode characters in the prompt, the shell returns that text of the character (e.g. 'U+1F600'), and not the character graphic icon itself (šŸ˜€).

I was wondering if this is something that is possible in Zsh?

I was inspired by a prompt that looks like this:

enter image description here

like image 936
Richard Jarram Avatar asked Mar 19 '20 13:03

Richard Jarram


People also ask

How do I add a Unicode?

Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X.

How do I write Unicode in terminal?

Hold down CTRL-SHIFT-u and you will see an underlined u character that is waiting for input. Type the hexadecimal Unicode value and press space. http://unicode.org/charts/ is a full collection of charts that list all Unicode characters with their codes.

What is Prompt_subst?

If the PROMPT_SUBST option is set, the prompt string is first subjected to parameter expansion, command substitution and arithmetic expansion. See section Expansion. Certain escape sequences may be recognised in the prompt string. If the PROMPT_BANG option is set, a `!


1 Answers

Use the \U escape, which can be followed by a hexadecimal value of 1-8 digits (though Unicode as currently defined would only ever required 6 digits, as it only allows code points up to 10FFFF).

% print '\U1f600'
šŸ˜€

For a prompt, you need to use $'...' quoting to cause the escape to be expanded (the print command does that itself):

% PS1=$'%m %1~ \U1f600 %# '
myhost ~ šŸ˜€ %

As in other contexts, different kinds of quoting can be combined in a single assignment. E.g.,

PS1='%m %1~ '$'\U1f600'' %# '

is equivalent to the above.

like image 165
chepner Avatar answered Sep 29 '22 17:09

chepner