Skip to content

Instantly share code, notes, and snippets.

@fzero
Last active August 4, 2021 00:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fzero/1120265 to your computer and use it in GitHub Desktop.
Save fzero/1120265 to your computer and use it in GitHub Desktop.
My bash customizations with git-aware prompt and some aliases.
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
# Prompt goodness
#
# This is for my local machine, so I don't display the host to save space (I do on remote servers).
# It looks like this:
#
# ~/Code/some_dir(my-git-branch)$
#
# There are variations if there are untracked files (a + will appear), uncommited changes
# (branch colour turns yellow) and if you must pull/push (arrows will appear).
function parse_git_branch {
# Inits
GIT_BRANCH=""
GIT_UNTRACKED=""
GIT_CLEAN=""
GIT_REMOTE=""
# Match patterns
local branch_pattern="On branch ([^${IFS}]*)"
local remote_pattern="Your branch is (ahead|behind)"
local clean_pattern="working tree clean"
local untracked_pattern="Untracked files"
local diverge_pattern="Your branch and (.*) have diverged"
# Get git status
local git_status="$(git status 2> /dev/null)"
# Get branch
if [[ ${git_status} =~ ${branch_pattern} ]]; then
GIT_BRANCH="${BASH_REMATCH[1]}"
fi
# Check if our working dir is clean
if [[ ! ${git_status} =~ ${clean_pattern} ]]; then
GIT_CLEAN="dirty"
fi
# Check for untracked files
if [[ ${git_status} =~ ${untracked_pattern} ]]; then
GIT_UNTRACKED="+"
fi
# Check if we're ahead or behind
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
GIT_REMOTE="↑"
elif [[ ${BASH_REMATCH[1]} == "behind" ]]; then
GIT_REMOTE="↓"
fi
fi
# Check if we diverged
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
GIT_REMOTE="↕"
fi
}
function awesome_prompt {
local BLACK="\[\033[0;30m\]"
local BLACKBOLD="\[\033[1;30m\]"
local RED="\[\033[0;31m\]"
local REDBOLD="\[\033[1;31m\]"
local GREEN="\[\033[0;32m\]"
local GREENBOLD="\[\033[1;32m\]"
local YELLOW="\[\033[0;33m\]"
local YELLOWBOLD="\[\033[1;33m\]"
local BLUE="\[\033[0;34m\]"
local BLUEBOLD="\[\033[1;34m\]"
local PURPLE="\[\033[0;35m\]"
local PURPLEBOLD="\[\033[1;35m\]"
local CYAN="\[\033[0;36m\]"
local CYANBOLD="\[\033[1;36m\]"
local WHITE="\[\033[0;37m\]"
local WHITEBOLD="\[\033[1;37m\]"
case $TERM in
xterm*)
TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
*)
TITLEBAR=""
;;
esac
# Get git status and fill in status vars
parse_git_branch
GIT_PART=""
if [ "$GIT_BRANCH" != "" ]; then
# Different colours if working dir isn't clean
if [[ "$GIT_CLEAN" == "dirty" ]]; then
GIT_BRANCH_COLOR=$YELLOW
else
GIT_BRANCH_COLOR=$GREEN
fi
GIT_PART="$BLUE($GIT_BRANCH_COLOR$GIT_BRANCH$YELLOW$GIT_REMOTE$RED$GIT_UNTRACKED$BLUE)"
fi
HOSTPART="$BLUE\u@\h"
MAINPART="$CYAN\w$GIT_PART$WHITE\$ "
# Without hostname
export PS1="$TITLEBAR$MAINPART"
# With hostname:
# export PS1="$TITLEBAR$HOSTPART $MAINPART"
}
export PROMPT_DIRTRIM=1 # Shorten directory part
export PROMPT_COMMAND="awesome_prompt"
export PS2='> '
export PS4='+ '
# I like vi.
export EDITOR="vim"
# Appends to history, don't delete
shopt -s histappend
# Alias definitions.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
# Same thing, but for OS X with brew
if [ "$(which brew)" != "" ] && [ -f $(brew --prefix)/etc/bash_completion.d ]; then
. $(brew --prefix)/etc/bash_completion.d/*
fi
# Go stuff
export GOPATH="$HOME/Code/go"
# Postgres.app
export PGPATH="/Applications/Postgres.app/Contents/Versions/latest"
# Adjust path
ADDITIONAL_PATHS="/usr/local/bin /usr/local/sbin /usr/local/share/npm/bin $PGPATH/bin $GOPATH/bin $HOME/bin"
for p in $ADDITIONAL_PATHS; do
if [ -d $p ] ; then
PATH="$p:$PATH"
fi
done
# Path cleanup - remove duplicates
PATH=$(echo -n $PATH | tr ":" "\n" | awk ' !x[$0]++' | tr "\n" ":" | sed 's/:*$//')
# rbenv stuff
if which rbenv > /dev/null; then
eval "$(rbenv init -)"
fi
# nvm stuff
if [ -f $(brew --prefix nvm)/nvm.sh ]; then
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
fi
# pyenv stuff
if which pyenv > /dev/null; then
eval "$(pyenv init -)"
fi
### Aliases
## Generic
alias ssh="ssh -C"
alias scp="scp -C"
alias grep="grep --color=auto"
# ls is different between Linux and OSX. On linux, use:
alias ls="ls -F --color=auto"
# On OSX:
#alias ls="ls -GF"
alias l="ls"
alias ll="ls -lh"
alias la="ls -lah"
alias "ps?"="ps ax | grep -i"
alias "set?"="set | grep -i"
# Make pgrep/pkill smarted/more verbose and pkill safer
alias pgrep="pgrep -ilf"
alias pkill="pkill -Iilf"
## Ruby/Rails
alias be="bundle exec"
alias foc="foreman run rails console"
alias fos="foreman start"
alias mm="bundle exec middleman"
## Postgres
# Load database from dump file:
# pgrestore <database_name> <dump_file>
alias pgrestore="pg_restore --verbose --clean --no-acl --no-owner -h localhost -d"
# Dumps database
# pgdump <database_name> > <dump_file>
alias pgdump="pg_dump -Fc --no-acl --no-owner -h localhost"
## Tmux
# Attaches tmux to the last session; creates a new session if none exists.
alias t='tmux attach || tmux -2 new-session'
# Attaches tmux to a session (example: ta portal)
alias ta='tmux attach -t'
# Creates a new session
alias tn='tmux new-session'
# Lists all ongoing sessions
alias tl='tmux list-sessions'
## Utilities
alias quickweb='python -m SimpleHTTPServer'
alias flacandkill='flac --best --delete-input-file'
## OS X stuff
alias ql='qlmanage -p 2>/dev/null'
alias preview='groff -Tps > /tmp/tmp.ps && open -a Preview /tmp/tmp.ps'
# Open manpages on Preview
pman () {
man -t "${1}" | open -f -a /Applications/Preview.app
}
# Quits apps from the terminal
quit () {
for app in $*; do
osascript -e 'quit app "'$app'"'
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment