-
-
Save concise/7641557 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A handy j() function, printing some useful info in bash cmdline | |
j() { | |
printf '\n\e[0;35m%(%Y/%m/%d (%a) %H:%M:%S)T\e[0m ' -1 | |
printf '\n\e[0;33m%s@%s\e[0m ' "$USER" "${HOSTNAME/.*/}" | |
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then | |
local GIT_CLEAN= | |
local GIT_REF=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) | |
git diff-index --quiet HEAD && GIT_CLEAN=1 | |
if [ "$GIT_CLEAN" ]; then | |
printf '\e[0;32mgit:%s(clean) ' "$GIT_REF" | |
else | |
printf '\e[0;31mgit:%s(dirty) ' "$GIT_REF" | |
fi | |
fi | |
printf '\e[0;34m' | |
} # end of function j() | |
# Simple bash prompt | |
PS1='$PWD \$ \[\033[0m\]' | |
# Enable Control-O in readline bash | |
stty discard undef | |
# Bind Control-O to execute my j() bash function | |
bind -x '"\C-o": j' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is it?
This is just a simple illustration of the idea about how you can keep your bash prompt simple (and fast), and get more information only when you need some.
Demo
(I pressed Ctrl-O just before each colored prompt shows up)
Normally, the bash cmdline prompt is just the working directory name followed by a '$' character.
When I want to know more about what's going on, I press Control-O, and then the powerful colored prompt with customized information will show up as shown in the above image. You can change the
j()
function to meet your needs.FYI, I am using GNU bash version 4.2.45 (installed by Homebrew) on OS X for this demo.