Bash Prompt #7: Use the Space in Front of the Prompt

Bash Prompt Index

This is a simple concept, but it never occurred to me until yesterday to use the space in front of the prompt to display information.

This is simple, but does involve some tricks and caveats. The most obvious caveat is that you're going to type right over top of the information as soon as you enter a command. Less obvious is that putting the information there requires a bit of care or it will mess up the line wrap of your prompt: see the discussion about Non-Printing Characters in the Colours entry in this series. You'll also want to choose a colour that will make this ephemeral information visually distinct from the commands you're typing.

What use is the ability to print ephemeral information? It depends how you work, and what you want your system to tell you about. If your network connection goes down a lot (and then you have to go reboot your modem or other gear), maybe you would want to display the network status. In my case, I started with just showing the date - and this is surprisingly useful because I leave my computer screens littered with terminals, each for a different project. And I look at them and wonder "when did I last work on that?" With the date printed after the prompt, that becomes obvious.

Here it is:

DARKGRAY='\033[38;5;008m'
NOCOLOUR='\033[0m'
PS1="\\\$ "
PS1="${PS1}\[\$(
    tput sc
    echo -en \"            ${DARKGRAY}\$(date +%Y-%m-%d:%H%M)${NOCOLOUR}\"
    tput rc
)\]"

Here's what it looks like:

screenshot: p_ephemera is a file containing the above code

return value prompt and prompt with ephemeral date shown

See Return Value in the Prompt if you're interested in the p_retval prompt.

The details:

  • I use a terminal with a black background and white text, so I chose to print the "ephemeral" information in a dark gray so it doesn't look like anything I've typed as a command (see Colours)
  • we record the cursor position with tput sc
  • add as many spaces as you want to position the text after the end of the prompt
  • notice the date sequence is backslash-escaped: if it weren't, the text would always be the date when you sourced the prompt, not when the prompt appeared
  • finally, we restore the cursor position with tput rc
  • look closely at both the colours I'm using and the second piece of the PS1 string that's added: the colours are NOT escaped as non-printing characters, and the code piece of the PS1 string IS. This is because we don't want the prompt to consider any of that in its calculation toward the length of the prompt string - and putting \[ and \] escapes inside other escapes will break stuff, so (unusually), it's important NOT to escape the colours themselves

The placement of the date printed after the prompt is a matter of personal taste, but also depends on the length of the prompt and the width of your terminal. If you have the current working directory before the $, you'll find the length of your prompt varies. I use two line prompts, so I put the variable length stuff in the upper line and have a fixed amount of material in the lower line. You can start doing math to calculate the position or length of the ephemeral information if you want, but that can get annoying.

Another caveat is the odd question of "when will the ephemeral text disappear?" If you have a very short command like ls, the command won't over-write the date. But if you type a backspace at any time, the whole line gets redrawn and the date is gone. It's ephemeral. If you want a piece of information to remain on screen permanently, this isn't the right method to use.