Skip to content

Instantly share code, notes, and snippets.

@slice
Last active August 29, 2015 14:14
Show Gist options
  • Save slice/2ad701de36fbb79b2b02 to your computer and use it in GitHub Desktop.
Save slice/2ad701de36fbb79b2b02 to your computer and use it in GitHub Desktop.
My new PS1!

This is my new $PS1. I designed it myself, and (I will admit it), was a pain to setup, but I did it!

How as this made?

I was trying out powerline-shell when I saw that it was very slow on my system. Although it was easy to setup, it runs a Python script every time to generate a $PS1. It would take 0.5-1.0 seconds to run the script on my system to generate the $PS1, and that was bothering me. (It's great, though!)

Step 0: Pre-requirements

  • Git
  • Bash
  • Computer knowledge

Step 1: Installing Powerline-patched fonts

See those fancy symbols, such as the Git branch icon, and the tall arrows that separate the segments? Those are not ordinary Unicode characters, so you need a patched font in order to use this specific $PS1. Download one of the fonts (there are plenty) from the powerline/fonts repository and install one. The font will contain the necessary characters for the fancy prompt.

Step 2: Using it in your terminal

In order for the characters to display properly, you need to use the font in your terminal. In the default Ubuntu terminal, right click on the window and hover over Profiles. Then click Profile Preferences and choose the font. Make sure the font's name ends with "for Powerline" at the end, or else the characters will not display properly.

Step 3: Using in ~/.bashrc

Copy and paste this at the end of your ~/.bashrc. Then, close all active terminals and relaunch. Or, you can do . ~/.bashrc.

# Powerline-like prompt
# By SliceOfCode (@sliceofcode)

# ANSI constants.
# If your terminal doesn't support ANSI, then instead of nice colors
# you will see ugly text.
function __set_ansi_constants() {
	Res='\e[0m'    # Text Reset
	# Regular           Bold                Underline           High Intensity      BoldHigh Intens     Background          High Intensity Backgrounds
	Bla='\e[0;30m';     BBla='\e[1;30m';    UBla='\e[4;30m';    IBla='\e[0;90m';    BIBla='\e[1;90m';   On_Bla='\e[40m';    On_IBla='\e[0;100m';
	Red='\e[0;31m';     BRed='\e[1;31m';    URed='\e[4;31m';    IRed='\e[0;91m';    BIRed='\e[1;91m';   On_Red='\e[41m';    On_IRed='\e[0;101m';
	Gre='\e[0;32m';     BGre='\e[1;32m';    UGre='\e[4;32m';    IGre='\e[0;92m';    BIGre='\e[1;92m';   On_Gre='\e[42m';    On_IGre='\e[0;102m';
	Yel='\e[0;33m';     BYel='\e[1;33m';    UYel='\e[4;33m';    IYel='\e[0;93m';    BIYel='\e[1;93m';   On_Yel='\e[43m';    On_IYel='\e[0;103m';
	Blu='\e[0;34m';     BBlu='\e[1;34m';    UBlu='\e[4;34m';    IBlu='\e[0;94m';    BIBlu='\e[1;94m';   On_Blu='\e[44m';    On_IBlu='\e[0;104m';
	Pur='\e[0;35m';     BPur='\e[1;35m';    UPur='\e[4;35m';    IPur='\e[0;95m';    BIPur='\e[1;95m';   On_Pur='\e[45m';    On_IPur='\e[0;105m';
	Cya='\e[0;36m';     BCya='\e[1;36m';    UCya='\e[4;36m';    ICya='\e[0;96m';    BICya='\e[1;96m';   On_Cya='\e[46m';    On_ICya='\e[0;106m';
	Whi='\e[0;37m';     BWhi='\e[1;37m';    UWhi='\e[4;37m';    IWhi='\e[0;97m';    BIWhi='\e[1;97m';   On_Whi='\e[47m';    On_IWhi='\e[0;107m';
}

# UTF-8 Powerline constants. Contents in the strings below
# SHOULD LOOK WEIRD IF NOT USING A POWERLINE SUPPORTED FONT.
function __set_powerline_constants() {
	PBranch=""; PLine=""; PLock="";
	PRightArrow=""; PRightArrowOutline="";
	PLeftArrow=""; PLeftArrowOutline="";
}

# pl_segment
# Draws a segment
#
# pl_segment $TEXT_BG "TEXT" $ARROW_COL
function pl_segment() {
	echo -ne "${1} ${2} ${Res}${3}${PRightArrow}${Res}"
}

# pl_segment_hasafter
# Draws a segment, before a new segment.
#
# pl_segment_hasafter $TEXT_BG "TEXT" $ARROW_COL $NEXTSEGMENT_BG
function pl_segment_hasafter() {
	echo -ne "${1} ${2} ${Res}${3}${4}${PRightArrow}${Res}"
}

# Sets constants.
__set_ansi_constants
__set_powerline_constants

# Generate PS1 using PROMPT_COMMAND
export PROMPT_COMMAND=__prompt_command # gen ps1

# Generates PS1
function __prompt_command() {
	EXIT="$?" # Exit status of last command.
	PS1="" # Blank the PS1
	USER=`whoami` # Get the current user.
	BRANCH=`git symbolic-ref --short HEAD 2>/dev/null` # Current branch
	pwd=${PWD/#$HOME/\~} # Replace /home/user with ~.

	if [[ $BRANCH != "" ]]; then # Check if there is a branch in this directory.
		PS1+="\\[$(pl_segment_hasafter $On_Gre "${PBranch} ${BRANCH}" $Gre $On_Blu)\\]"
	fi

	PS1+="\\[$(pl_segment_hasafter $On_Blu "${USER}@${HOSTNAME}" $Blu $On_Cya)\\]" # Add user@hostname
	PS1+="\\[$(pl_segment $On_Cya "${pwd}" $Cya)\\]" # Add current directory
	PS1+=" " # Add blank space.
}

Features

Displays a sort of Powerline style PS1, with the branch described, if you are in an active Git repository. It's not the best, but it looks good. (In my opinion)

Goodbye!

Hope you enjoy my prompt! ~sliceofcode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment