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\$
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\$
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?
gnome-terminal
can display Unicode chars.\001
and\002
are Unicode escapes.\001
and\002
works in other Unicode-supporting terminals (e.g. iTerm2).\001
, those are octal values. Unicode inprintf
is\uHHHH
Only that doesn't seem to work withprintf
- there's some kind of bug inprintf
.