I've written this nice and colorful prompt for my OS X terminal:

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

export PS1="\e]2;\w\a\e[1;30;1m\][\u@\[\033[0;36m\]\h\e[1;30;1m\]:\[\033[0;35m\]\w\e[1;30;1m\]] \[\033[0;31m\](\t) \[\033[0;33m\]\$(parse_git_branch)\n\[\033[0m\]\e[1;30;1m\]\$\[\e[0m\]"

I source ~/.bash_profile it and it works fine. except when I use the arrow up to navigate the history I get a weird thing happening.

basically the beginning of the source ~/.bash_profile command in the history, becomes part of the prompt. when I ctrl+c or enter it returns to normal, but as soon as I see the history of the source command the prompt changes again.

Here's an example where I've seen the history and then ctrl+u and typed ls:

[user@host:~/Documents] (14:01:40)  [branch]
$source ~/.ls

Any clue about this?


You've completely banjanxed bash's idea of what's been printed and what it has to erase/rewrite as it displays command history and lets you edit the command line. This is because you've made a real hash of that prompt.

  • Use either \e or \033 consistently, for your own sanity.
  • Make your \[ and \] strictly matching non-nesting pairs.
  • Make sure that all non-printing sequences are within \[ and \] (and that, conversely, that all printing sequences are not).

(This is why I personally prefer the Z Shell and its alternative prompt expansion mechanism for when I want wacky coloured prompts. It knows that things like %F{green} aren't printing sequences, without having to be told; and it also works out the correct escape sequences from terminfo, without having them hardwired.)

| improve this answer | |
  • thanks, good idea I turned to use zsh which I didn't know before and looks real cool. – Don Giulio Jan 1 '14 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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