Multiline RPROMPT in zsh
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?
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
andRPS1
are shorter forPROMPT
andRPROMPT
.
Demo
The time is............................................Fri, Sep 18, 2015 8:20:22 PM
foo > _ bar