Permalink
Switch branches/tags
Nothing to show
Find file Copy path
554 lines (439 sloc) 18.7 KB
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# | Defaults |
# ------------------------------------------------------------------------------
# Enable simple aliases to be sudo'ed. ("sudone"?)
# http://www.gnu.org/software/bash/manual/bashref.html#Aliases says: "If the
# last character of the alias value is a space or tab character, then the next
# command word following the alias is also checked for alias expansion."
alias sudo='sudo '
# Super user
alias _='sudo'
alias please='sudo'
alias clr='clear'
# use vim
if which vim >/dev/null 2>&1; then
alias vi="vim"
fi
# add ssh-key to ssh-agent when key exist
if [ "$SSH_AUTH_SOCK" != "" ] && [ -f ~/.ssh/id_rsa ] && [ -x /usr/bin/ssh-add ]; then
ssh-add -l >/dev/null || alias ssh='(ssh-add -l >/dev/null || ssh-add) && unalias ssh; ssh'
fi
# Confirm before overwriting
# --------------------------
# I know it is bad practice to override the default commands, but this is for
# my own safety. If you really want the original "instakill" versions, you can
# use "command rm", "\rm", or "/bin/rm" inside your own commands, aliases, or
# shell functions. Note that separate scripts are not affected by the aliases
# defined here.
#alias cp='cp -i'
#alias mv='mv -i'
#alias ln='ln -i'
alias rm='rm -I' # 'rm -i' prompts for every file
# safety features
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'
alias chgrp='chgrp --preserve-root'
# dmesg with readable time
alias dmesg='dmesg -T'
# grep v2.21 depracates GREP_OPTIONS env var
alias grep="grep $GREP_OPTIONS"
# ------------------------------------------------------------------------------
# | Global Quick Commands |
# ------------------------------------------------------------------------------
# quick & short
alias g="git"
alias h="history"
alias j="jobs"
# quick edit
alias zshrc="vim ~/.zshrc"
alias bashrc="vim ~/.bashrc"
alias vimrc="vim ~/.vimrc"
# quick reload
alias reload_bash_profile='source ~/.bash_profile'
alias reload_zsh_profile='source ~/.zprofile'
# reload the shell (i.e. invoke as a login shell)
alias reload="exec $SHELL -l"
# Print each PATH entry on a separate line
alias path="echo -e ${PATH//:/\\n}"
# ------------------------------------------------------------------------------
# | Navigation |
# ------------------------------------------------------------------------------
# Push and pop directories on directory stack
alias pu='pushd'
alias po='popd'
# Easier navigation: .., ..., ...., ....., ~ and -
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ~="cd ~" # `cd` is probably faster to type though
alias -- -="cd -"
# fallback by typo
alias cd..='cd ..'
alias cd...='cd ../..'
alias cd....='cd ../../..'
alias cd.....='cd ../../../..'
if [[ "$SHELL" == "$(which zsh 2>/dev/null)" ]]; then
alias 1='cd -'
alias 2='cd -2'
alias 3='cd -3'
alias 4='cd -4'
alias 5='cd -5'
alias 6='cd -6'
alias 7='cd -7'
alias 8='cd -8'
alias 9='cd -9'
fi
# ------------------------------------------------------------------------------
# | Directories Commands (create / remove) |
# ------------------------------------------------------------------------------
# mkdir: always create parent-dirs, if needed
alias mkdir="mkdir -p"
alias md="mkdir"
# dirs
alias d='dirs -v | head -10'
# rmdir
alias rd="rmdir"
# create a dir with date from today
alias mkdd='mkdir $(date +%Y%m%d)'
# ------------------------------------------------------------------------------
# | Colors |
# ------------------------------------------------------------------------------
if [ "$TERM" != "dumb" ]; then
# dircolors
if [[ "$SYSTEM_TYPE" == "LINUX" || "$SYSTEM_TYPE" == "CYGWIN" ]]; then
if [ -s "$HOME/.dircolors" ]; then
eval "$(dircolors -b $HOME/.dircolors)"
else
eval "$(dircolors -b)"
fi
fi
# enable ls colors
if [[ "$DISABLE_LS_COLORS" != "true" ]]; then
if [[ "$SYSTEM_TYPE" == "NET_BSD" ]]; then
# On NetBSD, test if "gls" (GNU ls) is installed (this one supports colors);
# otherwise, leave ls as is, because NetBSD's ls doesn't support -G
gls --color -d . &>/dev/null 2>&1 && COLORFLAG="--color=tty"
elif [[ "$SYSTEM_TYPE" == "OPEN_BSD" ]]; then
# On OpenBSD, test if "colorls" is installed (this one supports colors);
# otherwise, leave ls as is, because OpenBSD's ls doesn't support -G
colorls -G -d . &>/dev/null 2>&1 && COLORFLAG="-G"
elif [[ "$SYSTEM_TYPE" == "OSX" ]]; then
# on OS X use "-G"
COLORFLAG="-G"
elif ls --color -d . &>/dev/null 2>&1; then
COLORFLAG="--color"
if [[ "$SYSTEM_TYPE" == "LINUX" || "$SYSTEM_TYPE" == "CYGWIN" ]]; then
COLORFLAG="--color=auto"
fi
fi
fi
# Burl: better curl shortcuts (https://github.com/visionmedia/burl).
if command -v burl > /dev/null; then
alias curl_get='burl GET'
alias curl_head='burl -I'
alias curl_post='burl POST'
alias curl_put='burl PUT'
alias curl_patch='burl PATCH'
alias curl_delete='burl DELETE'
alias curl_options='burl OPTIONS'
fi
# Use "colordiff" or "highlight" to colour diffs.
if command -v colordiff > /dev/null; then
alias difflight="colordiff"
elif command -v highlight > /dev/null; then
alias difflight="highlight --dark-red ^-.* \
| highlight --dark-green ^+.* \
| highlight --yellow ^Only.* \
| highlight --yellow ^Files.*differ$ \
| less -XFIRd"
else
alias difflight="less -XFIRd"
fi
# Use GRC for additionnal colorization
if which grc >/dev/null 2>&1; then
alias colour="grc -es --colour=auto"
alias as="colour as"
alias configure="colour ./configure"
alias diff="colour diff"
alias dig="colour dig"
alias g++="colour g++"
alias gas="colour gas"
alias gcc="colour gcc"
alias head="colour head"
alias ifconfig="colour ifconfig"
alias ld="colour ld"
alias ls="colour ls"
alias make="colour make"
alias mount="colour mount"
alias netstat="colour netstat"
alias ping="colour ping"
alias ps="colour ps"
alias tail="colour tail"
alias traceroute="colour traceroute"
alias syslog="sudo colour tail -f -n 100 /var/log/syslog"
fi
# replace diff with "git diff"
#if command -v git >/dev/null; then
# alias diff_orig="/usr/bin/diff"
# alias diff="git diff --color-words "
#fi
# replace top with htop
if command -v htop >/dev/null; then
alias top_orig="/usr/bin/top"
alias top="htop"
fi
# Colorize the grep command output for ease of use (good for log files)
alias grep="grep $COLORFLAG"
alias egrep="egrep $COLORFLAG"
alias fgrep="fgrep $COLORFLAG"
fi
# ------------------------------------------------------------------------------
# | List Directory Contents (ls) |
# ------------------------------------------------------------------------------
# list all files colorized in long format
alias l="ls -lhF $COLORFLAG"
# list all files with directories
alias ldir="l -R"
# Show hidden files
alias l.="ls -dlhF .* $COLORFLAG"
alias ldot="l."
# use colors
alias ls="ls -F $COLORFLAG"
# display only files & dir in a v-aling view
alias l1="ls -1 $COLORFLAG"
# displays all files and directories in detail
alias la="ls -laFh $COLORFLAG"
# displays all files and directories in detail (without "." and without "..")
alias lA="ls -lAFh $COLORFLAG"
alias lsa="la"
# displays all files and directories in detail with newest-files at bottom
alias lr="ls -laFhtr $COLORFLAG"
# show last 10 recently changed files
alias lt="ls -altr | grep -v '^d' | tail -n 10"
# show files and directories (also in sub-dir) that was touched in the last hour
alias lf="find ./* -ctime -1 | xargs ls -ltr $COLORFLAG"
# displays files and directories in detail
alias ll="ls -lFh --group-directories-first $COLORFLAG"
# shows the most recently modified files at the bottom of
alias llr="ls -lartFh --group-directories-first $COLORFLAG"
# list only directories
alias lsd="ls -lFh $COLORFLAG | grep --color=never '^d'"
# sort by file-size
alias lS="ls -1FSshr $COLORFLAG"
# displays files and directories
alias dir="ls --format=vertical $COLORFLAG"
# displays more information about files and directories
alias vdir="ls --format=long $COLORFLAG"
# tree (with fallback)
if which tree >/dev/null 2>&1; then
# displays a directory tree
alias tree="tree -Csu"
# displays a directory tree - paginated
alias ltree="tree -Csu | less -R"
else
alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
alias ltree="tree | less -R"
fi
# ------------------------------------------------------------------------------
# | Search and Find |
# ------------------------------------------------------------------------------
# super-grep ;)
alias sgrep='grep -R -n -H -C 5 --exclude-dir={.git,.svn,CVS} '
# search in files (with fallback)
if which ack-grep >/dev/null 2>&1; then
alias ack=ack-grep
alias afind="ack-grep -iH"
else
alias afind="ack -iH"
fi
# ------------------------------------------------------------------------------
# | Package Managers |
# ------------------------------------------------------------------------------
# get / check for updates
alias apt-update="sudo apt-get update"
alias apt-upgrade="sudo apt-get update && sudo apt-get upgrade && sudo apt-get clean"
# ------------------------------------------------------------------------------
# | Network |
# ------------------------------------------------------------------------------
# external ip address
alias myip_dns="dig +short myip.opendns.com @resolver1.opendns.com"
alias myip_http="GET http://ipecho.net/plain && echo"
# show dns info via "dig"
alias dnsInfo="digga"
# speedtest: get a 100MB file via wget
alias speedtest="wget -O /dev/null http://speedtest.wdc01.softlayer.com/downloads/test100.zip"
# Gzip-enabled `curl`
alias gurl="curl --compressed"
# displays the ports that use the applications
alias lsport='sudo lsof -i -T -n'
# shows more about the ports on which the applications use
alias llport='netstat -nape --inet --inet6'
# show only active network listeners
alias netlisteners='sudo lsof -i -P | grep LISTEN'
# ------------------------------------------------------------------------------
# | Date & Time |
# ------------------------------------------------------------------------------
# date
alias date_iso_8601='date "+%Y%m%dT%H%M%S"'
alias date_clean='date "+%Y-%m-%d"'
alias date_year='date "+%Y"'
alias date_month='date "+%m"'
alias date_week='date "+%V"'
alias date_day='date "+%d"'
alias date_hour='date "+%H"'
alias date_minute='date "+%M"'
alias date_second='date "+%S"'
alias date_time='date "+%H:%M:%S"'
# stopwatch
alias timer='echo "Timer started. Stop with Ctrl-D." && date && time cat && date'
# ------------------------------------------------------------------------------
# | Hard- & Software Infos |
# ------------------------------------------------------------------------------
# pass options to free
alias meminfo="free -m -l -t"
# get top process eating memory
alias psmem="ps -o time,ppid,pid,nice,pcpu,pmem,user,comm -A | sort -n -k 6"
alias psmem5="psmem | tail -5"
alias psmem10="psmem | tail -10"
# get top process eating cpu
alias pscpu="ps -o time,ppid,pid,nice,pcpu,pmem,user,comm -A | sort -n -k 5"
alias pscpu5="pscpu5 | tail -5"
alias pscpu10="pscpu | tail -10"
# shows the corresponding process to ...
alias psx="ps auxwf | grep "
# shows the process structure to clearly
alias pst="pstree -Alpha"
# shows all your processes
alias psmy="ps -ef | grep $USER"
# the load-avg
alias loadavg="cat /proc/loadavg"
# show all partitions
alias partitions="cat /proc/partitions"
# shows the disk usage of a directory legibly
alias du='du -kh'
# show the biggest files in a folder first
alias du_overview='du -h | grep "^[0-9,]*[MG]" | sort -hr | less'
# shows the complete disk usage to legibly
alias df='df -kTh'
# ------------------------------------------------------------------------------
# | System Utilities |
# ------------------------------------------------------------------------------
# becoming root + executing last command
alias sulast='su -c !-1 root'
# Recursively delete `.DS_Store` files
alias cleanup="find . -type f -name '*.DS_Store' -ls -delete"
# empty the your trash-dir
alias emptytrash="rm -rfv ~/.local/share/Trash/*"
# Clipboard access. I created these aliases to have the same command on
# Cygwin, Linux and OS X.
if command -v pbpaste >/dev/null; then
alias getclip="pbpaste"
alias putclip="pbcopy"
elif command -v xclip >/dev/null; then
alias getclip="xclip -selection clipboard -o"
alias putclip="xclip -selection clipboard -i"
elif [[ "$SYSTEM_TYPE" == "MINGW" || "$SYSTEM_TYPE" == "CYGWIN" ]]; then
alias getclip="cat /dev/clipboard"
alias putclip="cat > /dev/clipboard"
fi
# Trim new lines and copy to clipboard
alias pc="putclip"
# ring the terminal bell, and put a badge on Terminal.app’s Dock icon
# (useful when executing time-consuming commands)
alias badge="tput bel"
# ------------------------------------------------------------------------------
# | Other |
# ------------------------------------------------------------------------------
# Language aliases
alias rb='ruby'
alias py='python'
alias ipy='ipython'
# decimal to hexadecimal value
alias dec2hex='printf "%x\n" $1'
# 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"
# OS X has no `sha1sum`, so use `shasum` as a fallback
command -v sha1sum > /dev/null || alias sha1sum="shasum"
# urldecode - url http network decode
alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
# urlencode - url encode network http
alias urlencode='python -c "import sys, urllib as ul; print ul.quote_plus(sys.argv[1])"'
# ROT13-encode text. Works for decoding, too! ;)
alias rot13='tr a-zA-Z n-za-mN-ZA-M'
# intuitive map function
#
# For example, to list all directories that contain a certain file:
# find . -name .gitattributes | map dirname
alias map="xargs -n1"
# make Grunt print stack traces by default
command -v grunt > /dev/null && alias grunt="grunt --stack"
# 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"
# php - package-manager - composer
alias composer-install="composer install --optimize-autoloader"
# add ssh-key to ssh-agent when key exist
if [ "$SSH_AUTH_SOCK" != "" ] && [ -f "~/.ssh/id_rsa" ] && [ -x "/usr/bin/ssh-add" ]; then
ssh-add -l >/dev/null || alias ssh='(ssh-add -l >/dev/null || ssh-add) && unalias ssh; ssh'
fi
# ------------------------------------------------------------------------------
# | Fun |
# ------------------------------------------------------------------------------
#alias nyancat="telnet miku.acm.uiuc.edu" # offline
alias starwars="telnet towel.blinkenlights.nl"
# ------------------------------------------------------------------------------
# | auto-completion (for bash) |
# ------------------------------------------------------------------------------
# Automatically add completion for all aliases to commands having completion functions
# source: http://superuser.com/questions/436314/how-can-i-get-bash-to-perform-tab-completion-for-my-aliases
alias_completion()
{
local namespace="alias_completion"
# parse function based completion definitions, where capture group 2 => function and 3 => trigger
local compl_regex='complete( +[^ ]+)* -F ([^ ]+) ("[^"]+"|[^ ]+)'
# parse alias definitions, where capture group 1 => trigger, 2 => command, 3 => command arguments
local alias_regex="alias ([^=]+)='(\"[^\"]+\"|[^ ]+)(( +[^ ]+)*)'"
# create array of function completion triggers, keeping multi-word triggers together
eval "local completions=($(complete -p | sed -rne "/$compl_regex/s//'\3'/p"))"
(( ${#completions[@]} == 0 )) && return 0
# create temporary file for wrapper functions and completions
rm -f "/tmp/${namespace}-*.XXXXXXXXXX" # preliminary cleanup
local tmp_file="$(mktemp "/tmp/${namespace}-${RANDOM}.XXXXXXXXXX")" || return 1
# read in "<alias> '<aliased command>' '<command args>'" lines from defined aliases
local line; while read line; do
eval "local alias_tokens=($line)" 2>/dev/null || continue # some alias arg patterns cause an eval parse error
local alias_name="${alias_tokens[0]}" alias_cmd="${alias_tokens[1]}" alias_args="${alias_tokens[2]# }"
# skip aliases to pipes, boolan control structures and other command lists
# (leveraging that eval errs out if $alias_args contains unquoted shell metacharacters)
eval "local alias_arg_words=($alias_args)" 2>/dev/null || continue
# skip alias if there is no completion function triggered by the aliased command
[[ " ${completions[*]} " =~ " $alias_cmd " ]] || continue
local new_completion="$(complete -p "$alias_cmd")"
# create a wrapper inserting the alias arguments if any
if [[ -n $alias_args ]]; then
local compl_func="${new_completion/#* -F /}"; compl_func="${compl_func%% *}"
# avoid recursive call loops by ignoring our own functions
if [[ "${compl_func#_$namespace::}" == $compl_func ]]; then
local compl_wrapper="_${namespace}::${alias_name}"
echo "function $compl_wrapper {
(( COMP_CWORD += ${#alias_arg_words[@]} ))
COMP_WORDS=($alias_cmd $alias_args \${COMP_WORDS[@]:1})
$compl_func
}" >> "$tmp_file"
new_completion="${new_completion/ -F $compl_func / -F $compl_wrapper }"
fi
fi
# replace completion trigger by alias
new_completion="${new_completion% *} $alias_name"
echo "$new_completion" >> "$tmp_file"
done < <(alias -p | sed -rne "s/$alias_regex/\1 '\2' '\3'/p")
source "$tmp_file" && rm -f "$tmp_file"
}
if [ -n "$BASH_VERSION" ]; then
alias_completion
fi
unset -f alias_completion