Skip to content

Instantly share code, notes, and snippets.

@toriaezunama
Last active March 29, 2018 21:50
Show Gist options
  • Save toriaezunama/cde5308a3be80b00f087292822be15ac to your computer and use it in GitHub Desktop.
Save toriaezunama/cde5308a3be80b00f087292822be15ac to your computer and use it in GitHub Desktop.
Shell prompt
# Special
\e # an ASCII escape character (033)
\h # the hostname up to the first `.'
\H # the hostname
\j # the number of jobs currently managed by the shell
\l # the basename of the shell's terminal device name
\n # newline
\r # carriage return
\t # the current time in 24-hour HH:MM:SS format
\T # the current time in 12-hour HH:MM:SS format
\@ # the current time in 12-hour am/pm format
\A # the current time in 24-hour HH:MM format
\u # the username of the current user
\w # the current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM variable)
\W # the basename of the current working directory, with $HOME abbreviated with a tilde
\! # the history number of this command
\# # the command number of this command
\$ # if the effective UID is 0, a #, otherwise a $
\nnn # the character corresponding to the octal number nnn
\\ # a backslash
\[ # begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] # end a sequence of non-printing characters
# Colours
\033]01;31\] # pink
\033]00m\] # white
\033]01;36\] # bold green
\033]02;36\] # green
\033]01;34\] # blue
\033]01;33\] # bold yellow
# Ex 1.
PS1='[\033[00m\][\W]\$ '
# [ white ] [ working dir ] $
# Ex git - git status
# get current branch in git repo
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
echo "[${BRANCH}${STAT}]"
else
echo ""
fi
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " ${bits}"
else
echo ""
fi
}
export PS1="\`parse_git_branch\` "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment