I am sure that I have seen someone have a part of their prompt aligned to the right in their terminal window and then have the actual cursor start on a second line. I know that I can achieve the second line with a "\n" in the PS1, but I cannot figure out how to align part of it to the right. Was what I saw just whitespace added between the two strings?
Answer
What you want can fairly easily be done by displaying the first line before displaying the prompt. For example, the following displays a prompt of \w
on the left of the first line and a prompt of \u@\h
on the right of the first line. It makes use of the $COLUMNS
variable which contains the width of the terminal and the $PROMPT_COMMAND
parameter which is evaluated before bash displays the prompt.
print_pre_prompt ()
{
PS1L=$PWD
if [[ $PS1L/ = "$HOME"/* ]]; then PS1L=\~${PS1L#$HOME}; fi
PS1R=$USER@$HOSTNAME
printf "%s%$(($COLUMNS-${#PS1L}))s" "$PS1L" "$PS1R"
}
PROMPT_COMMAND=print_pre_prompt
Comments
Post a Comment