Skip to content

Instantly share code, notes, and snippets.

@denhamparry
Last active March 12, 2020 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denhamparry/2ead4547799f5c9e10c18deb3e832e66 to your computer and use it in GitHub Desktop.
Save denhamparry/2ead4547799f5c9e10c18deb3e832e66 to your computer and use it in GitHub Desktop.
Dotfiles (To the tune of Duck Tails)
#!/bin/bash
# Check for various OS openers. Quit as soon as we find one that works.
for opener in browser-exec xdg-open cmd.exe cygstart "start" open; do
if command -v $opener >/dev/null 2>&1; then
if [[ "$opener" == "cmd.exe" ]]; then
# shellcheck disable=SC2139
alias open="$opener /c start";
else
# shellcheck disable=SC2139
alias open="$opener";
fi
break;
fi
done
# Easy Source
alias sourced="source ~/.bashrc"
# Easier navigation: .., ..., ...., ....., ~ and -
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ~="cd ~"
alias -- -="cd -"
# Shortcuts
alias dl="cd ~/Downloads"
alias g="git"
alias h="history"
# Git
alias g="git "
alias ga="git add -A"
alias gamend="git add -A && git commit --amend --no-edit "
alias gcc="git commit --amend -v "
alias gco="git checkout"
alias gf="git fetch --all && git rebase origin/master"
alias gc=". /usr/local/bin/gitdate && git commit -v "
alias gs="git status"
alias gp="git push"
# Detect which `ls` flavor is in use
if ls --color > /dev/null 2>&1; then # GNU `ls`
colorflag="--color"
else # OS X `ls`
colorflag="-G"
fi
# List all files colorized in long format
# shellcheck disable=SC2139
alias l="ls -lhF ${colorflag}"
# List all files colorized in long format, including dot files
# shellcheck disable=SC2139
alias la="ls -lahF ${colorflag}"
# List only directories
# shellcheck disable=SC2139
alias lsd="ls -lhF ${colorflag} | grep --color=never '^d'"
# Always use color output for `ls`
# shellcheck disable=SC2139
alias ls="command ls ${colorflag}"
export LS_COLORS='no=00:fi=00:di=01;34: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:'
# Always enable colored `grep` output
alias grep='grep --color=auto '
# Enable aliases to be sudo’ed
alias sudo='sudo '
# Get week number
alias week='date +%V'
# Stopwatch
alias timer='echo "Timer started. Stop with Ctrl-D." && date && time cat && date'
# IP addresses
alias pubip="dig +short myip.opendns.com @resolver1.opendns.com"
alias localip="sudo ifconfig | grep -Eo 'inet (addr:)?([0-9]*\\.){3}[0-9]*' | grep -Eo '([0-9]*\\.){3}[0-9]*' | grep -v '127.0.0.1'"
alias ips="sudo ifconfig -a | grep -o 'inet6\\? \\(addr:\\)\\?\\s\\?\\(\\(\\([0-9]\\+\\.\\)\\{3\\}[0-9]\\+\\)\\|[a-fA-F0-9:]\\+\\)' | awk '{ sub(/inet6? (addr:)? ?/, \"\"); print }'"
# Canonical hex dump; some systems have this symlinked
command -v hd > /dev/null || alias hd="hexdump -C"
# OS X has no `md5sum`, so use `md5` as a fallback
command -v md5sum > /dev/null || alias md5sum="md5"
# URL-encode strings
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1]);"'
# Intuitive map function
# For example, to list all directories that contain a certain file:
# find . -name .gitattributes | map dirname
alias map="xargs -n1"
# One of @janmoesen’s ProTip™s
for method in GET HEAD POST PUT DELETE TRACE OPTIONS; do
# shellcheck disable=SC2139,SC2140
alias "$method"="lwp-request -m \"$method\""
done
# Kill all the tabs in Chrome to free up memory
# [C] explained: http://www.commandlinefu.com/commands/view/402/exclude-grep-from-your-grepped-output-of-ps-alias-included-in-description
alias chromekill="ps ux | grep '[C]hrome Helper --type=renderer' | grep -v extension-process | tr -s ' ' | cut -d ' ' -f2 | xargs kill"
# vhosts
alias hosts='sudo vim /etc/hosts'
# copy file interactive
alias cp='cp -i'
# move file interactive
alias mv='mv -i'
# untar
alias untar='tar xvf'
# Pipe my public key to my clipboard.
alias pubkey="more ~/.ssh/id_rsa_yubikey.pub"
# Check yourself before you wreck yourself
alias rm='rm -i'
# Set default IDE
alias ide="code-insiders"
# Access dotfiles
alias dotfiles="ide ~/dotfiles"
# Personal projects
alias notes-denhamparry="ide ~/git/denhamparry/denhamparry"
# Control Plane projects
alias notes-controlplane="ide ~/git/denhamparry/controlplane"
alias cp-sec-test="ide ~/git/controlplane/cp-sec-test"
# Create temp directory
alias tmp=' cd $(mktemp -d)'
# Terraform
alias tf='terraform'
alias tfi='terraform init'
alias tfp='terraform plan'
alias tfpd='terraform plan -destroy'
alias tfa='terraform apply'
alias tfaaa='terraform apply -auto-approve'
alias tfs='terraform show'
alias tfd='terraform destroy'
alias tfdaa='terraform destroy -auto-approve'
alias tft='terraform taint'
alias tfut='terraform untaint'
alias tfg='terraform graph'
alias tfgd='terraform graph > graph.dot'
alias tfimport='terraform import'
# Notifications
alias notify-off='gsettings set org.gnome.desktop.notifications show-banners false'
alias notify-on='gsettings set org.gnome.desktop.notifications show-banners true'
#!/bin/bash
# Simple calculator
calc() {
local result=""
result="$(printf "scale=10;%s\\n" "$*" | bc --mathlib | tr -d '\\\n')"
if [[ "$result" == *.* ]]; then
printf "%s" "$result" |
sed -e 's/^\./0./' \
-e 's/^-\./-0./' \
-e 's/0*$//;s/\.$//'
else
printf "%s" "$result"
fi
printf "\\n"
}
# Create a new directory and enter it
mkd() {
mkdir -p "$@"
cd "$@" || exit
}
# Decrypt a file
reveal() {
output=$(echo "${1}" | rev | cut -c16- | rev)
gpg --decrypt --output ${output} "${1}" \
&& echo "${1} -> ${output}"
}
# Encrypt a file
secret() {
output=~/"${1}".$(date +%s).enc
gpg --encrypt --armor \
--output ${output} \
-r 0x8C649FA0D2396E42 \
-r lewis@control-plane.io \
"${1}" && echo "${1} -> ${output}"
}
# Encrypt a file in same directory
secrethere() {
output="${1}".$(date +%s).enc
gpg --encrypt --armor \
--output ${output} \
-r 0x8C649FA0D2396E42 \
-r lewis@control-plane.io \
"${1}" && echo "${1} -> ${output}"
}
# check if uri is up
isup() {
local uri=$1
if curl -s --head --request GET "$uri" | grep "200 OK" > /dev/null ; then
notify-send --urgency=critical "$uri is down"
else
notify-send --urgency=low "$uri is up"
fi
}
# Check if uri is up
isup() {
local uri=$1
if curl -s --head --request GET "$uri" | grep "200 OK" > /dev/null ; then
notify-send --urgency=critical "$uri is down"
else
notify-send --urgency=low "$uri is up"
fi
}
# Get colors in manual pages
man() {
env \
LESS_TERMCAP_mb="$(printf '\e[1;31m')" \
LESS_TERMCAP_md="$(printf '\e[1;31m')" \
LESS_TERMCAP_me="$(printf '\e[0m')" \
LESS_TERMCAP_se="$(printf '\e[0m')" \
LESS_TERMCAP_so="$(printf '\e[1;44;33m')" \
LESS_TERMCAP_ue="$(printf '\e[0m')" \
LESS_TERMCAP_us="$(printf '\e[1;32m')" \
man "$@"
}
# Call from a local repo to open the repository on github/bitbucket in browser
# Modified version of https://github.com/zeke/ghwd
repo() {
# Figure out github repo base URL
local base_url
base_url=$(git config --get remote.origin.url)
base_url=${base_url%\.git} # remove .git from end of string
# Fix git@github.com: URLs
base_url=${base_url//git@github\.com:/https:\/\/github\.com\/}
# Fix git://github.com URLS
base_url=${base_url//git:\/\/github\.com/https:\/\/github\.com\/}
# Fix git@bitbucket.org: URLs
base_url=${base_url//git@bitbucket.org:/https:\/\/bitbucket\.org\/}
# Fix git@gitlab.com: URLs
base_url=${base_url//git@gitlab\.com:/https:\/\/gitlab\.com\/}
# Validate that this folder is a git folder
if ! git branch 2>/dev/null 1>&2 ; then
echo "Not a git repo!"
exit $?
fi
# Find current directory relative to .git parent
full_path=$(pwd)
git_base_path=$(cd "./$(git rev-parse --show-cdup)" || exit 1; pwd)
relative_path=${full_path#$git_base_path} # remove leading git_base_path from working directory
# If filename argument is present, append it
if [ "$1" ]; then
relative_path="$relative_path/$1"
fi
# Figure out current git branch
# git_where=$(command git symbolic-ref -q HEAD || command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
git_where=$(command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
# Remove cruft from branchname
branch=${git_where#refs\/heads\/}
[[ $base_url == *bitbucket* ]] && tree="src" || tree="tree"
url="$base_url/$tree/$branch$relative_path"
echo "Calling $(type open) for $url"
open "$url" &> /dev/null || (echo "Using $(type open) to open URL failed." && exit 1);
}
# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression
targz() {
local tmpFile="${1%/}.tar"
tar -cvf "${tmpFile}" --exclude=".DS_Store" "${1}" || return 1
size=$(
stat -f"%z" "${tmpFile}" 2> /dev/null; # OS X `stat`
stat -c"%s" "${tmpFile}" 2> /dev/null # GNU `stat`
)
local cmd=""
if (( size < 52428800 )) && hash zopfli 2> /dev/null; then
# the .tar file is smaller than 50 MB and Zopfli is available; use it
cmd="zopfli"
else
if hash pigz 2> /dev/null; then
cmd="pigz"
else
cmd="gzip"
fi
fi
echo "Compressing .tar using \`${cmd}\`…"
"${cmd}" -v "${tmpFile}" || return 1
[ -f "${tmpFile}" ] && rm "${tmpFile}"
echo "${tmpFile}.gz created successfully."
}
# Determine size of a file or total size of a directory
fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh
else
local arg=-sh
fi
# shellcheck disable=SC2199
if [[ -n "$@" ]]; then
du $arg -- "$@"
else
du $arg -- .[^.]* *
fi
}
# Use Git’s colored diff when available
if hash git &>/dev/null ; then
diff() {
git diff --no-index --color-words "$@"
}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment