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

3

9

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

Folk answered 20/1, 2017 at 23:33 Comment(7)
FWIW, "all RGB colors" is not a thing; while the palete may be able to be changed it's still a fixed set: xfree86.org/current/ctlseqs.htmlPentavalent
That is 88-no-million or 256 color palettes. Standard True Color is only 24bits or ~16 million colors and expressed as a composition of values. Terminals (any of the VT- family, anyway) require indexing into a fixed color palette table.Pentavalent
I still think it's a fair question - just keep in mind that accessing a color by RGB (without updating the palette itself) is probably not possible.Pentavalent
Oh, cute. Here are some terminals that do support True Color. This is beyond VT- - deductivelabs.com/en/2016/03/using-true-color-vim-tmux ; make sure Konsole is on that list.Pentavalent
@Pentavalent Konsole is on the list, I already checked here: gist.github.com/XVilka/8346728Folk
Note that interactive shell customization is generally a better topic for Unix & Linux than Stack Overflow; the latter is narrowly focused on writing software.Unquestioned
Please consider accepting @chepner's answer for this question. It's the most correct, avoids creating brittle control code strings, and nearest colors are used on non-truetype terminals, if you ever find yourself in that increasingly unlikely situation.Foucquet
W
5

You can specify arbitrary 24-bit colors with %F using an RGB triplet.

% print -P "%F{#009090}tealish"
tealish

result of above code

Womanish answered 1/8, 2023 at 16:36 Comment(3)
Finally something that works! The \e[38m syntax wasn't working for me on Cygwin zsh 5.8Dumas
\e[38m is an example of an ANSI terminal escape sequence, and as such only works on an ANSI terminal. zsh's own escapes use whatever is appropriate for the terminal you are actually using.Womanish
To be fair, since hex colors in the form "%F{#000000}" have been available in zsh for 6+ years, at least (as of now) this should be the top answer. Not only are you saved from terminal control codes. Zsh also gets the nearest color for the current terminal display, if it's not truecolor. In other words it's not only familiar syntax, it's also just better and more compatible.Foucquet
S
4

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.

Scotopia answered 9/3, 2017 at 22:2 Comment(0)
N
1

I might be a little late for this, but on ZSH, your answer will be:

PS1="%F{green}%M >:%f"

Your original code used the ANSI escape sequences for colour formatting, which might not work correctly in all Zsh terminals. This updated code uses the Zsh-specific prompt escape sequences (%F{color_code} and %K{color_code}) to set the Foreground and bacKground colours, respectively.

To apply this you must set it in ~/.zshrc.

Run this to set it automatically (it will not override any existing settings there):

touch ~/.zshrc && echo '\nPS1="%F{green}%M >:%f"' >> ~/.zshrc && echo "Success";

Here is a StackOverflow question that answers how the colours work in zsh.

Nasty answered 29/7, 2023 at 20:33 Comment(1)
OP already knew about %F, etc.Loeb

© 2022 - 2024 — McMap. All rights reserved.