Skip to content

Instantly share code, notes, and snippets.

@metachris
Last active December 31, 2023 15:05
Show Gist options
  • Save metachris/141e2bb21ac66868bdacdf43058d9b1b to your computer and use it in GitHub Desktop.
Save metachris/141e2bb21ac66868bdacdf43058d9b1b to your computer and use it in GitHub Desktop.
Bash setup & dotfiles
# Easier navigation: .., ..., ...., ....., ~ and -
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ~="cd ~" # `cd` is probably faster to type though
alias -- -="cd -"
# Open current path in VS Code
alias c="code-insiders ."
# v shows current git code hash + tag + number of commits since
# For example: v1.6.1-3-g3265e6d (3 commits after tag v1.6.1 with current hash 3265e6d)
alias v="git describe --tags --always --dirty="-dev""
# List all files colorized in long format
alias l="ls -lhF ${colorflag}"
# List all files colorized in long format, excluding . and ..
alias ll="ls -lAhF ${colorflag}"
# List only directories
alias lsd="ls -lF ${colorflag} | grep --color=never '^d'"
# Always use color output for `ls`
alias ls="command ls ${colorflag}"
# lt runs "make lint" and "make test"
alias lt="make lint && make test"
# Git
alias g="git"
alias ggo="git checkout"
alias gs='git status -sb'
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias gds='git diff --staged'
alias gl='git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'
alias gca="git commit -a --amend --reuse-message=HEAD"
alias gcn="git commit --no-verify"
alias gdp='git diff | less'
alias gb-current='git branch --show-current'
# Switch to common branch, pull and prune
alias g-main="git checkout main && git pull && git fetch -p"
alias g-dev="git checkout develop && git pull && git fetch -p"
# git+github: check out PR by number
alias ghpr='gh pr checkout'
# git: create a PR
alias pr-create='gh pr create -f'
alias pr-create-draft='gh pr create -f -d'
# Kubernetes
alias k="kubectl"
alias kg="k get"
alias kd="k describe"
alias kgp="kg pods"
alias kgpw="kg pods --watch"
alias kgd="kg deploy"
alias kgn="kg nodes"
alias kdp="kd pod"
alias kdd="kd deploy"
alias ktop="k top pod"
alias kexec="k exec -ti "
alias klog="k logs"
alias klogf="k logs -f"
alias klogf5m="k logs --since 5m -f"
alias klogf1m="k logs --since 1m -f"
alias klogf1s="k logs --since 1s -f"
alias klogprev="k logs --tail 40 --previous"
# Various
alias gr="grep -nir"
alias f="find ./ -name"
alias uuid='uuidgen | tr "[:upper:]" "[:lower:]"'
alias sshpwd="ssh -o PreferredAuthentications=password"
alias pubkey="cat ~/.ssh/id_rsa.pub"
alias pubkeycopy="cat ~/.ssh/id_rsa.pub | pbcopy"
# Get week number
alias week='date +%V'
# Stopwatch
alias timer='echo "Timer started. Stop with Ctrl-D." && date && time cat && date'
# Show IP
alias ip="dig +short myip.opendns.com @resolver1.opendns.com"
# Print each PATH entry on a separate line
alias path='echo -e ${PATH//:/\\n}'
# Empty the Trash on all mounted volumes and the main HDD.
# Also, clear Apple’s System Logs to improve shell startup speed.
# Finally, clear download history from quarantine. https://mths.be/bum
alias emptytrash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl; sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'delete from LSQuarantineEvent'"
# Show/hide hidden files in Finder
alias show="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
alias hide="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
# Hide/show all desktop icons (useful when presenting)
alias hidedesktop="defaults write com.apple.finder CreateDesktop -bool false && killall Finder"
alias showdesktop="defaults write com.apple.finder CreateDesktop -bool true && killall Finder"
# URL-encode strings
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1]);"'
# Merge PDF files, preserving hyperlinks
# Usage: `mergepdf input{1,2,3}.pdf`
alias mergepdf='gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=_merged.pdf'
# Load the shell dotfiles, and then some:
# ~/.path can be used to extend `$PATH`.
# ~/.bash_prompt sets up the prompt
# ~/.exports is various exports
# ~/.aliases and ~/.functions contain a bunch of useful helpers
# ~/.extra can be used for other settings you don’t want to commit.
for file in ~/.{path,bash_prompt,exports,aliases,functions,extra}; do
[ -r "$file" ] && [ -f "$file" ] && source "$file";
done;
unset file;
# Case-insensitive globbing (used in pathname expansion)
shopt -s nocaseglob;
# Append to the Bash history file, rather than overwriting it
shopt -s histappend;
# Autocorrect typos in path names when using `cd`
shopt -s cdspell;
# Enable some Bash 4 features when possible:
# * `autocd`, e.g. `**/qux` will enter `./foo/bar/baz/qux`
# * Recursive globbing, e.g. `echo **/*.txt`
for option in autocd globstar; do
shopt -s "$option" 2> /dev/null;
done;
# Enable homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
# Enable zoxide https://github.com/ajeetdsouza/zoxide
eval "$(zoxide init bash)"
# Enable fzf
[ -f ~/.fzf.bash ] && source ~/.fzf.bash
# bash- completion@2
[[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/etc/profile.d/bash_completion.sh"
# Enable git bash completion for `g`
if type __git_complete &> /dev/null; then
__git_complete g __git_main
fi;
# Add tab completion for SSH hostnames based on ~/.ssh/config, ignoring wildcards
[ -e "$HOME/.ssh/config" ] && complete -o "default" -o "nospace" -W "$(grep "^Host" ~/.ssh/config | grep -v "[?*]" | cut -d " " -f2- | tr ' ' '\n')" scp sftp ssh;
#!/usr/bin/env bash
# https://github.com/fotinakis/bashrc/blob/master/crazy-prompt.sh
# Color bash prompt that displays the last 40 characters of the current
# working directory.
# From https://wiki.archlinux.org/index.php/Color_Bash_Prompt
##################################################
# Fancy PWD display function
##################################################
# The home directory (HOME) is replaced with a ~
# The last pwdmaxlen characters of the PWD are displayed
# Leading partial directory names are striped off
# /home/me/stuff -> ~/stuff if USER=me
# /usr/share/big_dir_name -> ../share/big_dir_name if pwdmaxlen=20
##################################################
bash_prompt_command() {
# How many characters of the $PWD should be kept
local pwdmaxlen=40
# Indicate that there has been dir truncation
local trunc_symbol=".."
local dir=${PWD##*/}
pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
NEW_PWD=${PWD/#$HOME/\~}
local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
if [ ${pwdoffset} -gt "0" ]
then
NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
fi
}
bash_prompt() {
case $TERM in
xterm*|rxvt*)
local TITLEBAR='\[\033]0;\u:${NEW_PWD}\007\]'
;;
*)
local TITLEBAR=""
;;
esac
local NONE="\[\033[0m\]" # unsets color to term's fg color
# regular colors
local K="\[\033[0;30m\]" # black
local R="\[\033[0;31m\]" # red
local G="\[\033[0;32m\]" # green
local Y="\[\033[0;33m\]" # yellow
local B="\[\033[0;34m\]" # blue
local M="\[\033[0;35m\]" # magenta
local C="\[\033[0;36m\]" # cyan
local W="\[\033[0;37m\]" # white
local LB="\[\033[36;40m\]" # light-blue
# emphasized (bolded) colors
local EMK="\[\033[1;30m\]"
local EMR="\[\033[1;31m\]"
local EMG="\[\033[1;32m\]"
local EMY="\[\033[1;33m\]"
local EMB="\[\033[1;34m\]"
local EMM="\[\033[1;35m\]"
local EMC="\[\033[1;36m\]"
local EMW="\[\033[1;37m\]"
# background colors
local BGK="\[\033[40m\]"
local BGR="\[\033[41m\]"
local BGG="\[\033[42m\]"
local BGY="\[\033[43m\]"
local BGB="\[\033[44m\]"
local BGM="\[\033[45m\]"
local BGC="\[\033[46m\]"
local BGW="\[\033[47m\]"
local UC=$W # user's color
[ $UID -eq "0" ] && UC=$R # root's color
local HOSTNAME="\h"
#PS1="$TITLEBAR ${EMK}[${UC}\u${EMK}@${UC}\h ${EMB}\${NEW_PWD}${EMK}]${UC}\\$ ${NONE}"
PS1="${G}${HOSTNAME}:${LB}\${NEW_PWD}${W}${NONE}\\$ "
# without colors: PS1="[\u@\h \${NEW_PWD}]\\$ "
# extra backslash in front of \$ to make bash colorize the prompt
}
PROMPT_COMMAND=bash_prompt_command
bash_prompt
unset bash_prompt
#!/usr/bin/env bash
export EDITOR='vim';
export PYTHONIOENCODING='UTF-8';
# Increase Bash history size. Allow 32³ entries; the default is 500.
export HISTSIZE=;
export HISTFILESIZE="${HISTSIZE}";
# Omit duplicates and commands that begin with a space from history.
export HISTCONTROL='ignoreboth';
# Prefer US English and use UTF-8.
export LANG='en_US.UTF-8';
export LC_ALL='en_US.UTF-8';
# Avoid issues with `gpg` as installed via Homebrew.
# https://stackoverflow.com/a/42265848/96656
export GPG_TTY=$(tty);
# Detect which `ls` flavor is in use
if ls --color > /dev/null 2>&1; then # GNU `ls`
colorflag="--color"
export LS_COLORS='no=00:fi=00:di=01;31:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:'
else # macOS `ls`
colorflag="-G"
export LSCOLORS='BxBxhxDxfxhxhxhxhxcxcx'
fi
#!/usr/bin/env bash
# Create a new directory and enter it
function mcd() {
mkdir -p "$@" && cd "$_";
}
# Change working directory to the top-most Finder window location
function cdf() { # short for `cdfinder`
cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')";
}
# Push the current branch to git "origin" remote
function gitpb() {
branch=$( git rev-parse --abbrev-ref HEAD )
cmd="git push origin $branch"
echo $cmd
$cmd
}
# git push origin <current-branch> --no-verify --force
function gitpbf() {
branch=$( git rev-parse --abbrev-ref HEAD )
cmd="git push origin $branch --no-verify --force"
echo $cmd
$cmd
}
# Delete all git branches except current
function git-delete-branches {
branch=$( git rev-parse --abbrev-ref HEAD )
cmd="git branch | grep -v \"$branch\" | xargs git branch -D"
read -p "This will delete all branches except $branch. Are you sure? " -n 1 -r
echo $cmd
bash -c "$cmd"
}
# Set the current branch to track origin with the same branch name
function git-set-upstream {
branch=$( git rev-parse --abbrev-ref HEAD )
cmd="git branch -u origin/$branch"
echo $cmd
$cmd
}
# Copy the file contents to clipboard
function catcopy() {
cat $1 | pbcopy
}
# curl, but show only the headers
function curl-headers() {
curl -s -D - "$@" -o /dev/null
}
# curl and JSON prettify the response body
function cjq () {
curl -s "$@" | jq;
}
# Run bash in a docker container
function docker-bash() {
docker exec -it $1 /bin/bash
}
# Run docker container, mount current directory to /mnt/, and remove after stop
function docker-run() {
CMD="docker run --rm -it -w /mnt -v $(pwd):/mnt $@"
echo "$CMD"
$CMD
}
[alias]
# View abbreviated SHA, description, and history graph of the latest 20 commits.
l = log --pretty=oneline -n 20 --graph --abbrev-commit
# View the current working tree status using the short format.
s = status -s
# Show the diff between the latest commit and the current state.
d = !"git diff-index --quiet HEAD -- || clear; git --no-pager diff --patch-with-stat"
# `git di $number` shows the diff between the state `$number` revisions ago and the current state.
di = !"d() { git diff --patch-with-stat HEAD~$1; }; git diff-index --quiet HEAD -- || clear; d"
# Pull in remote changes for the current repository and all its submodules.
p = pull --recurse-submodules
# Clone a repository including all submodules.
c = clone --recursive
# Commit all changes.
ca = !git add -A && git commit -av
# Switch to a branch, creating it if necessary.
go = "!f() { git checkout -b \"$1\" 2> /dev/null || git checkout \"$1\"; }; f"
# Show verbose output about tags, branches or remotes
tags = tag -l
branches = branch --all
remotes = remote --verbose
# List aliases.
aliases = config --get-regexp alias
# Amend the currently staged files to the latest commit.
amend = commit --amend --reuse-message=HEAD
# Credit an author on the latest commit.
credit = "!f() { git commit --amend --author \"$1 <$2>\" -C HEAD; }; f"
# Interactive rebase with the given number of latest commits.
reb = "!r() { git rebase -i HEAD~$1; }; r"
# Remove the old tag with this name and tag the latest commit with it.
retag = "!r() { git tag -d $1 && git push origin :refs/tags/$1 && git tag $1; }; r"
# Find branches containing commit
fb = "!f() { git branch -a --contains $1; }; f"
# Find tags containing commit
ft = "!f() { git describe --always --contains $1; }; f"
# Find commits by source code
fc = "!f() { git log --pretty=format:'%C(yellow)%h %Cblue%ad %Creset%s%Cgreen [%cn] %Cred%d' --decorate --date=short -S$1; }; f"
# Find commits by commit message
fm = "!f() { git log --pretty=format:'%C(yellow)%h %Cblue%ad %Creset%s%Cgreen [%cn] %Cred%d' --decorate --date=short --grep=$1; }; f"
# Remove branches that have already been merged with main.
# a.k.a. ‘delete merged’
dm = "!git branch --merged | grep -v '\\*' | xargs -n 1 git branch -d"
# List contributors with number of commits.
contributors = shortlog --summary --numbered
# Show the user email for the current repository.
whoami = config user.email
# Show recently checked out branches from the least recent
rb = !git reflog | egrep -io \"moving from ([^[:space:]]+)\" | awk '{ print $3 }' | awk ' !x[$0]++' | egrep -v '^[a-f0-9]{40}$' | fzf | xargs git checkout
# Show current commit hash
hash = git rev-parse --short HEAD
# Get current version
v = git describe --tags --always --dirty=-dev
PATH="$HOME/bin:$PATH"
PATH="/usr/local/bin:$PATH"
PATH="$HOME/Tools:$PATH"
GOPATH="$HOME/go"
PATH="$GOPATH/bin:$PATH"
PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin/:$PATH"
PATH="/opt/homebrew/opt/findutils/libexec/gnubin:$PATH"
PATH="/opt/homebrew/opt/curl/bin:$PATH"
PATH="$HOME/.foundry/bin/:$PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment