The Linux Page

Changing the command line prompt in your terminal

The prompt is generally the name of the user, the name of the computer, the path, and then a character such as $ or #.

What's missing to the default prompt, to my point of view, is time. It's quite often that I like to know when I ran a certain command. So I can do enter and then run the command. The first enter prints the current date (otherwise you have the date when you last ran a command...)

Bash

For bash I use the following:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \D{%Y/%m/%d} \t \$ '

So I added the:

\D{%Y/%m/%d} \t

The \D is for the date and I specify the format so it has the year first, which makes more sense to me.

The \t is for the time (the usual HH:MM:SS).

The one thing I think is better in zsh (see below) is the color specification. Having to use the escape commands as in here is rather cryptic.

Zsh

For zsh, since I hav a mac too, I changed the prompt like so:

export PS1="%F{green}%n@mac %F{blue}%~ %F{black}%D %T %# "

The mac as a weird hostname which I do not care, just knowing that's the mac is sufficient to me (I can always use hostname to get the name which I pretty much never use).

My main changes are the %~, because I like to have the whole path rather than just the end of it (i.e. the default %1~ means only the last path segment is shown; you could also increase the count with say %5~ to get five segments).

Like above, I also added the date and time. Somehow, in zsh, the %D prints the date the way I like it! So no specific formatting necessary.