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

Community
  • 1
  • 1
0x131
  • 99
  • 1
  • 2
  • FWIW, "all RGB colors" is not a thing; while the palete may be able to be changed it's still a fixed set: http://www.xfree86.org/current/ctlseqs.html – user2864740 Jan 20 '17 at 23:45
  • That is 88-no-million or 256 color palettes. Standard [True Color](https://en.wikipedia.org/wiki/Color_depth#True_color_.2824-bit.29) 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. – user2864740 Jan 21 '17 at 01:00
  • 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. – user2864740 Jan 21 '17 at 01:05
  • Oh, cute. Here are some terminals that do support True Color. This is beyond VT- - https://deductivelabs.com/en/2016/03/using-true-color-vim-tmux/ ; make sure Konsole is on that list. – user2864740 Jan 21 '17 at 01:07
  • 1
    @user2864740 Konsole is on the list, I already checked here: https://gist.github.com/XVilka/8346728 – 0x131 Jan 21 '17 at 02:07
  • Note that interactive shell customization is generally a better topic for [unix.se] than Stack Overflow; the latter is narrowly focused on writing software. – Charles Duffy Jul 30 '23 at 12:55

3 Answers3

6

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.

sbdchd
  • 580
  • 1
  • 7
  • 18
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 the .zshrc file located at ~/.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.

V.G.
  • 51
  • 3
0

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

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

(You have to imagine the appropriate color, as I can't reproduce it using text alone.)

chepner
  • 497,756
  • 71
  • 530
  • 681