LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-27-2021, 03:08 AM   #1
thomasbb
Member
 
Registered: Mar 2019
Location: Nice
Distribution: Xubuntu
Posts: 123

Rep: Reputation: Disabled
Angry [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?

Last edited by thomasbb; 01-27-2021 at 03:29 AM. Reason: add info
 
Old 01-27-2021, 05:49 PM   #2
Tonus
Senior Member
 
Registered: Jan 2007
Location: Paris, France
Distribution: Slackware-15.0
Posts: 1,397
Blog Entries: 3

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Is that on tty ? On a terminal emulator ? Which one ?
A bit more context could help to help...
 
1 members found this post helpful.
Old 01-27-2021, 08:38 PM   #3
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,327

Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
Quote:
Originally Posted by thomasbb View Post
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.

Last edited by computersavvy; 01-27-2021 at 08:41 PM.
 
1 members found this post helpful.
Old 01-28-2021, 01:35 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ Yes please.
Quote:
Originally Posted by thomasbb View Post
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?
 
1 members found this post helpful.
Old 01-28-2021, 06:43 AM   #5
thomasbb
Member
 
Registered: Mar 2019
Location: Nice
Distribution: Xubuntu
Posts: 123

Original Poster
Rep: Reputation: Disabled
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 View Post
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 View Post
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 View Post
^ 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

Last edited by thomasbb; 01-28-2021 at 06:46 AM. Reason: typo
 
Old 01-28-2021, 12:49 PM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by thomasbb View Post
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.
 
  


Reply

Tags
prompt


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tiny but annoying problem regarding PS1 prompt appearance after tweaking Andramalech Linux - Software 1 04-08-2010 09:30 AM
Changing the command prompt PS1="****" scotthill Linux - Newbie 7 01-02-2009 11:51 AM
Prompt not hard returning after PS1 variable change? SlowCoder Linux - Newbie 5 05-01-2007 09:16 PM
Post your BASH prompt [PS1]? introuble General 11 12-27-2006 03:47 PM
$PS1 different in X and prompt codec Slackware 7 06-27-2004 04:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration