You are not logged in.

#1 2018-01-06 22:53:57

Zhme
Member
Registered: 2017-06-11
Posts: 80

[SOLVED]Broke my bash PS1//word wraps over PS1 prompt on the same line

I just wanted to change the color of the user escape and the host escape and add a nice bold to it to spruce up my terminal window and make it snazzy.  The prompt looks exactly as I want it to... but now when I issue a command I believe 30 characters and higher just writes over the same line and overwrites my PS1 prompt.  Although it looks foolish and doesn't work as intended obviously, the commands that overwrite the PS still work even while wrapping onto the same line. 

Here is my ~/.bashrc

MAGENTA="$(tput setaf 5)"
CYAN="$(tput setaf 6)"
BOLD="$(tput bold)"
RESET="$(tput sgr0)"

PS1="[${BOLD}${CYAN}\u${RESET}@${BOLD}${MAGENTA}\h${RESET} \W]\$ "

I realize this may be an awful way to do this but it is my very first attempt, be kind!  Thank you in advance for any tips!

Last edited by Zhme (2018-01-06 23:27:15)

Offline

#2 2018-01-06 23:09:31

chris.m
Member
Registered: 2014-04-06
Posts: 24

Re: [SOLVED]Broke my bash PS1//word wraps over PS1 prompt on the same line

I don't have much experience using tput, but I have come across the same problem using escape sequences. The PS1 needs to be properly escaped to avoid this issue;

MAGENTA='\[\e[0;35m\]'
CYAN='\[\e[0;36m\]'
BOLD='\[\e[1m\]'
RESET='\[\e[0m\]'

export PS1="[${BOLD}${CYAN}\u${RESET}@${BOLD}${MAGENTA}\h${RESET} \W]\$ " 

In the above code the extra `\[` and `\]` are needed. You can get some good tips here ... FLOZz' MISC » bash:tip_colors_and_formatting

Offline

#3 2018-01-06 23:17:00

Zhme
Member
Registered: 2017-06-11
Posts: 80

Re: [SOLVED]Broke my bash PS1//word wraps over PS1 prompt on the same line

chris.m wrote:

In the above code the extra `\[` and `\]` are needed.

Are you talking about replacing them inside of the PS1?

PS1="\[${BOLD}${CYAN}\u${RESET}@${BOLD}${MAGENTA}\h${RESET} \W\]\$ "

like so?

Offline

#4 2018-01-06 23:18:01

ayekat
Member
Registered: 2011-01-17
Posts: 1,617

Re: [SOLVED]Broke my bash PS1//word wraps over PS1 prompt on the same line

Or to make use of tput:

MAGENTA="\\[$(tput setaf 5)\\]"

\[ and \] are required to tell bash not to count the escape sequences to the prompt width.

Also, you may need to properly escape your backslashes, as you're using double quotes (e.g. for the \$ part).


pkgshackscfgblag

Offline

#5 2018-01-06 23:18:44

Zhme
Member
Registered: 2017-06-11
Posts: 80

Re: [SOLVED]Broke my bash PS1//word wraps over PS1 prompt on the same line

Changing that did fix the line wrap but only slightly it added more characters to the wrap where it begins to replace again

Offline

#6 2018-01-06 23:26:47

Zhme
Member
Registered: 2017-06-11
Posts: 80

Re: [SOLVED]Broke my bash PS1//word wraps over PS1 prompt on the same line

Thank you all for the help it was my escape '\'s that were misplaced, need to do a vast amount of reading in order to properly modify/use them in the future xD.

MAGENTA="\\[$(tput setaf 5)\\]"
CYAN="\\[$(tput setaf 6)\\]"
BOLD="\\[$(tput bold)\\]"
RESET="\\[$(tput sgr0)\\]"

PS1="${BOLD}${CYAN}\u${RESET}@${BOLD}${MAGENTA}\h${RESET} \W\$ "

This now gives me the perfect output, operationally and visually.  Cheers!

Offline

#7 2018-01-06 23:35:16

ayekat
Member
Registered: 2011-01-17
Posts: 1,617

Re: [SOLVED]Broke my bash PS1//word wraps over PS1 prompt on the same line

Little-nitpicking (it doesn't really make any difference, because \$ is only going to be "$" anyway as a regular user):
If you want to have \$ as understood by bash, you'll need to write it as \\\$ (escape both the backslash and the dollar), as you're defining it between double-quotes (").

The same is true for all the other backslashes (it's pure coincidence that neither \u nor \h nor \W have any special meaning in a string). So:

PS1="${BOLD}${CYAN}\\u${RESET}@${BOLD}${MAGENTA}\\h${RESET} \\W\\\$ "

Last edited by ayekat (2018-01-06 23:35:47)


pkgshackscfgblag

Offline

Board footer

Powered by FluxBB