Ugly but useful `PS1` promptSat 12 January 2019
I live in Germany since a bit more than 5 years now. There is something particular in this country that does virtually not exist in France. I believe this something characterizes a lot what German people do, and that can be summarized as the following:
Usefulness justifies everything
For instance, usefulness justifies ugliness. I am good at doing ugly, now I need useful.
I use git everyday, in a tmux terminal on OSX, and I've never managed to have git commands completion so far.
I was also jaleous from all those people that configured
oh my zsh on their computer, such that they have
fancy coloring and more things I do not know about.
After some digging:
- completion can be done by sourcing a file in a vanilla Xcode installation
- prompt showing additional hint may benefit from the
__git_ps1command that comes also with the git in Xcode installation from the file
- coloring the prompt is super easy, according to those color tables
Here is what my
.bashrc looks like on my OSX machine :
source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash source /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh export GIT_PS1_DESCRIBE_STYLE=branch GIT_PS1_SHOWDIRTYSTATE=true GIT_PS1_SHOWUPSTREAM="auto" GIT_PS1_SHOWCOLORHINTS=1 export PS1="\[\e[1m\]\u\[\e[0m\] \[\e[7m\]#\[\e[0m\] \[\e[96m\]\t\[\e[0m\] \[\e[45m\e[1;30m\]\w\[\e[0m\] \[\e[43m\e[30m\]\$(__git_ps1)\[\e[0m\] \[\e[1m\e[31m\]>\[\e[0m\] "
Of course, it did not work out of the box and took me a bit more time than expected to make it work. The important bits were:
- every sequence that does not emit a visible output, for instance all formatting/coloring commands, should be wrapped in a group beginning with
\[and ending with
- every command evaluation like
__git_ps1should be written as
\$(__git_ps1), such that it is evaluated every time
PS1needs to be rendered (every new line)
For the first bullet above, it means that if you want to change to color of the foreground and background before a specific placeholder such as
you have to use
\[\] before the
\w (prints current path), example:
In the above, we see 2 groups: one before and one after the
\[\e[45m\e[1;30m\] for magenta background+bold+black text, and
\[\e[0m\] for resetting all attributes. Not putting things in group would mess up your terminal, in particular you will not be able to see
properly previous commands with the up arrow.
Here is what my terminal looks like now
I haven't lied when I told you I can do ugly...