forked from voku/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.aliases
556 lines (441 loc) · 18.8 KB
/
.aliases
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#!/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
# keep the ssh connection open for at least two hours
alias ssh="ssh -o \"ServerAliveInterval 60\" -o \"ServerAliveCountMax 120\""
# ------------------------------------------------------------------------------
# | 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