Wednesday, June 15, 2011

Bash Color Prompt Problems With Reverse-I-Search in Terminal.app

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. Terminal.app 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\]\$ "

Uffffff.

The fix: use iTerm.app. Terminal.app doesn't render color properly, apparently. I don't have an explanation for this yet, but iTerm.app 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 Terminal.app. 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 Terminal.app is not nearly as feature rich, or as responsive as anything on Linux).

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

7 comments:

  1. This was really useful to me, thanks.

    ReplyDelete
  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:
    Code:
    PS1=$'\[\e[1;36m\]\u@\h:\[\e[22m\]\w\[\e[1m\]\$ \[\e[00m\]'

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

      Delete
  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 terminal.app 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.

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

    ReplyDelete
  5. Anonymous2:57 am

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

    FWIW, I use PuTTY under Windoze7.

    ReplyDelete
  6. Anonymous3:03 am

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

    ReplyDelete

Labels

About Me

My photo
Singapore
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. :)