Skip to content

Instantly share code, notes, and snippets.

@machl
Last active November 17, 2017 23:24
Show Gist options
  • Save machl/c0ce33b939cb86004f54 to your computer and use it in GitHub Desktop.
Save machl/c0ce33b939cb86004f54 to your computer and use it in GitHub Desktop.
dotfiles
## Thanks to https://dotfiles.github.io/
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
### EXPORTS ###
# PATH
export PATH=$PATH:/Users/machl/bin:/Applications/Postgres.app/Contents/Versions/latest/bin/
# Highlight section titles in manual pages.
export LESS_TERMCAP_md="${yellow}";
# Always enable colored `grep` output.
export GREP_OPTIONS='--color=auto';
### ALIASES ###
alias vlc="/Applications/VLC.app/Contents/MacOS/VLC"
# Shortcuts
alias m="cd ~/Movies/"
alias d="cd ~/Downloads/"
alias temp="cd ~/Downloads/temp/"
# Easier navigation: .., ..., ...., ....., ~
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ~="cd ~" # `cd` is probably faster to type though
# 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
alias ll="ls -lhF ${colorflag}"
#alias ll="ls -lahG"
# List all files colorized in long format, including dot files
alias la="ls -lahF ${colorflag}"
# List only directories
alias lsd="ls -lhF ${colorflag} | grep --color=never '^d'"
# Always use color output for `ls`
alias ls="command ls ${colorflag}"
# 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 ip="dig +short myip.opendns.com @resolver1.opendns.com"
alias localip="ipconfig getifaddr en0"
alias ips="ifconfig -a | grep -o 'inet6\? \(addr:\)\?\s\?\(\(\([0-9]\+\.\)\{3\}[0-9]\+\)\|[a-fA-F0-9:]\+\)' | awk '{ sub(/inet6? (addr:)? ?/, \"\"); print }'"
# Flush Directory Service cache
alias flush="dscacheutil -flushcache && killall -HUP mDNSResponder"
# OS X has no `md5sum`, so use `md5` as a fallback
alias md5sum="md5"
# OS X has no `sha1sum`, so use `shasum` as a fallback
alias sha1sum="shasum"
# Recursively delete `.DS_Store` files
#alias cleanup="find . -type f -name '*.DS_Store' -ls -delete"
# Clean up old XCode files
alias cleanup="xcrun simctl delete unavailable"
# Empty the Trash on all mounted volumes and the main HDD
alias emptytrash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash"
# Also, clear Apple’s System Logs to improve shell startup speed
# alias emptytrash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl"
# 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"
# Merge PDF files
# Usage: `mergepdf -o output.pdf input{1,2,3}.pdf`
alias mergepdf='/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py'
# Ring the terminal bell, and put a badge on Terminal.app’s Dock icon
# (useful when executing time-consuming commands)
alias notify="tput bel"
# Stuff I never really use but cannot delete either because of http://xkcd.com/530/
alias stfu="osascript -e 'set volume output muted true'"
alias pumpitup="osascript -e 'set volume 7'"
# 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"
# Lock the screen (when going AFK)
alias afk="/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend"
# Show disks space
alias freespace='df -h'
# Sudo previous command
alias please='sudo $(history -p \!\!)'
alias yolo=please
alias fuck='curl -s http://rage.metroserve.me/?format=plain'
#alias wget="curl -O --retry 999 --retry-max-time 0 -C -"
command -v wget > /dev/null || alias wget="curl -O --retry 999 --retry-max-time 0 -C -"
### FUNCTIONS ###
# Create a new directory and enter it
function mkd() {
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)')";
}
# Determine size of a file or total size of a directory
function fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh;
else
local arg=-sh;
fi
if [[ -n "$@" ]]; then
du $arg -- "$@";
else
du $arg .[^.]* *;
fi;
}
# Syntax-highlight JSON strings or files
# Usage: `json '{"foo":42}'` or `echo '{"foo":42}' | json`
# Installation on OS X: sudo easy_install Pygments
function json() {
if [ -t 0 ]; then # argument
python -mjson.tool <<< "$*" | pygmentize -l javascript;
else # pipe
python -mjson.tool | pygmentize -l javascript;
fi;
}
# Run `dig` and display the most useful info
function digga() {
dig +nocmd "$1" any +multiline +noall +answer;
}
# `s` with no arguments opens the current directory in Sublime Text, otherwise
# opens the given location
#function s() {
# if [ $# -eq 0 ]; then
# subl .;
# else
# subl "$@";
# fi;
#}
# `o` with no arguments opens the current directory, otherwise opens the given
# location
function o() {
if [ $# -eq 0 ]; then
open .;
else
open "$@";
fi;
}
# Extract many types of compress files
# Source: http://nparikh.org/notes/zshrc.txt
function extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar -jxvf "$1" ;;
*.tar.gz) tar -zxvf "$1" ;;
*.tar.xz) tar -xvf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.dmg) hdiutil mount "$1" ;;
*.gz) gunzip "$1" ;;
*.rar) unrar x "$1" ;; # brew install unrar
*.tar) tar -xvf "$1" ;;
*.tbz2) tar -jxvf "$1" ;;
*.tgz) tar -zxvf "$1" ;;
*.zip) unzip "$1" ;;
*.ZIP) unzip "$1" ;;
*.pax) cat "$1" | pax -r ;;
*.pax.Z) uncompress "$1" --stdout | pax -r ;;
*.Z) uncompress "$1" ;;
*) echo "'$1' cannot be extracted/mounted via extract()" ;;
esac
else
echo "'$1' is not a valid file to extract"
fi
}
# Download videos interactive queue
function youtube() {
while IFS= read -r line
do
params=""
# Detect youtube and set properties:
if [[ $line == *"youtube"* ]]
then
cd ~/Movies/
echo "Downloading to folder Movies"
params="-f bestvideo[ext=mp4]+bestaudio[ext=m4a] "
fi
# Do the fun:
echo "Downloading video: $line"
youtube-dl $params $line
echo "Download finished!"
done
echo "All videos downloaded!"
}
# Download files interactive queue
function multidownload() {
while IFS= read -r line
do
# display $line or do somthing with $line
echo "Downloading file: $line"
export ec=18; while [ $ec -eq 18 ]; do /usr/bin/curl -O -C - "$line"; export ec=$?; done
done
echo "Finished!"
}
### BASH TWEAKS ###
# PS1
# With git branch and status
function git_color {
local git_status="$(git status 2> /dev/null)"
if [[ ! $git_status =~ "working tree clean" ]]; then
echo -e "\033[2;31m" # Red
elif [[ $git_status =~ "Your branch is ahead of" ]]; then
echo -e "\033[2;33m" # Yellow
elif [[ $git_status =~ "nothing to commit" ]]; then
echo -e "\033[2;32m" # Dark Gray
else
echo -e "\033[0;37m" # White
fi
}
function git_branch {
local git_status="$(git status 2> /dev/null)"
local on_branch="On branch ([^${IFS}]*)"
local on_commit="HEAD detached at ([^${IFS}]*)"
if [[ $git_status =~ $on_branch ]]; then
local branch=${BASH_REMATCH[1]}
echo "($branch) "
elif [[ $git_status =~ $on_commit ]]; then
local commit=${BASH_REMATCH[1]}
echo "($commit) "
fi
}
export PS1="🍺 \[\e[0;32m\]\u@\h \[\e[0;33m\]\W \[\$(git_color)\]\$(git_branch)\[\e[0m\]\\$ "
# Colored (short pwd)
# export PS1="\[\e[00;32m\]\u@\h\[\e[0m\]\[\e[00;37m\] \[\e[0m\]\[\e[00;33m\]\W\[\e[0m\]\[\e[00;37m\] \\$ \[\e[0m\]"
# Colored (full pwd)
# export PS1="\[\e[00;32m\]\u@\h\[\e[0m\]\[\e[00;37m\] \[\e[0m\]\[\e[00;33m\]\w\[\e[0m\]\[\e[00;37m\] \\$ \[\e[0m\]"
# Grayscale
# export PS1="\[\e[00;37m\]\u@\h \w \\$ \[\e[0m\]"
# 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;
# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# http://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
export HISTCONTROL=ignoredups
# 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;
# Backward history search on up key press, and a forward one on down key
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
set show-all-if-ambiguous on
set completion-ignore-case on
### OTHER ###
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
export NVM_DIR="/Users/machl/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
export PATH="$HOME/.yarn/bin:$PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment