Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use escape sequences in a ZSH prompt for truecolor or bold?

I am in the middle of customizing my ZSH prompt but am seemingly unable to use escape sequences to tell Konsole to use bold text or a specific RGB color.

I know about the built in formatting options in ZSH, like %F{000} %f, but as far as I know, those options only allow access to the defaults(red, blue, etc) and the 256 color palette. While %B %b, the built-in option for bold, does work, it seems limited to just one color.

What I want to be able to do is color a specific section of the prompt using all RGB colors and/or make it bold. From what I could find, something like this should work:

PS1="%{\e[38;0;255;0;255m%}%M >:%{\e[0m%}"

That should give me a pink prompt like this:

HOSTNAME >:                  

But what I get is this:

\e[38;0;255;0;255mHOSTNAME >:\e[0m

I have tried different escape sequences like \033 \x1b, but nothing seems to work.

So, how do I properly use escape sequences in ZSH prompts?



Specifics:

OpenSUSE Tumbleweed KDE

Konsole --version 16.12.0 (Keyboard:XFree 4)

ZSH --version 5.3

like image 399
0x131 Avatar asked Jan 20 '17 23:01

0x131


1 Answers

You need to change your strings so that zsh evaluates them correctly.

Try changing:

PS1="%{\e[38;0;255;0;255m%}%M >:%{\e[0m%}"

To:

PS1=$'%{\e[38;0;255;0;255m%}%M >:%{\e[0m%}'

Notice the change from " to ' quotes along with the prepended $

See http://zsh.sourceforge.net/Guide/zshguide05.html for more info on substitutions.

like image 180
sbdchd Avatar answered Sep 30 '22 03:09

sbdchd