-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions
129 lines (114 loc) · 3.14 KB
/
functions
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
#!/usr/bin/env zsh
# Simple backup
function backup() {
mv "$1" "$1.backup"
}
# All the dig info
function digga() {
dig +nocmd "$1" any +multiline +noall +answer
}
# `e` with no arguments opens the current directory in Sublime Text, otherwise
# opens the given location
function e() {
if [ $# -eq 0 ]; then
code .;
else
code "$@";
fi;
}
# credit: http://nparikh.org/notes/zshrc.txt
# Usage: extract <file>
# Description: extracts archived files / mounts disk images
# Note: .dmg/hdiutil is Mac OS X-specific.
function extract() {
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar -jxvf $1 ;;
*.tar.gz) tar -zxvf $1 ;;
*.bz2) bunzip2 $1 ;;
*.dmg) hdiutil mount $1 ;;
*.gz) gunzip $1 ;;
*.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"
fi
}
# No arguments: `git status`
# With arguments: acts like `git`
# unalias g
function g() {
if [[ $# > 0 ]]; then
git $@
else
git status
fi
}
# Complete g like git
compdef g=git
compdef tig=git
# Create a new directory and enter it
function mkd() {
mkdir -p "$@" && cd "$@"
}
# Usage: mv oldfilename
# If you call mv without the second parameter it will prompt you to edit the filename on command line.
# Original mv is called when it's called with more than one argument.
function mv() {
if [ "$#" -ne 1 ] || [ ! -f "$1" ]; then
command mv "$@"
return
fi
read -ei "$1" newfilename
command mv -v -- "$1" "$newfilename"
}
# `o` with no arguments opens current directory, otherwise opens the given
# location
function o() {
if [ $# -eq 0 ]; then
open .
else
open "$@"
fi
}
# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression
function targz() {
local tmpFile="${@%/}.tar"
tar -cvf "${tmpFile}" --exclude=".DS_Store" "${@}" || 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."
}
# `v` with no arguments opens the current directory in Vim, otherwise opens the
# given location
function v() {
if [ $# -eq 0 ]; then
vim .
else
vim "$@"
fi
}