Skip to content

Instantly share code, notes, and snippets.

@hnq90
Last active May 18, 2025 05:17
Show Gist options
  • Save hnq90/2ea115818f0ffb359b0e to your computer and use it in GitHub Desktop.
Save hnq90/2ea115818f0ffb359b0e to your computer and use it in GitHub Desktop.
ZSH Alias
# Aliases in this file are bash and zsh compatible
alias -g ...='../..'
alias -g ....='../../..'
alias -g .....='../../../..'
alias -g C='| wc -l'
alias -g H='| head'
alias -g L="| less"
alias -g N="| /dev/null"
alias -g S='| sort'
alias -g G='| grep' # now you can do: ls foo G something
# PS
alias psa="ps aux"
alias psg="ps aux | grep "
alias psr='ps aux | grep ruby'
# Moving around
alias cdb='cd -'
alias cls='clear;ls'
# Show human friendly numbers and colors
alias df='df -h'
alias ll='ls -alGh'
alias ls='ls -Gh'
alias du='du -h -d 2'
# show me files matching "ls grep"
alias lsg='ll | grep'
# vimrc editing
alias ve='vim ~/.vimrc'
# zsh profile editing
alias ze='vim ~/.zshrc'
alias zr='source ~/.zshrc'
# Git Aliases
alias gs='git status'
alias gstsh='git stash'
alias gst='git stash'
alias gsp='git stash pop'
alias gsa='git stash apply'
alias gsh='git show'
alias gshw='git show'
alias gshow='git show'
alias gi='vim .gitignore'
alias gcm='git ci -m'
alias gcim='git ci -m'
alias gci='git ci'
alias gco='git co'
alias gcp='git cp'
alias ga='git add -A'
alias guns='git unstage'
alias gunc='git uncommit'
alias gm='git merge'
alias gms='git merge --squash'
alias gam='git amend --reset-author'
alias grv='git remote -v'
alias grr='git remote rm'
alias grad='git remote add'
alias gr='git rebase'
alias gra='git rebase --abort'
alias ggrc='git rebase --continue'
alias gbi='git rebase --interactive'
alias gl='git l'
alias glg='git l'
alias glog='git l'
alias co='git co'
alias gf='git fetch'
alias gfch='git fetch'
alias gd='git diff'
alias gb='git b'
alias gbd='git b -D -w'
alias gdc='git diff --cached -w'
alias gpub='grb publish'
alias gtr='grb track'
alias gpl='git pull'
alias gplr='git pull --rebase'
alias gps='git push'
alias gpsh='git push'
alias gnb='git nb' # new branch aka checkout -b
alias grs='git reset'
alias grsh='git reset --hard'
alias gcln='git clean'
alias gclndf='git clean -df'
alias gclndfx='git clean -dfx'
alias gsm='git submodule'
alias gsmi='git submodule init'
alias gsmu='git submodule update'
alias gt='git t'
alias gbg='git bisect good'
alias gbb='git bisect bad'
# Common shell functions
alias less='less -r'
alias tf='tail -f'
alias l='less'
alias lh='ls -alt | head' # see the last modified files
alias screen='TERM=screen screen'
alias cl='clear'
# Zippin
alias gz='tar -zcvf'
alias gzx='tar -xvf'
# Ruby
alias c='rails c' # Rails 3
alias co='script/console --irb=pry' # Rails 2
alias ts='thin start'
alias ms='mongrel_rails start'
alias tfdl='tail -f log/development.log'
alias tftl='tail -f log/test.log'
alias ka9='killall -9'
alias k9='kill -9'
# Gem install
alias sgi='sudo gem install --no-ri --no-rdoc'
# Forward port 80 to 3000
alias portforward='sudo ipfw add 1000 forward 127.0.0.1,3000 ip from any to any 80 in'
alias rdm='rake db:migrate'
alias rdmr='rake db:migrate:redo'
# Zeus
alias zs='zeus server'
alias zc='zeus console'
# Rspec
alias rs='rspec spec'
alias sr='spring rspec'
alias src='spring rails c'
alias srgm='spring rails g migration'
alias srdm='spring rake db:migrate'
alias srdt='spring rake db:migrate'
alias srdmt='spring rake db:migrate db:test:prepare'
# Sprintly - https://github.com/nextbigsoundinc/Sprintly-GitHub
alias sp='sprintly'
# spb = sprintly branch - create a branch automatically based on the bug you're working on
alias spb="git checkout -b \`sp | tail -2 | grep '#' | sed 's/^ //' | sed 's/[^A-Za-z0-9 ]//g' | sed 's/ /-/g' | cut -d"-" -f1,2,3,4,5\`"
alias hpr='hub pull-request'
alias grb='git recent-branches'
alias dbtp='spring rake db:test:prepare'
alias dbm='spring rake db:migrate'
alias dbmr='spring rake db:migrate:redo'
alias dbmd='spring rake db:migrate:down'
alias dbmu='spring rake db:migrate:up'
alias ip='ifconfig | grep eth'
###
# Utils Functions
###
answer_is_yes() {
[[ "$REPLY" =~ ^[Yy]$ ]] \
&& return 0 \
|| return 1
}
ask() {
print_question "$1"
read
}
ask_for_confirmation() {
print_question "$1 (y/n) "
read -n 1
printf "\n"
}
ask_for_sudo() {
# Ask for the administrator password upfront
sudo -v
# Update existing `sudo` time stamp until this script has finished
# https://gist.github.com/cowboy/3118588
while true; do
sudo -n true
sleep 60
kill -0 "$$" || exit
done &> /dev/null &
}
cmd_exists() {
[ -x "$(command -v "$1")" ] \
&& printf 0 \
|| printf 1
}
execute() {
$1 &> /dev/null
print_result $? "${2:-$1}"
}
get_answer() {
printf "$REPLY"
}
get_os() {
declare -r OS_NAME="$(uname -s)"
local os=""
if [ "$OS_NAME" == "Darwin" ]; then
os="osx"
elif [ "$OS_NAME" == "Linux" ] && [ -e "/etc/lsb-release" ]; then
os="ubuntu"
fi
printf "%s" "$os"
}
is_git_repository() {
[ "$(git rev-parse &>/dev/null; printf $?)" -eq 0 ] \
&& return 0 \
|| return 1
}
mkd() {
if [ -n "$1" ]; then
if [ -e "$1" ]; then
if [ ! -d "$1" ]; then
print_error "$1 - a file with the same name already exists!"
else
print_success "$1"
fi
else
execute "mkdir -p $1" "$1"
fi
fi
}
print_error() {
# Print output in red
printf "\e[0;31m [✖] $1 $2\e[0m\n"
}
print_info() {
# Print output in purple
printf "\n\e[0;35m $1\e[0m\n\n"
}
print_question() {
# Print output in yellow
printf "\e[0;33m [?] $1\e[0m"
}
print_result() {
[ $1 -eq 0 ] \
&& print_success "$2" \
|| print_error "$2"
[ "$3" == "true" ] && [ $1 -ne 0 ] \
&& exit
}
print_success() {
# Print output in green
printf "\e[0;32m [✔] $1\e[0m\n"
}
# Create a new directory and enter it
function md() {
mkdir -p "$@" && cd "$@"
}
# find shorthand
function f() {
find . -name "$1" 2>&1 | ag -v 'Permission denied'
}
# Copy w/ progress
cp_p () {
rsync -WavP --human-readable --progress $1 $2
}
# get gzipped size
function gz() {
echo "orig size (bytes): "
cat "$1" | wc -c
echo "gzipped size (bytes): "
gzip -c "$1" | wc -c
}
# whois a domain or a URL
function whois() {
local domain=$(echo "$1" | awk -F/ '{print $3}') # get domain from URL
if [ -z $domain ] ; then
domain=$1
fi
echo "Getting whois record for: $domain …"
# avoid recursion
# this is the best whois server
# strip extra fluff
/usr/bin/whois -h whois.internic.net $domain | sed '/NOTICE:/q'
}
# Extract archives - use: extract <file>
# Based on https://github.com/xvoland/Extract/blob/master/extract.sh
function extract {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
echo " extract <path/file_name_1.ext> [path/file_name_2.ext] [path/file_name_3.ext]"
return 1
else
for n in $@
do
if [ -f "$n" ] ; then
case "${n%,}" in
*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
tar xvf "$n" ;;
*.lzma) unlzma ./"$n" ;;
*.bz2) bunzip2 ./"$n" ;;
*.rar) unrar x -ad ./"$n" ;;
*.gz) gunzip ./"$n" ;;
*.zip) unzip ./"$n" ;;
*.z) uncompress ./"$n" ;;
*.7z) 7z x ./"$n" ;;
*.xz) unxz ./"$n" ;;
*.exe) cabextract ./"$n" ;;
*)
echo "extract: '$n' - unknown archive method"
return 1
;;
esac
else
echo "'$n' - file does not exist"
return 1
fi
done
fi
}
# Display error codes of last program
EC() {
echo -e '\e[1;33m'code $?'\e[m\n'
}
# IP info
ipif() {
if grep -P "(([1-9]\d{0,2})\.){3}(?2)" <<< "$1"; then
curl ipinfo.io/"$1"
else
ipawk=($(host "$1" | awk '/address/ { print $NF }'))
curl ipinfo.io/${ipawk[1]}
fi
echo
}
# animated gifs from any video
# from alex sexton gist.github.com/SlexAxton/4989674
gifify() {
if [[ -n "$1" ]]; then
if [[ $2 == '--good' ]]; then
ffmpeg -i $1 -r 10 -vcodec png out-static-%05d.png
time convert -verbose +dither -layers Optimize -resize 900x900\> out-static*.png GIF:- | gifsicle --colors 128 --delay=5 --loop --optimize=3 --multifile - > $1.gif
rm out-static*.png
else
ffmpeg -i $1 -s 600x400 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=3 > $1.gif
fi
else
echo "proper usage: gifify <input_movie.mov>. You DO need to include extension."
fi
}
# turn that video into webm.
webmify(){
ffmpeg -i $1 -vcodec libvpx -acodec libvorbis -isync -copyts -aq 80 -threads 3 -qmax 30 -y $2 $1.webm
}
function weather() {
local LOCATION=${1:-"Hanoi"}
curl wttr.in/$LOCATION
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment