During certain stages of our software development cycle, I find myself wondering exactly when I ran a given command – did I start that UI process or task before, or after, I ran that command line utility?

More often than not, I give up guessing and re-run both jobs just in case. Scroll-back helps to some extent, but it’s infuriating to see that you ran a command, yet still not know when you ran it.

In an attempt to improve on this, I’ve modified my prompt to include a right-aligned date, effectively adding a timestamp to the previous completed command:

If you’re interested in using this yourself, you can add all or some of the following to your ~/.bash_profile:

function right_aligned_date {
    current_date=\[$( date )\]
    width=$( tput cols )
    echo "$( printf %${width}s "${current_date}" )"
}

PS1="\[\033[0;37m\]\$( right_aligned_date )\[\033[0m\]\n\u@\h \W $ "

Update: Some utilities – such as Python’s virtualenv – will modify the $PS1 to add additional information about the current environment. This can break the date alignment used in the above example as it relies on spaces to position the date.

The better solution is to explicitly set the column start position as follows:

function right_aligned_date {
    current_date=$( date "+%H:%M:%S %Z" )
    column=$( expr $( tput cols ) - ${ #current_date } + 1 )
    printf "\e[${column}G${current_date}"
}