PS1 prompt #
The default prompt for a use looks like this:
username@homepage:~$ ls -lWhich gets unwieldy when using deep paths:
username@homepage:~/some/very/very/very/very/very/very/very/very/very/very/very/very/long/path$ ls -lrestructure the prompt #
The user has ever increasing problems keeping their command line in check. Form old HP-UX prompts I retained the following prompt to fix this:
PS1="\u@\h [\w]
# "This turns the prompt into:
username@homepage [~/some/very/very/very/very/very/very/very/very/very/very/very/very/long/path]
# ls -lAdding a timestamp #
Adding a timestamp:
PS1="\u@\h \T [\w]
# "username@homepage 10:00:56 [~/some/very/very/very/very/very/very/very/very/very/very/very/very/long/path]
# ls -lAdding git #
A very useful element in your prompt is the current git branch (if any). Adding the following to out prompt code achieves this:
__git_ps1() {
local printf_format="${1:- (%s)}"
local branch
branch="$(git symbolic-ref --short HEAD 2>/dev/null)" || {
branch="$(git rev-parse --short HEAD 2>/dev/null)" || return 0
}
printf -- "$printf_format" "$branch"
}
update_ps1() {
PS1="\u@\h \T$(__git_ps1 " (%s)") [\w]
# "
}
shopt -u promptvars
PROMPT_COMMAND=update_ps1results in:
username@homepage 10:12:55 (main) [~/some/very/very/very/very/very/very/very/very/very/very/very/very/long/path]
# ls -lColors #
The last part is to add colors.
PS1="\[^[[1;32m\]\u@\h\[^[[0m\] \T$(__git_ps1 " (%s)") [\[^[[1;33m\]\w\[^[[0m\]]
# "Unfortunately the prompt uses escape chars and I am too lazy to
figure out how to change the ^[ into readable code. So I just
use:
CTRL-V ESCFor each code. Making the prompt something like:
PS1="\[<ctrl-v>+ESC[1;32m\]\u@\h\[<ctrl-v>+ESC[0m\] \T$(__git_ps1 " (%s)") [\[<ctrl-v>+ESC[1;33m\]\w\[<ctrl-v>+ESC[0m\]]
# "A way to test this update:
eval "$(echo "dXBkYXRlX3BzMSgpIHsKICBQUzE9IlxbG1sxOzMybVxdXHVAXGhcWxtbMG1cXSBcVCQoX19naXRfcHMxICIgKCVzKSIpIFtcWxtbMTszM21cXVx3XFsbWzBtXF1dCiMgIgp9Cg==" | base64 -d)"The string above is just a base64 encode version of:
update_ps1() {
PS1="\[[1;32m\]\u@\h\[[0m\] \T$(__git_ps1 " (%s)") [\[[1;33m\]\w\[[0m\]]
# "
}We need the function otherwise we’ll never see the update and base64 is one of the easiest ways to coerce none text content into something transferable.
The result should look something like this:
