-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdot_bash_functions
214 lines (184 loc) · 5.96 KB
/
dot_bash_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
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
# -*-mode:bash-*- vim:ft=bash
# ~/.bash_functions
# =============================================================================
# Shell functions sourced by `~/.bashrc` and `~/.zshrc`.
# shellcheck shell=bash
# General
# -----------------------------------------------------------------------------
# Extracts common file formats.
# Syntax: `extract <file1>`
# Source: https://github.com/xvoland/Extract
# TODO extract in named folder
function extract {
if [ -z "$1" ]; then
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 0
fi
for n in "$@"
do
if [ -f "$n" ] ; then
case "${n%,}" in
*.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
tar xvf "$n" ;;
*.lzma) unlzma ./"$n" ;;
*.bz2) bunzip2 ./"$n" ;;
*.cbr|*.rar) unrar x -ad ./"$n" ;;
*.gz) gunzip ./"$n" ;;
*.cbz|*.epub|*.zip) unzip ./"$n" ;;
*.z) uncompress ./"$n" ;;
*.7z|*.apk|*.arj|*.cab|*.cb7|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar)
7z x ./"$n" ;;
*.xz) unxz ./"$n" ;;
*.exe) cabextract ./"$n" ;;
*.cpio) cpio -id < ./"$n" ;;
*.cba|*.ace) unace x ./"$n" ;;
*.zpaq) zpaq x ./"$n" ;;
*.arc) arc e ./"$n" ;;
*.cso) ciso 0 ./"$n" ./"$n.iso" && \
extract "$n".iso && \rm -f "$n" ;;
*)
echo "extract: '$n' - unknown archive method"
return 1
;;
esac
else
echo "'$n' - file does not exist"
return 1
fi
done
}
alias x=extract
# Searches history for a string, or lists all history.
# Syntax: `historysearch <string>`
function history_search() {
if [ -z "$1" ]; then
history
else
history | grep "$1"
fi
}
# Searches session history for a string, or lists all session history.
# Syntax: `history_session_search <string>`
function history_session_search() {
prefix=$(date +"$HISTTIMEFORMAT")
offset=$((8 + ${#prefix}))
comm -23 <(history | cut -c ${offset}-) "${HISTFILE:-'~/.bash_history'}" | grep "$1"
}
# Creates a directory and changes to it.
# Syntax: `mkcd <directory>`
function mkcd() {
if [ -z "$1" ]; then
echo "Usage: mkcd <path>"
echo "Help: mkcd creates a directory if it doesn't exist, then changes to it."
return 0
fi
mkdir -p -- "$@" && cd -P -- "$_" || exit;
}
alias take=mkcd
# Repeats a command a set number of times.
# Syntax: `repeat <count> <command>`
function repeat() {
if [ -z "$1" ] || [ "$#" -lt 2 ]; then
echo "Usage: repeat <count> <command> ..."
echo "Help: repeat runs a command x number of times."
return $#
fi
local i max
max=$1; shift;
for ((i=1; i <= max ; i++)); do
eval "$@";
done
}
alias r=repeat
# Sysadmin
# -----------------------------------------------------------------------------
# Keeps all apps and packages up to date.
# Syntax: `update [all]`
function update() {
if command -v softwarepudate &> /dev/null; then
echo 'Checking for system updates...'
softwareupdate -l -i -a
fi
if command -v brew &> /dev/null; then
echo 'Updating packages with Homebrew/Linuxbrew...'
brew update
brew upgrade
brew cask update
brew cleanup
fi
if [[ "$1" == 'all' ]]; then
if command -v mas &> /dev/null; then
echo 'Updating App Store applications...'
mas upgrade
fi
fi
if ! [[ "$OSTYPE" =~ ^darwin ]]; then
if command -v apt &> /dev/null; then
echo 'Updating packages with apt...'
apt update
apt full-upgrade
apt autoremove
apt clean
apt autoclean
fi
if command -v apt-get &> /dev/null; then
echo 'Updating packages with apt-get...'
apt-get update
apt-get upgrade
apt-get dist-upgrade
fi
fi
if command -v npm &> /dev/null; then
echo 'Updating Node.js packages with npm...'
which npm
npm update -g
fi
if command -v npm &> /dev/null; then
echo 'Updating Ruby gems...'
which gem
gem update --system
gem update
gem cleanup
fi
}
# Applications
# -----------------------------------------------------------------------------
# Opens file/URL in Microsoft Edge.
# Syntax: `microsoft-edge <url>`
if [[ "$OSTYPE" =~ ^(cygwin|mingw|msys) ]]; then
function microsoft-edge() {
start microsoft-edge:"$1"
}
fi
# Development
# -----------------------------------------------------------------------------
# Calls Python's pip3 at the global level.
if command -v pip3 > /dev/null; then
function gpip3() {
PIP_REQUIRE_VIRTUALENV="0" pip3 "$@"
}
fi
# Varia
# -----------------------------------------------------------------------------
# Copies contents to the clipboard.
function cb() {
if [ -z "$1" ]; then
echo "Usage: cb <path>"
echo "Help: cb copies a file contents to the clipboard."
return 0
fi
if command -v pbcopy > /dev/null; then
pbcopy < "$1"
elif command -v xclip > /dev/null; then
xclip -selection clipboard < "$1"
elif command -v xsel > /dev/null; then
xsel -ib < "$1"
elif command -v clipboard > /dev/null; then # node.js clipboard-cli
clipboard < "$1"
elif command -v clip > /dev/null; then
clip < "$1"
elif command -v powershell > /dev/null; then
powershell -NoProfile -Command "Set-Clipboard"
fi
}