2

Without color my prompt looks like this:

[0] [dimo414@dimo414:/tmp]
$ echo $PS1 | sed 's/\x1b/\\e/g'
\n[0] [dimo414@dimo414:/tmp]\n\$

I add some color and \[...\] delimit it:

[0] [dimo414@dimo414:/tmp]
$ echo $PS1 | sed 's/\x1b/\\e/g'
\n[\[\e[32m\]0\[\e[0m\]] [\[\e[34m\]dimo414\[\e[0m\]\[\e[32m\]@dimo414\[\e[0m\]:\[\e[94m\]/tmp\[\e[0m\]]\n\$

Original Prompt

Looks great! But the BashFAQ points out that:

The \[ \] are only special when you assign PS1, if you print them inside a function that runs when the prompt is displayed it doesn't work. In this case you need to use the bytes \001 and \002

However when I change my prompt to use \001 and \002 instead, I get this (StackExchange appears to correctly renders them as zero-width characters, but they're there):

[0] [dimo414@dimo414:/tmp]
$ echo $PS1 | sed 's/\x1b/\\e/g'
\n[\e[32m0\e[0m] [\e[34mdimo414\e[0m\e[32m@dimo414\e[0m:\e[94m/tmp\e[0m]\n\$

New, broken prompt

This same prompt - with \001...\002 - looks correct in iTerm2 on OSX.

I'm using these two commands to escape the color sequences:

# Previously (works)
printf '\[%s\]' "$(color "$@")"

# Currently (doesn't work on Ubuntu)
printf '\001%s\002' "$(color "$@")"

What should I be doing instead of the second option in order to use \001...\002 on Ubuntu?

8
  • I think it's because gnome-terminal can display Unicode chars. \001 and \002 are Unicode escapes.
    – user423626
    Apr 26, 2016 at 23:55
  • @BharadwajRaju using \001 and \002 works in other Unicode-supporting terminals (e.g. iTerm2).
    – dimo414
    Apr 27, 2016 at 0:15
  • I'd say there's something missing in your prompt probably. By the way, Unicode isn't \001 , those are octal values. Unicode in printf is \uHHHH Only that doesn't seem to work with printf - there's some kind of bug in printf. Apr 27, 2016 at 0:26
  • @serg any idea what sort of something might be missing?
    – dimo414
    Apr 27, 2016 at 2:00
  • Upstreamed: bugzilla.gnome.org/show_bug.cgi?id=765992
    – egmont
    May 4, 2016 at 17:02

0

You must log in to answer this question.

Browse other questions tagged .