49

I'm encountering a strange issue in the Terminal app in Mac OS X Lion. When I enter in a long line of text that should wrap to the next line when it reaches the edge of the Terminal window, it continues to type on top of the text from the line above it.

Here are some screenshots to help illustrate the issue:

Before my text reaches the window edge:

before

After the text reaches the window edge:

after

I've also supplied screenshots of my text and window settings in case those might be helpful.

Text settings:

text

Window settings:

window

Thanks in advance for any assistance offered. I've had this issue for a while and just never got around to it. It's now really becoming a pain in the ass when I get into things that require big grep commands and long path names.

4
  • 1
    Have you tried less fancy value for PS1 environment variable? Try export PS1='> ' ; reset. Commented Aug 6, 2012 at 15:31
  • @hamstergene when I ran that command and started typing, it was working properly. Opening a new window puts it back into the same issue though. I'm not familiar with this command but it seems to do the trick. Can you explain further on how to make this a permanent change? Thanks!
    – Daryn
    Commented Aug 6, 2012 at 16:08
  • @hamstergene after doing a little research I figured out what this was referring to. I never thought about customizing that and this seems to be a solid resource.
    – Daryn
    Commented Aug 6, 2012 at 16:40
  • Bash escapes to the rescue: apple.stackexchange.com/questions/37001/…
    – miku
    Commented Feb 27, 2014 at 10:08

6 Answers 6

74

PS1 environment variable determines what shell's prompt will look like. man bash gives full documentation on it. (There are actually several of them, for different modes).

There are number of files that may be setting it, usually one of ~/.profile, ~/.bashrc, /etc/profile or /etc/bashrc.

If you're going to have color codes or other control sequences inside it, you must wrap them with \[ and \] properly (and NOT wrap normal text), otherwise line editing may become messed up like in your case. I suggest resetting PS1 to the default value then carefully adding coloring back item by item.

For example:

PS1='\[\033[1m\033[32m\]\u@\h \w\[\033[0m\]\$ '
       ^^^^^^^^^^^^^^^            ^^^^^^^

Coloring commands are underlined. Note how they are surrounded with \[ \].

6
  • I actually added an answer above this one that was based on your comment, but selected this as the answer because you explained it more in depth and you provided me with the initial info to find a solution. I'm not sure what was defining my original PS1 settings but I edited my .bash_profile and added the appropriate text. This overrode whatever was setting it before and did the trick.
    – Daryn
    Commented Aug 6, 2012 at 17:35
  • 1
    Thank you for this. For some reason every other website I went to had incorrect color codes (perhaps it's just OSX). This is the only one that worked 100% properly in OSX.
    – rosslebeau
    Commented Mar 6, 2013 at 21:35
  • Worked brilliantly for me. Same as @Daryn, any other way just would have problem. Thanks!!! Commented Dec 7, 2015 at 4:09
  • 6
    This works if you use \e too, eg: \[\e[32m\]\u \[\e[36m\]\T \[\e[32m\]\w \[\e[0m\]\\$ . Not each \e has a \[ on the lef, and each m has a \] on the right.
    – DanielM
    Commented Jan 22, 2016 at 12:06
  • 1
    It's worth noting that \e is less universal than \033 and won't work with some shells. If you only use bash it's fine but if you share your scripts with others they may not work.
    – DanielM
    Commented May 12, 2019 at 12:10
8

I have the same problem, i found if you change

Advanced > Emulation > Declare terminal as: ANSI.

This solves colored PS1 problem. With Mac Terminal

BUT creates a strange behavior: I found a solution to my problem with @koiyu answer.

https://apple.stackexchange.com/questions/37001/strange-behavior-in-terminal-with-custom-bash-profile/37036#37036

1
  • 2
    The link provided solves the issue for me, colours used with the \e[... format require proper escape sequences \[\e[...\] to avoid destroying your console behaviour.
    – RndmTsk
    Commented Apr 22, 2015 at 15:20
8

I used to have the same issue due to incorrectly using color codes. Here is my PS1 which solved the issue. Also if you use GIT, then this will be also helpful to show git branch you are working on and if your working tree is dirty or not. Put this in your .profile or .bash_profile

# Git branch in prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

parse_git_dirty() {
    st=$(git status 2>/dev/null | tail -n 1)
    if [[ $st == "" ]]; then
        echo ''
    elif [[ $st == "nothing to commit (working directory clean)" ]]; then
        echo ''
    elif [[ $st == 'nothing added to commit but untracked files present (use "git add" to track)' ]]; then
        echo '?'
    else
        echo '*'
    fi
}

# coloring the terminal comman line
SB_GREEN="\[\033[1;32m\]"
SB_BLUE="\[\033[1;34m\]"
SB_RED="\[\033[1;31m\]"
SB_NOCOLOR="\[\033[0m\]"
export PS1="$SB_GREEN\u@\h$SB_NOCOLOR: $SB_BLUE\w$SB_GREEN\$(parse_git_branch)$SB_RED\$(parse_git_dirty)$SB_NOCOLOR $ "

Hope this helps.

1
  • Thank you, you are the best!
    – bora89
    Commented Mar 4, 2018 at 8:43
2

With the guidance of hamstergene I was able to figure out how to make it play nice. Using this Geek Stuff guide and this It's Me Tommy tutorial, I was able to define how I wanted my PS1 text to display. Changing this to something much more simplified eliminated the weird overlapping text issue I was running into.

Before:

before

After:

after

I simply edited my .bash_profile and added the following line:

export PS1="[\u@\h] > ";

Then I went and changed the window colors for good measure because I can.

0

The original issue was that there was a new line in the PS1 (FYI for anybody running into this)

Typically its improperly escaped color codes, but if its not that its that you have a new line in your ps1

0

As others have said, you have to properly wrap your color commands in escaped square brackets. However, for me, this makes the string really, really confusing to look at!

As such, here's a trick I use to always get it right and also make it much more readable.

I first create a shell function called setColor like so...

setColor(){
    echo "\[\033[${1}m\]"
}

Then I use it like this...

PS1="$(setColor 92)\u$(setColor 37):$(setColor 96)\w $(setColor)\$ "

That's the same as writing this...

\[\033[92m\]\u\[\033[37m\]:\[\033[96m\]\w \[\033[m\]$

...but as you can see, the former is much clearer and also guarantees everything's properly escaped.

Note, you can specify multiple colors too by using the ; character. The only thing is you have to explicitly escape it, so 92;41 becomes 92\;41, like so...

PS1="$(setColor 92\;41)\u$(setColor 37):$(setColor 96)\w $(setColor)\$ "

Again, still easier to read than this...

\[\033[92;41m\]\u\[\033[37m\]:\[\033[96m\]\w \[\033[m\]$

You can take this a step further by defining constants for the colors, or even 'wrapper' functions with the color names you use most, so you can write this...

PS1="$(setRed)\u$(setBlue):$(setGreen)\w $(resetColor)\$ "

Hope this helps!

Not the answer you're looking for? Browse other questions tagged or ask your own question.