Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like a multi-line zsh prompt with a right alined part, that will look something like this:

2.nate@host:/current/dir                                               16:00

I know about RPROMPT in zsh, but that has a right-aligned prompt opposite your normal prompt, which is on the same line of text as your typing.

Is there a way to have a right-aligned portion to the first line of a multi-line command prompt? I'm looking for either a directive in the PS1 variable that says 'right align now' or a variable that is to PS1 what RPROMPT is to PROMPT.


share|improve this question
up vote 12 down vote accepted

You will find a detailed answer and an example here. The idea is to write the line before PS1 using the precmd callback, use $COLUMNS, and a bit of math to calculate the position of the text on the right side of the screen. Knowledge of escape sequences will also help you with cursor positioning and colouring.

Another solution can be to use a theme from Oh My ZSH.

share|improve this answer

I've been looking for this too. For me, the fact that precmd() drawn lines don't redraw on resize or when ^L is used to clear the screen was something that kept itching at me. What I'm doing now is using ANSI escape sequences to move the cursor around a bit. Though I suspect there is a more elegant way to issue them, this is working for me:



Keep in mind that the zsh manual states that %{...%} is for literal escape sequences that don't move the cursor. Even so, I'm using them because they allow to ignore the length of it's content (couldn't figure out how to issue the escape that moves the cursor using them though)

share|improve this answer
Thank you! This is exactly what I was looking for. – mpen Apr 24 '14 at 17:29
After playing around with this for some time, it messes up occasionally and puts the cursor or date on the wrong line. Not such a big deal though; can just press enter to correct it. – mpen Apr 2 '15 at 18:17

Here is how I've configured this thing just now. This approach doesn't require any escape-sequence manipulations, but will make you have two different variables for primary prompt: PS1 with coloring and NPS1 without.

# Here NPS1 stands for "naked PS1" and isn't a built-in shell variable. I've
# defined it myself for PS1-PS2 alignment to operate properly.
PS1='%S%F{red}[%l]%f%s %F{green}%n@%m%f %B%#%b '
NPS1='[%l] %n@%m # '

# Hook function which gets executed right before shell prints prompt.
function precmd() {
    local expandedPrompt="$(print -P "$NPS1")"
    local promptLength="${#expandedPrompt}"
    PS2="> "
    PS2="$(printf "%${promptLength}s" "$PS2")"

Note the usage of print -P for prompt expansion, ${#variable} for getting the length of string stored in variable, and printf "%Nd" for left-hand padding with N spaces. Both print and printf are built-in commands, so there should be no performance hit.

share|improve this answer

You must log in to answer this question.

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