Skip to content

Instantly share code, notes, and snippets.

@lmintmate
Last active May 10, 2018 10:20
Show Gist options
  • Save lmintmate/26ca136e129529419a80a2b014641188 to your computer and use it in GitHub Desktop.
Save lmintmate/26ca136e129529419a80a2b014641188 to your computer and use it in GitHub Desktop.
My bash prompt

My bash prompt

I recently decided to see if I could create my own bash prompt, inspired by a short guide on making it like powerline. I did want to customize the prompt, but not for it to have the powerline look, so I decided to dig further. Here’s the result.

Bootstrap git status indication

Thanks to the aforementioned short guide, I found out that in the git repository there is a file that enables git prompt support in bash and zsh. The comments in the beginning of the file give setup and usage instructions and configurable parameters. I’ve set it up to show dirty state, untracked files and if there are differences between the repo and upstream.

# custom prompt
source $HOME/.git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE="True"
export GIT_PS1_SHOWUNTRACKEDFILES="True"
export GIT_PS1_SHOWUPSTREAM="auto"

Main prompt

This is my main prompt. It uses escape codes to define colors and styles. It uses bold green for the username and hostname (though I’m thinking of removing the hostname part), bold light blue for the directory path, regular yellow for the git status, bold default foreground color (white in my case) for the $, and in the end it resets the colors, so that the actual commands will show up with the default color and style. For more information, consult bash:tip_colors_and_formatting and bash:tip_customize_the_shell_prompt.

export PS1='\[\e[1;32m\]\u@\h \[\e[1;94m\]\w$(__git_ps1 "\[\e[21;33m\] %s") \[\e[1;39m\]\$ \[\e[0m\]'

Cleaner version with added newline

export PS1='\[\e[1;32m\]\u@\h \[\e[1;94m\]\w$(__git_ps1 "\[\e[21;33m\] %s") \[\e[1;39m\]\n\$ \[\e[0m\]'

A variation of the main prompt with condensed PWD

Adapted from Customizing the Terminal: The Prompt – Lindesk

PROMPT_COMMAND='DIR=`pwd|sed -e "s!$HOME!~!"`; if [ ${#DIR} -gt 30 ]; then CurDir=${DIR:0:12}...${DIR:${#DIR}-15}; else CurDir=$DIR; fi'
export PS1="\[\e[1;32m\]\u@\h \[\e[1;94m\]\$CurDir$(__git_ps1 "\[\e[21;33m\] %s") \[\e[1;39m\]\n\$ \[\e[0m\]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment