LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   [PS1] prompt misaligned (https://www.linuxquestions.org/questions/linux-newbie-8/%5Bps1%5D-prompt-misaligned-4175689334/)

thomasbb 01-27-2021 03:08 AM

[PS1] prompt misaligned
 
When I go back in the command history, if a command is long, it shortens the prompt by 14 characters whatever the directory. Here is my PS1:
Code:

PS1="" # reset
PS1="$PS1\[\e[0m\]\[\e[03;32m\]\u" # user
PS1="$PS1\[\e[0m\]\[\e[01;35m\]@" # separator
PS1="$PS1\[\e[0m\]\[\e[01;36m\]\h" # host
PS1="$PS1\[\e[0m\]\[\e[01;35m\]:" # separator
PS1="$PS1\[\e[0m\]\[\e[48;5;95;38;5;214m\D{%d.%m@%H.%M.%S}\]" # date
PS1="$PS1\[\e[0m\]\[\e[01;35m\]:" # separator
PS1="$PS1\[\e[0m\]\[\e[01;34m\]\w" # path
PS1="$PS1\[\e[0m\]\[\e[01;35m\]:" # separator
PS1="$PS1\[\e[0m\]\[\e[01;37m\]$?" # last error code
PS1="$PS1\[\e[0m\]\[\e[01;35m\]:\$\[\e[00m\] " # ending

Any idea what goes wrong?

Tonus 01-27-2021 05:49 PM

Is that on tty ? On a terminal emulator ? Which one ?
A bit more context could help to help...

computersavvy 01-27-2021 08:38 PM

Quote:

Originally Posted by thomasbb (Post 6212476)
When I go back in the command history, if a command is long, it shortens the prompt by 14 characters whatever the directory. Here is my PS1:
Code:

PS1="$PS1\[\e[0m\]\[\e[01;34m\]\w" # path
Any idea what goes wrong?

On the line you have labeled path \w gives the full path to the pwd while \W give just the current pwd. That can certainly give you a long prompt line.

Beyond that you do not tell us what distro you are on nor the $SHELL you are using so there is no way for most of us to even test and see what might be happening. That PS1 line does not work at all well in bash on my system.

A quick google search for "how to change ps1 in linux" will give you several links and you can even change that search for the shell you are using.

ondoho 01-28-2021 01:35 AM

^ Yes please.
Quote:

Originally Posted by thomasbb (Post 6212476)
When I go back in the command history, if a command is long, it shortens the prompt by 14 characters whatever the directory. Here is my PS1:
Code:

PS1="" # reset
PS1="$PS1\[\e[0m\]\[\e[03;32m\]\u" # user
PS1="$PS1\[\e[0m\]\[\e[01;35m\]@" # separator
PS1="$PS1\[\e[0m\]\[\e[01;36m\]\h" # host
PS1="$PS1\[\e[0m\]\[\e[01;35m\]:" # separator
PS1="$PS1\[\e[0m\]\[\e[48;5;95;38;5;214m\D{%d.%m@%H.%M.%S}\]" # date
PS1="$PS1\[\e[0m\]\[\e[01;35m\]:" # separator
PS1="$PS1\[\e[0m\]\[\e[01;34m\]\w" # path
PS1="$PS1\[\e[0m\]\[\e[01;35m\]:" # separator
PS1="$PS1\[\e[0m\]\[\e[01;37m\]$?" # last error code
PS1="$PS1\[\e[0m\]\[\e[01;35m\]:\$\[\e[00m\] " # ending

Any idea what goes wrong?

I see what you're trying to do there, but I think this way of "composing" it makes it more complex instead of simpler.

But that's beside the point.

2 things:
  • Are you sure you need to reset ('[\e[0m]') every time you choose a new color?
  • The '[' and ']' characters enclose sections that do not take up any space (like change of color). Sections that do take up space should not be enclosed int his way. Getting this wrong can misalign the prompt. Are you sure you got that right everywhere?

thomasbb 01-28-2021 06:43 AM

The problem was the date: it was
Code:

PS1="$PS1\[\e[0m\]\[\e[48;5;95;38;5;214m\D{%d.%m@%H.%M.%S}\]" # date
and it takes
Code:

PS1="$PS1\[\e[0m\]\[\e[48;5;95;38;5;214m\]\D{%d.%m@%H.%M.%S}" # date
Now it all goes well

Quote:

Originally Posted by Tonus (Post 6212793)
Is that on tty ? On a terminal emulator ? Which one ?
A bit more context could help to help...

It was from a terminal but on a remote via ssh

Quote:

Originally Posted by computersavvy (Post 6212845)
On the line you have labeled path \w gives the full path to the pwd while \W give just the current pwd. That can certainly give you a long prompt line.

Beyond that you do not tell us what distro you are on nor the $SHELL you are using so there is no way for most of us to even test and see what might be happening. That PS1 line does not work at all well in bash on my system.

A quick google search for "how to change ps1 in linux" will give you several links and you can even change that search for the shell you are using.

The path is a bit long indeed but as I use the full width of the screen, it wasn't the source of the problem.

The shell is bash on the remote, just checked with the command chck.
The pages on ps1 don't give the same level of information: the basic ones give full explanation but not all options and the pages that give a broader choice of configuration don't tell the syntax to use them.

Quote:

Originally Posted by ondoho (Post 6212893)
^ Yes please.

I see what you're trying to do there, but I think this way of "composing" it makes it more complex instead of simpler.

But that's beside the point.

2 things:
  • Are you sure you need to reset ('[\e[0m]') every time you choose a new color?
  • The '[' and ']' characters enclose sections that do not take up any space (like change of color). Sections that do take up space should not be enclosed int his way. Getting this wrong can misalign the prompt. Are you sure you got that right everywhere?

Well, not breaking it up makes it even harder to read than in several lines...
The reset is needed when 'bolding' and 'backgrounding' but for indentation readability, I put it everywhere.
The only space I have is the last character, but thanks for the trick because I didn't know that

ondoho 01-28-2021 12:49 PM

Quote:

Originally Posted by thomasbb (Post 6212970)
The only space I have is the last character, but thanks for the trick because I didn't know that

That's not what "taking up space" means here, but you got it fixed according to my meaning anyhow.
:thumbsup:


All times are GMT -5. The time now is 03:30 PM.