15

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

Richard Jarram
  • 899
  • 11
  • 22

1 Answers1

22

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.

chepner
  • 497,756
  • 71
  • 530
  • 681