Wednesday, June 15, 2011

Bash Color Prompt Problems With Reverse-I-Search in

I recently colorized my bash prompt. It's a nice touch, and with a bit of taste, it's good design. Makes visual parsing very quick.

Gone are the days of souped up command-line prompts that look like they're about to lift off into orbit. For a trip down memory lane, check this out. I stumbled across it while Google'ing this problem. Good fun. :-)

No, my prompt is very Spartan:

shaque@marius is user@host (so you know what computer you're on), ~/git_tree/users is the present working directory, and ihaque/vim-tweaks is my git branch.

All of this is awesome. The colors are awesome too (Solarized), they're specially picked colors for a low contrast delta between the foreground and the background, while maintaining excellent readability over prolonged periods. Prevents tired eyes. Go to Ethan Schoonover's website I've linked to, and integrate it into your environment. Srsly.

The problem: once you have colors, and if you use bash's builtin reverse-i-search for previously typed commands, things get fudged. doesn't write the characters properly on-screen so if you reverse-i-search for a previous command, then scroll backward or forward to edit it, it doesn't display correctly.

I spent a lot of hours tinkering with my bash prompt to make this work. It was awful, because bash syntax is very un-pretty. Look at the code for my prompt:

export PS1="\[\e[0;36m\]\u@\h\[\e[m\] \[\e[0;34m\]\w\[\e[m\] \[\e[0;33m\]\$(parse_git_branch)\[\e[m\]\$ "


The fix: use doesn't render color properly, apparently. I don't have an explanation for this yet, but will gladly handle your colorized prompt and finicky interactive shell features with barely a sweat.

It's also an actively maintained open source project with a shorter release cycle (read: one that is not pinned to that of OS X's).

Bless the folks at Apple, I think they did a pretty good job with It's miles and miles better than any stock CLI shell Microsoft ever shipped with their operating systems, and the anti-aliasing and opacity control is better than anything I've seen on Linux (though is not nearly as feature rich, or as responsive as anything on Linux).

Credit goes to, I stuck with it for a long time, but I'm switching to now. It's just better.


  1. This was really useful to me, thanks.

  2. Good article!
    And if you are searching for unix colored bash fix:
    When setting PS1 or PS2, you have to tell the shell if any part of it doesn't advance the cursor. You do that by wrapping those parts of the prompt inside \[ and \] codes. That is:
    PS1=$'\[\e[1;36m\]\u@\h:\[\e[22m\]\w\[\e[1m\]\$ \[\e[00m\]'

    1. Thank You. This fixed my reverse search issue. Using iTerm didn't help my case

  3. Great article, found it because I had the exact same problem (even down to the color scheme!)

    BUT the above comment fixed my problem so mega props to Drey.

    I was hoping to fix this in because I am using the total terminal wrapper to keep it available as an overlay. I have heard enough good things about iTerm though, that I am still compelled to check it out.

    Thanks both of you.

  4. Switching apps to get this fixed seems a sub-optimal fix no matter how good iTerm 2 is (highly recommend!), I think I'll update this with a proper fix. :-D Thanks, guys.

  5. For some reason, my PS1 was missing the leading $ and adding that fixed the problem.

    FWIW, I use PuTTY under Windoze7.

  6. Further investigation reveals that Putty doesn't have this problem IF you either surround the value of PS1 with double-quotes OR prefix the single-quoted value with a dollar sign.

    This is the opposite of TIMTOWTDI and more like Tolstoy's "Happy families are all alike; every unhappy family is unhappy in its own way."



About Me

My photo
I write essays in my spare time on things that are important to me. The ones that I feel are any good, or make any sense, I put them up here. :)