4

I have the following configuration for Bash:

#   Change Prompt
#   ------------------------------------------------------------
    export PS1="________________________________________________________________________________\n| \e[0;34m\u\e[m@\e[0;32m\h\e[m {\e[0;33m\w\e[m} --- \e[0;35m\D{%F %T}\e[m \n| $: "
    export PS2="|  $: "

I tried replacing the \ with %, but it didn't work. What can I do to make this work in zsh?

Essentially this ends up looking like the following (minus colors):

________________________________________________________________________________
| user1@MacBook-Pro {~} --- 2020-09-14 13:17:39
| $:
3
  • Did you look at the zsh man page? Are you struggling with the prompt in general or mainly with the encoding of escape sequences?
    – nohillside
    Commented Sep 14, 2020 at 18:44
  • mainly with the escapes, I think, I can't figure out how to customize the colors
    – ctote
    Commented Sep 14, 2020 at 22:00
  • Unfortunately I don’t know the answer, but if you don’t find it here, the Zsh Users’ list is full of super-helpful folks. I’ve been on it since the 90s! You can find out more here too: zsh.sourceforge.net/Arc/mlist.html
    – TJ Luoma
    Commented Sep 15, 2020 at 3:45

2 Answers 2

7

As others have already mentioned, there are built-in sequences for setting colours, like %F{red}. If you want to include more exotic terminal control codes, you can do it like this:

PS1=$'%{\e[1m%}%m%{\e[0m%}'

(This should make the prompt display your hostname in boldface, on most typical terminals.) There are two things to notice here

  1. Using the $'... ' quoting mechanism to include control characters in the string. This is described under QUOTING in the zshmisc(1) man page. (In other words, this is what makes \e work).

  2. Using %{...%} to indicate that the enclosed sequence does not cause the cursor to move.

  3. (%m expands to the host name. This is described under PROMPT SEQUENCES in zshmisc(1), but you probably knew this already.)

(Alright then, three things. Monty Python fans will know what reference I would put here, others won't care. :-) )

2

From https://scriptingosx.com/2019/07/moving-to-zsh-06-customizing-the-zsh-prompt/

Adding a bit of color or shades of gray to the prompt can make it more readable. In bash you need cryptic escape codes to switch the colors. zsh provides an easier way. To turn the directory in the path blue, you can use:

PROMPT='%F{blue}%1~%f %# '

The F stands for ‘Foreground color.’ zsh understands the colors black, red, green, yellow, blue, magenta, cyan and white. %F or %f resets to the default text color.

There are more details about this on the linked page (and in man zsh).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .