OS X Terminal text stacking on top of itself

Viewed 19046

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.

6 Answers

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!

Related