Skip to content

Configure your prompt conveniently

TomfromBerlin edited this page Feb 5, 2023 · 1 revision

I'm not sure if the configuration of the prompt described here also works in other shells. Maybe it works, maybe not.

This .bashrc makes the prompt configuration a little bit easier. Nonetheless it is a pain in the a...

A colored prompt with some useful information often looks like this in the .bashrc file:

PS1="\n\e[0;37mSystemzeit \A\n\e[1;34m\u \e[1;33m@ \e[1;32m\h \e[1;32m\w\\e[0m:\$\[\e[0;5m\]_\e[0m

This is a bunch of cryptic strings. There are escape codes and color codes and it almost takes rocket science to read and understand. But if you don't hate your life then you won't turn down help and there are some really badass guys out there who will use their skills to make your life easier. One of them is Scriptim and he made the phantastic Bash Prompt Generator. You can use this tool to preformat your prompt and afterwards you can colorize it using human-readable format. The code for the prompt in this .bashrc looks like this:

PS1="\n\[${LIGHTGRAY}\]Systemzeit \A\n\[${LIGHTBLUE}\]\u \[${YELLOW}\]@ \[${LIGHTGREEN}\]\h \[${LIGHTGREEN}\]\w\[${NC}\]:\$\[\e[0;5m\]_\[${NC}\]'

As you can see it is more readable. I'll break it down into individual pieces for better understanding:

Code Description
PS1 prompt variable, you can define more than one prompt var, (PS1, PS2...PSN (where N is an integer)
= equal sign, splits the name of the variable from the content
" The first quotation mark defines the beginning of the content of the variable. Some will need or just use a single quote instead of a double quote.
\n line break / new line
\[${LIGHTGRAY}\] color formatting
Systemzeit text to be shown (with tailing space)
\A time
\n line break / new line
\[${LIGHTBLUE}\] color formatting
\u user name (with tailing space)
\[${YELLOW}\] color formatting
@ text to be shown (with tailing space)
\[${LIGHTGREEN}\] color formatting
\h host name (with tailing space)
\[${LIGHTGREEN}\] color formatting
\w Path
\[${NC}\] reset all
: text to be shown
\$ dollar sign
\[\e[0;5m\] makes the following output blink. Note that the blink attribute may not be supported by all terminals (XTerm, tty will).
_ the actual prompt, in this case an underscore
\[${NC}\] reset all (color and formatting)
" The second and last quotation mark defines the end of the content of the variable. If you use a single quote for the first quotation mark you have to use it here, too.
Without the tailing spaces the output looks very squeezed.

Despite the human-readable format, configuring the prompt remains a small challenge.

Which colors already have been "transformed" into human-readable format you can learn while studying the file ~/.shellcfg/colors. In case you want to have more colors invoke allcolors in a terminal. This will show you a lot of color codes and how they will look like. You can then add the desired color codes to the ~/.shellcfg/colors file using the following format: <unique color name>=<'color code'> (the quotation marks are required) and you're good to go. In order to use that human-readable format, colors must be indicated in the prompt configuration as follows: \[${COLORNAME}\]. The specified color remains valid until another color is specified or it is reset with \[${NC}\]. Theoretically, up to 256 colors can be used, but I recommend to limit yourself to the colors that allcolors outputs unless you want everything brightly colored.

A few Escape Codes

Code Beschreibung Description
\@ Aktuelle Zeit im 12-Stunden am/pm Format Current time in 12-hour am/pm format
\T Uhrzeit im 12-Stunden Format (hh:mm:ss) time 12h format with seconds (hh:mm:ss)
\A Uhrzeit im 24-Stunden Format (hh:mm) time 24-hour format (hh:mm)
\t Uhrzeit im 24-Stunden Format (hh:mm:ss) time 24h format with seconds (hh:mm:ss)
\u aktueller Benutzer current user
\h Rechnername computer name
\d Datum date
\W aktuelles Verzeichnis current directory
\w kompleter Pfad zum aktuellen Verzeichnis full path to current directory
\n neue Zeile new line
\j Anzahl der z.Z. verwalteten Prozesse number of currently managed processes
\s Name der Shell shell name
\v die Version der Bash version of bash
\\ ein \ a \
\[ und \] Alles was dazwischen steht wird nicht ausgegeben. Farbcodes gehören zwischen diese Zeichen, sonst kommt es zu Anzeigefehlern, z.B. \[${LIGHTCYAN}\] resp. \[\e[1;36m\]. Escape-Codes gehören nicht dazwischen. Everything between \[ and \] is not output. Color codes belong between these characters, e.g. \[${LIGHTCYAN}\] resp. \[\e[1;36m\], otherwise display errors will occur. Escape codes do not belong in between.
Das sind nur die wichtigsten Escape-Codes, eine komplette Liste findet ihr in der Manpage der Bash <Befehl man bash> und hier und hier These are only the most important escape codes, a complete list can be found in the bash manpage <command man bash> and here and here