6

I know you can define multiline prompts in zsh:

PROMPT='
test
→ '

But I was wondering, why can't you define multiline right prompts? I tried:

RPROMPT='
test
still test '

but nothing shows up. Is this possible?

10

You can get the desired (if I guess right) effect by using the precmd function, which is executed every time before the prompt is displayed, to print additional lines above the prompt.

Use for example this code

precmd() {
  LEFT="The time is"
  RIGHT="$(date) "
  RIGHTWIDTH=$(($COLUMNS-${#LEFT}))
  print $LEFT${(l:$RIGHTWIDTH::.:)RIGHT}
}
PS1="foo > "
RPS1="bar"

Explanation

  • $LEFT and $RIGHT hold the strings for the left and right side, resp. You can use the output of command with the $(...) syntax.
  • $RIGHTWIDTH is the difference of the current terminal width (stored by zsh in the $COLUMNS parameter) and the width of the left string. $((...)) is for mathematical expressions
  • The magic comes with ${(l:$RIGHTWIDTH::.:)RIGHT}, where the parameter $RIGHT is left-padded (l) by dots (omit the last :.: if you want spaces) to the length of $RIGHTWIDTH.
  • Print the additional line. Repeat the commands if you want more lines.
  • PS1 and RPS1 are shorter for PROMPT and RPROMPT.

Demo

The time is............................................Fri, Sep 18, 2015  8:20:22 PM 
foo > _                                                                          bar
  • 3
    Great solution, but one has to be careful if LEFT or RIGHT contains ANSI escape sequences (e.g. colors). To compute string length ignoring ansi escapes, we can do it as $(( ${#${(S%%)str//(\%([KF1]|)\{*\}|\%[Bbkf])}} )) – Jongwook Choi Jan 22 '17 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.