Skip to content

Instantly share code, notes, and snippets.

@maxrothman
Last active August 23, 2017 11:46
Show Gist options
  • Save maxrothman/0eb15db7365e2e55a5f5 to your computer and use it in GitHub Desktop.
Save maxrothman/0eb15db7365e2e55a5f5 to your computer and use it in GitHub Desktop.
bash tips & one-liners
# Reset
NoColor='\[\e[m\]' # Text Reset
# Regular Colors
Black='\[\e[0;30m\]' # Black
Red='\[\e[0;31m\]' # Red
Green='\[\e[0;32m\]' # Green
Yellow='\[\e[0;33m\]' # Yellow
Blue='\[\e[0;34m\]' # Blue
Purple='\[\e[0;35m\]' # Purple
Cyan='\[\e[0;36m\]' # Cyan
White='\[\e[0;37m\]' # White
# Bold
BBlack='\[\e[1;30m\]' # Black
BRed='\[\e[1;31m\]' # Red
BGreen='\[\e[1;32m\]' # Green
BYellow='\[\e[1;33m\]' # Yellow
BBlue='\[\e[1;34m\]' # Blue
BPurple='\[\e[1;35m\]' # Purple
BCyan='\[\e[1;36m\]' # Cyan
BWhite='\[\e[1;37m\]' # White
# Underline
UBlack='\[\e[4;30m\]' # Black
URed='\[\e[4;31m\]' # Red
UGreen='\[\e[4;32m\]' # Green
UYellow='\[\e[4;33m\]' # Yellow
UBlue='\[\e[4;34m\]' # Blue
UPurple='\[\e[4;35m\]' # Purple
UCyan='\[\e[4;36m\]' # Cyan
UWhite='\[\e[4;37m\]' # White
# Background
On_Black='\[\e[40m\]' # Black
On_Red='\[\e[41m\]' # Red
On_Green='\[\e[42m\]' # Green
On_Yellow='\[\e[43m\]' # Yellow
On_Blue='\[\e[44m\]' # Blue
On_Purple='\[\e[45m\]' # Purple
On_Cyan='\[\e[46m\]' # Cyan
On_White='\[\e[47m\]' # White
# High Intensity
IBlack='\[\e[0;90m\]' # Black
IRed='\[\e[0;91m\]' # Red
IGreen='\[\e[0;92m\]' # Green
IYellow='\[\e[0;93m\]' # Yellow
IBlue='\[\e[0;94m\]' # Blue
IPurple='\[\e[0;95m\]' # Purple
ICyan='\[\e[0;96m\]' # Cyan
IWhite='\[\e[0;97m\]' # White
# Bold High Intensity
BIBlack='\[\e[1;90m\]' # Black
BIRed='\[\e[1;91m\]' # Red
BIGreen='\[\e[1;92m\]' # Green
BIYellow='\[\e[1;93m\]' # Yellow
BIBlue='\[\e[1;94m\]' # Blue
BIPurple='\[\e[1;95m\]' # Purple
BICyan='\[\e[1;96m\]' # Cyan
BIWhite='\[\e[1;97m\]' # White
# High Intensity backgrounds
On_IBlack='\[\e[0;100m\]' # Black
On_IRed='\[\e[0;101m\]' # Red
On_IGreen='\[\e[0;102m\]' # Green
On_IYellow='\[\e[0;103m\]' # Yellow
On_IBlue='\[\e[0;104m\]' # Blue
On_IPurple='\[\e[0;105m\]' # Purple
On_ICyan='\[\e[0;106m\]' # Cyan
On_IWhite='\[\e[0;107m\]' # White
#move user executables to beginning of path
grep -q '/usr/local/bin' <<< "$PATH" &&
export PATH=`echo "$PATH" | sed -E 's|(.*):/usr/local/bin(.*)|/usr/local/bin:\1\2|'`
#for coreutils and gnu-sed
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
export MANPATH="/usr/local/opt/gnu-sed/libexec/gnuman:$MANPATH"
export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:$PATH"
#add timestamps to bash history
export HISTTIMEFORMAT='%F %T '
#enable shell colors (ls and others)
#from https://github.com/seebi/dircolors-solarized
eval `dircolors ~/.dircolors`
#add syntax highlighting to less via gnu source-highlight
export LESSOPEN="| `brew --prefix`/bin/src-hilite-lesspipe.sh %s"
export LESS=' -R '
#extended source-highlight definitions (see src-hilite-lesspipe.sh)
export SOURCE_HIGHLIGHT_BASH_EXT=".bashrc .profile"
alias grep='grep --color'
alias ls='ls --color'
alias ll='ls -lArth'
alias la='ls -A'
alias rm=trash
#growl() {
# echo -ne "\e]9;${*}\007"
#}
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
source ~/.git-completion.sh
if [ -f $(brew --prefix)/etc/autojump.bash ]; then
. $(brew --prefix)/etc/autojump.bash
fi
if [ -f $(brew --prefix)/etc/bash_completion.d/vagrant ]; then
. $(brew --prefix)/etc/bash_completion.d/vagrant
fi
complete -C '/usr/local/bin/aws_completer' aws
export WORKON_HOME=~/.virtualenvs
export PROJECT_HOME=~
export VIRTUAL_ENV_DISABLE_PROMPT=1
source /usr/local/bin/virtualenvwrapper.sh
export ANSIBLE_NOCOWS=1
source ~/.bash_colors
#Handy git aliases
lastm() { git --no-pager log --merges -n1 --format='%H'; }
lastp() { git --no-pager rev-parse "@{u}"; }
alias gitcd='cd $(git rev-parse --show-toplevel)'
#-- Prompt --
shorten_path () {
#arg: path to shorten (defaults to PWD)
path="${1:-$PWD}"
shortened=$(echo "${path/#$HOME/\~}" | awk -F "/" '
{if (length($0) > 35)
{if (NF>4)
print $1 "/" $2 "/.../" $(NF-1) "/" $NF;
else if (NF>3)
print $1 "/" $2 "/.../" $NF;
else
print $1 "/.../" $NF;
} else
print $0;}
')
echo "${BGreen}${shortened}${NoColor}"
}
user_at_loc () {
echo "${BBlue}\u${NoColor}@${BPurple}\h${NoColor}"
}
git_prompt () {
git_branch=$(git symbolic-ref HEAD 2>&1)
r=$?
if [ "$git_branch" = "fatal: ref HEAD is not a symbolic ref" ]; then
echo "${Red}???${NoColor} "
elif [ $r -eq 0 ]; then
git_branch="${git_branch#refs/heads/}"
s=`/usr/bin/git status`
if grep -q "ahead of" <<<"$s"; then
symbol="+"
elif grep -q "behind" <<<"$s"; then
symbol="-"
elif grep -q "diverged" <<<"$s"; then
symbol='!!'
fi
if grep -q "nothing to commit" <<<"$s"; then
git_branch="${Green}($git_branch)${NoColor}"
elif grep -q -E "Untracked files|not staged" <<<"$s"; then
git_branch="${Yellow}($git_branch)${NoColor}"
elif grep -q "Changes to be committed" <<<"$s"; then
git_branch="${Red}($git_branch)${NoColor}"
fi
[ -n "$git_branch" ] && echo "${BYellow}${symbol}${NoColor}${git_branch}"
fi
}
git_branch () {
toplevel=$(git rev-parse --show-toplevel)
echo "${BPurple}"$(basename "$toplevel")"${NoColor}"
}
git_path () {
toplevel="$(git rev-parse --show-toplevel)"
shortened=-$([ "$toplevel" != "$PWD" ] && shorten_path ${PWD#$toplevel})
echo "${BGreen}$shortened${NoColor}"
}
set_prompt () {
gitstuff=$(git_prompt)
if [ -n "$VIRTUAL_ENV" ]; then
venv="${Yellow}!${NoColor} "
else
venv=""
fi
if [ -n "$gitstuff" ]; then
prompt="$(git_branch) $(git_prompt): $(git_path)"
else
prompt="$(user_at_loc): $(shorten_path)"
fi
export PS1="${venv}${prompt} ${BBlue}\$${NoColor} "
}
PROMPT_COMMAND="$PROMPT_COMMAND; set_prompt"
export SUDO_PS1="${IWhite}${On_Red}\u@\h: \W#${NoColor} "
# Use https://github.com/huyng/bashmarks and/or https://github.com/joelthelion/autojump
# for autojump, remember to source its .bash file
# This is Git's per-user configuration file.
[core]
ignorecase = false
[branch]
autosetupmerge = true
autosetuprebase = always
[push]
default = simple
[alias]
s = status
#pretty git log
lg = log --graph --format=format:'%C(red)%h%C(reset) - %C(green)(%ar)%C(reset) %C(bold blue)%an%C(reset)%C(yellow)%d%C(reset)%n %s'
#push a local branch to the remote
pushb = !git push --set-upstream origin $(git symbolic-ref --short HEAD)
#put all unpushed work on a new branch and reset this one
switch = "!f() { [ `git rev-list @{u}.. | wc -l` = 0 ] && return; [ -z $1 ] && echo branch name required && return 1; git branch $1 && git reset --hard @{u} && git checkout $1; }; f"
cmal = commit -a -m
cmend = commit -a --amend --no-edit
#open a pull request in github:
# git pr: open a PR from the current branch against master
# git pr <refname>: open a pr from the current branch against refname
# git pr <ref1> <ref2>: open a pr from ref2 against ref1
# Keep in mind that all refnames are from the remote's perspective, so "git pr master HEAD^" will use origin's head, aka the tip of master.
pr = "!f() { open https://github.com/$(git config --get remote.origin.url | sed -r 's|^.*[:/]([^/]+/[^/]+).git$|\\1|')/compare/${1-master}...${2-$(git symbolic-ref HEAD &> /dev/null && git rev-parse --abbrev-ref HEAD)}?expand=1; }; f"
#Usage: git merged <ref1> [ref2]
# Show whether ref 1 is merged into ref2 (default HEAD)
merged = "!f() { git merge-base --is-ancestor $1 ${2-HEAD} && echo \"$1 is merged into ${2-HEAD}\" && return 0 || echo \"$1 is NOT merged into ${2-HEAD}\" && return 1; }; f"
#Delete all local branches that have been merged into master (as determined by the "git merged" alias)
cleanup = "!git for-each-ref --format '%(refname)' refs/heads | grep -v 'master\\|release' | while read ref; do git merged $ref master >/dev/null && git branch -D ${ref#refs/heads/}; done"
ch = checkout
changed = !git --no-pager diff --name-status
logs = log --name-status
delbranch = push origin --delete
[user]
name = Your Name Here
email = your@email.here
[fetch]
prune = true #WARNING: will remove local branches if they've been deleted from the remote
[init]
#put a hooks directory in wherever this points to and they'll all get copied
#into new cloned repos. MAKE SURE THEY'RE ALL EXECUTABLE!
templatedir = ~/repos/.git-template
set completion-ignore-case on "Case-insensitive tab complete
"\e[A": history-search-backward "Search through history based on what you've already typed
"\e[B": history-search-forward " Bound to up and down arrows
"\e[1;9C": forward-word "Enable alt-forward and alt-back word skipping
"\e[1;9D": backward-word
#Makes iTerm scroll in bufferes with vim/less/etc. (nightly builds only)
defaults write com.googlecode.iterm2 AlternateMouseScroll -bool true
#!/bin/bash
#Pre-push hook
# Install: put in .git/hooks/ (or add to existing pre-push file there)
# Warns about pushing to master, prompts user to switch to another branch
# Dependencies: pushb, pr, and switch aliases (see ~/.gitconfig)
if [ "`git symbolic-ref --short HEAD`" == 'master' ]; then
i=0
while read stuff; do
((i += 1))
done
[ $i -le 0 ] && exit #no refs pushed, nothing to do
echo "You are pushing to MASTER! Are you sure you want to continue?"
echo "Enter the name of a new branch to switch all of your commits to, or if you're sure,"
echo "enter "'"'"I'm sure I want to push to master"'"'" (or CTRL+C to exit)"
exec < /dev/tty #see http://stackoverflow.com/questions/3417896/how-do-i-prompt-the-user-from-within-a-commit-msg-hook
read -p '> ' input
[ "$input" = "I'm sure I want to push to master" ] && exit 0
git switch $input
git pushb
sleep .1 #github takes a sec to register the branch
git pr
exit 1 #blocks original push
fi
#! /bin/sh
# My extensions to the standard src-hilite-lesspipe.sh provided by gnu source-highlight
# Adds an environment variable SOURCE_HIGHLIGHT_BASH_EXT that defines additional specific files
# that should be highlighted as bash
for source in "$@"; do
case $source in
*ChangeLog|*changelog)
source-highlight --failsafe -f esc --lang-def=changelog.lang --style-file=esc.style -i "$source" ;;
*Makefile|*makefile)
source-highlight --failsafe -f esc --lang-def=makefile.lang --style-file=esc.style -i "$source" ;;
*)
grep -q `basename $source` <<< $SOURCE_HIGHLIGHT_BASH_EXT &&
source-highlight --failsafe -f esc --lang-def=sh.lang --style-file=esc.style -i "$source"
source-highlight --failsafe --infer-lang -f esc --style-file=esc.style -i "$source"
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment