41

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Rob
  • 3,687
  • 2
  • 32
  • 40
  • 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 Jul 15 '09 at 18:51
  • 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 Jul 16 '09 at 16:54
  • @ephemient my $TERM is 'xterm-color' – Rob Jul 16 '09 at 20:45
  • 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 Jul 16 '09 at 21:35

9 Answers9

51

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

Rob
  • 3,687
  • 2
  • 32
  • 40
  • 1
    Thank you! This has driven me crazy for a long time. I think the trick is your method for doing colors. I was using this style for colors (this one is blue bold): \e[0;34m – Cory Gagliardi Nov 04 '11 at 16:13
  • 1
    this is the real answer for OS X, including latest Mavericks and Yosemite, thanks. – Joey T Oct 14 '14 at 17:32
  • +1 for El Capitan and `tput setaf`. The `\[$green\]` syntax worked for me; I was using `${green}` in the `PROMPT_COMMAND` which caused the odd same-line line wrapping. – mhulse Mar 12 '16 at 05:53
  • On macOS Sierra, the standard use of `\e[38;5;220m` is giving me line wrapping trouble, however simply wrapping those with `\[ ... \]` seems to solve the problem. – Pascal Sep 23 '16 at 11:42
  • @Pascal (and others here in the future), the original question should have worked, but there was a bug in bash at the time. https://lists.gnu.org/archive/html/bug-bash/2009-07/msg00016.html – Captain Man Mar 19 '20 at 19:51
21

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).

Community
  • 1
  • 1
willdye
  • 795
  • 9
  • 10
  • Using tput to generate my color codes as described in this FAQ: http://mywiki.wooledge.org/BashFAQ/037 And my prompt scrolls much better. However I am also on a new version of OS X so I am not sure which may have fixed this. – Rob May 05 '10 at 14:36
8

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.

Captain Man
  • 6,997
  • 6
  • 48
  • 74
TheBonsai
  • 15,513
  • 4
  • 22
  • 14
  • Thanks, I just wanted to confirm that I had escaped things correctly. – Rob Jul 16 '09 at 20:50
  • 1
    Just for people landing here from Google, the original post has nothing wrong with it. The key is using `\[` and `\]` (which Rob did). For an answer solving what you probably did wrong (same for me haha), see [here](https://stackoverflow.com/q/706750/1858327) (it's not mac specific though the title implies it). – Captain Man Mar 19 '20 at 19:46
2

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

neuro
  • 14,948
  • 3
  • 36
  • 59
  • You were correct. There was merely a [bug in bash](https://lists.gnu.org/archive/html/bug-bash/2009-07/msg00016.html) at the time. – Captain Man Mar 19 '20 at 19:54
1

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.

Martinos
  • 2,116
  • 1
  • 21
  • 28
0

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.

Anil
  • 21,730
  • 9
  • 73
  • 100
0

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.

rcd
  • 1,348
  • 1
  • 14
  • 27
0

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

0

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

Charlie
  • 8,530
  • 2
  • 55
  • 53