Bash prompt line wrapping issue
Asked Answered
S

4

12

Newbie question, I recently changed my PS1 into this:

RESET="\[\017\]"
NORMAL="\[\033[0m\]"
YELLOW="\[\033[0;33m\]"
CYAN="\[\033[0;36m\]"

export PS1="\[$RESET\]\u@\h:\[$CYAN\]\w\[$YELLOW\]\$(__git_ps1)\[$NORMAL\] \$ "

But now I get a line wrapping error. I created a gif to explain the problem:

enter image description here

Any ideas what might be wrong?

Scherman answered 1/9, 2013 at 14:34 Comment(3)
@What does your __git_ps1 function do? Can you show us a code?Dwayne
@Dwayne It's a shell script to display current branch if current directory is a git project. Code here: raw.github.com/git/git/master/contrib/completion/git-prompt.shScherman
+1 because I enjoy the gifApplique
D
5

I think you're double-quoting your escape codes with [ and ]. Try this one:

export PS1="$RESET\u@\h:$CYAN\w$YELLOW\$(__git_ps1)$NORMAL \$ "
Dwayne answered 1/9, 2013 at 17:21 Comment(0)
P
3

The wrapping error occurs whenever a non-printing character is not escaped (such as the escape codes that change the prompt color). It also occurs when the locale is set to something that does not understand unicode characters and the prompt includes them. An example would be non-breaking spaces. When the locale is "C" and there are unicode characters in the prompt, the shell thinks more characters are being printed than truly are, and the prompt wraps around prematurely.

Peeve answered 19/4, 2019 at 18:17 Comment(0)
C
1

shopt -s checkwinsize this should fix your problem. This will set the variable checkwinsize on. This is set by default in /etc/bashrc however since you're using your custom .bashrc file you can put this code in there or in your .bashrc file load the /etc/bashrc by adding . /etc/bashrc at the top of your .bashrc file.

Commorancy answered 7/7, 2021 at 21:26 Comment(0)
U
0

A relative recent re-occurrence of this issue was released as part of https://github.com/git/git/commit/7ee1af8cb8b97385fc4e603bc024d877def5adb4#r139625087

I've got a PR in! Hopefully it'll get through.

But if you look for the new version of git-prompt.sh on your machine, in that, look got \001 and \002. It is missing the escaping of the \. So double them up to \\001 and \\002. It looks like something evaluates \001 into 0b1/0x01 and \002 into 0b10/0x02.

These are observable if you echo $PS1 | hexdump -C.

Not sure how relevant this is, but it was changed and others have commented it as being bad. I couldn't find a PR so I made one.

Underlaid answered 11/3 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.