40
votes

I've done something to break my Bash Shell Prompt in OS X (10.5.7) Terminal.

This is the PS1 that I had configured:

PS1='\[\e[1;32m\]\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]\$ '

As far as I can tell I have the color commands escaping correctly. However when I scroll up and down in my command history I often get line wrapping issues if the historic commands wrap onto multiple lines.

I simplified my prompts to the following:

PS1='\[\e[1m\]\h:\w\$ \[\e[0m\]'
PS2='> '

And I still see something like:

localhost:~/Library/Application Support/Firefox/Profiles/knpmxpup.Defau
lt/extensions/{1A2D0EC4-75F5-4c91-89C4-3656F6E44B68}$ expocd \{1A2D0EC4-7
5F5-4c91-89C4-3656F6E                                           export PS1="\[
\e[1;32m\]\h\[\e[0m\]:                                          cd Library/Appl
ication\ Support/

I've also tried \033 instead of \e. I just included PS2 up there for information, I haven't changed that from the install default. If I completely remove the color codes then everything works fine, any ideas?

9
What is $TERM? If I recall correctly, Terminal.app's terminal emulation doesn't exactly match up to anything in the terminfo database, but dtterm is close.ephemient
I've tried your PS1 on bash 3.2.33 on Fedora8. It works OK with long lines even when dynamically changing the width of the terminal. It prints duplicate lines when changing height, but a Ctrl-L fix that. Might be a bug ...neuro
@ephemient my $TERM is 'xterm-color'Rob
If the problem still happens with TERM=dtterm (configurable through Terminal's settings somewhere), then I dunno. It's been years since I've touched OS X.ephemient

9 Answers

50
votes

I am now using this PS1 with good effect:

green=$(tput setaf 2)
blue=$(tput setaf 4)
bold=$(tput bold)
reset=$(tput sgr0)
PS1="\[$green$bold\]\h\[$reset\]:\[$blue$bold\]\w\[$reset\]\$ "

Scrolling through my command history appears to handle line wraps now. However in the meantime since this question was asked I have also updated my OS X to 10.6.3

21
votes

This stackoverflow thread seems relevant. As someone noted in that thread, the Bash FAQ at mywiki.wooledge.org discusses how to properly quote color codes in Bash prompts (FAQ 53), and the proper invocation of terminal colors (FAQ 37).

8
votes

Line wrapping issues in Bash are nothing new. Your PS1 should work as is but there is a bug in Bash 3.2.49. Consult the mailing list, there's yet another bug regarding this which was confirmed to be fixed in 4.0.

You can't do much more than tagging unprintable characters with \[ and \], the rest must be done by the prompting code.

2
votes

It seems that you have correctly escaped and enclosed sequences.

A workaround I use anyway it it to add a '\n' at the end. I find it clearer and lessen any problem with wrapping issues. The exact end of my PS1 is :

'\n\[\033[0;30m\]$\[\033[0m\]

An excellent howto you probably know :

Bash prompt howto

1
votes

I noticed that there are some issues with the prompt cursor positioning even if there are no special character in the PS1 or PROMPT environment variable.

If we output a file that does not have a end-of-line char at the end. It will confuse the prompt.

You can reproduce this by doing:

curl https://gist.githubusercontent.com/martinos/d4aa0a7d4d752b0d0d9f/raw/3198c39f84a080c44227a084a19fb3a0bb661ee5/wrapping_issue.txt

and pressing the up key multiple times and you will see that the prompt get confused.

You can see an example of this in action:

https://asciinema.org/a/9mtjhi9dib6md4ocsbw210cca

When this occurs, just press <CTRL-C> and the prompt will come back to normal.

Note that ZShell does not have this issue.

0
votes

For future reference, this is what I use:

export PS1="\[\033[0;31m\][\u@Project:\w]$\[\033[0m\] "

This would display my shell prompt as:

[ec2-user@Project:~]$

Helps me distinguish between live and dev sites.

0
votes

Here's mine: it's the best one I've found, but the site where I originally found it was missing an escape character, leading to the line wrapping issue. I tinkered with it and finally got it working. It shows your user, path, and branch info with good contrast, color-wise.

export PS1='\[\e[1;37m\]\[\e[1;32m\]\u\[\e[0;39m\]:\[\e[1;33m\]\w\[\e[0;39m\]\[\e[1;35m\]$(__git_ps1 " (%s)")\[\e[0;39m\] \[\e[1;37m\]|\[\e[0;39m\]\$'

Also, add

GIT_PS1_SHOWDIRTYSTATE=true

To show a marker when a branch is "dirty" (changes to be committed exist)

export HISTCONTROL=ignoredups

Is also useful to ignore duplicates when scrolling up through bash history.

bind "set completion-ignore-case on" 

Helps too.

Lastly,

shopt -s checkwinsize

May be helpful on OSX if issues persist.

0
votes

'shopt -s checkwinsize' also works for Cygwin wrap problems also

0
votes

If you're using the title bar trick "\e]2;titlebar\a", make sure to escape that too: "\[\e]2;titlebar\a\]"