forked from matthewmccullough/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathshellfunctions
263 lines (236 loc) · 5.97 KB
/
shellfunctions
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
#!/bin/sh
# Shell Functions.
# Set iTerm2 tab title
#function tabTitle() {
# echo -ne "\033]0;"$*"\007";
#}
# Set iTerm2 window title
#function winTitle() {
# echo -ne "\033]0;"$*"\007";
#}
# Always list directory contents and set title upon 'cd'
#function cd() {
# builtin cd "$@"; tabTitle ${PWD##*/}; winTitle ${PWD/#"$HOME" /~};
#}
function killxscreensaver() {
pkill xscreensaver
echo ""
return
}
function col() {
awk '{print $'$(echo $* | sed -e 's/ /,$/g')'}';
}
# Awk calculator.
function calc() {
awk "BEGIN { print $* }" ;
}
# Add a path to the global PATH.
function pathmunge() {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
# .. - Does a 'cd ..'
# .. 3 - Does a 'cd ../../..'
#
# Usage .. [n]
# Go up n-levels.
# i.e.: .. 3 will go up 3 levels
function .. {
local arg=${1:-1};
local dir=""
while [ $arg -gt 0 ]; do
dir="../$dir"
arg=$(($arg - 1));
done
cd $dir >&/dev/null
}
# Usage ... Thing/Some
# Go up until you encounter Thing/Some, then go there
# i.e.: I'm in /usr/share/X11
# ... src will go up to /usr, then change to /usr/src
function ... {
if [ -z "$1" ]; then
return
fi
local maxlvl=16
local dir=$1
while [ $maxlvl -gt 0 ]; do
dir="../$dir"
maxlvl=$(($maxlvl - 1));
if [ -d "$dir" ]; then
cd $dir >&/dev/null
fi
done
}
# Change extensions of specified files.
function chext() {
local fname
local new_ext="$1"
shift
IFS=$'\n'
for fname in $@
do
mv "$fname" "${fname%.*}.$new_ext"
done
}
function tools() {
pushd . >/dev/null
brtop
if [ -d $(pwd)/tools ]; then
PATH=${PATH/$TOOLS:/}
TOOLS=$(pwd)/tools/cm
pathmunge ${TOOLS}
#PATH=${TOOLS}:${PATH}
echo $TOOLS
fi
popd >/dev/null
}
# Create a new git repo with one README commit and cd into it
function git.nr() {
mkdir $1
cd $1
git init
touch README
git add README
git commit -m "First commit."
}
function find.name() {
wild=\'\*$1\*\'
sh -c "find . -iname $wild"
}
# Function that takes 2 args, a list and a string,
# and returns 0 if the string is in the list, or
# 1 otherwise.
function contains() {
for item in $1; do
if [ "$item" = "$2" ]; then
return 0
fi
done
return 1
}
# From Eli Bendersky
# http://eli.thegreenplace.net/2013/06/11/keeping-persistent-history-in-bash/
# https://github.com/eliben/code-for-blog/blob/master/2016/persistent-history/add-persistent-history.sh
function log_bash_persistent_history() {
local rc=$?
[[ $(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$ ]]
local date_part="${BASH_REMATCH[1]}"
local command_part="${BASH_REMATCH[2]}"
# Try to skip usernames/passwords.
if [[ $command_part == *"$USER"* ]]; then
return
fi
if [[ $command_part == *"API_KEY"* ]]; then
return
fi
if [[ $command_part == *"PASSWORD"* ]]; then
return
fi
for username in "$_USERNAMES"; do
if [[ $command_part == *"$username"* ]]; then
return
fi
done
if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]; then
echo "$HOSTNAME | $date_part | $command_part" >> ~/.persistent_history
export PERSISTENT_HISTORY_LAST="$command_part"
fi
}
# gitignore.io
# http://www.gitignore.io/cli
function gi() {
curl http://www.gitignore.io/api/$@ ;
}
# Linux?
function isLinux() {
if [ "$OS" = "Linux" ]; then
return 0
fi
return 1
}
# Mac?
function isMac() {
if [ "$OS" = "Darwin" ]; then
return 0
fi
return 1
}
# iTerm2 tab titles
function title() {
if [ ! -e "${HOME}/.iterm2_shell_integration.bash" ]; then
return
fi
if [ "$1" ]; then
# Manually specify the title.
echo -ne "\033]0;${*}\007"
else
# This shows the path from ~ in the title.
#echo -ne "\033]0;${PWD/#$HOME/~}\007"
# This shows the last part of the path in the title.
#echo -ne "\033]0;${PWD##*/}\007"
# This shows the current git repo, or directory, in the title.
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
local directory
echo -ne "\033];${PWD##*/}\007"
else
local branch
local branchdir
branchdir=$(basename "$(git rev-parse --show-toplevel)")
branch=$(git branch 2>/dev/null | grep -e '\* ' | sed "s/^..\(.*\)/{\1}/")
echo -ne "\033];[$branchdir] ${PWD##*/} $branch\007"
fi
fi
}
function gpip2() {
PIP_REQUIRE_VIRTUALENV="0" pip2 "$@"
}
function gpip() {
PIP_REQUIRE_VIRTUALENV="0" pip3 "$@"
}
function upgrade_pyenvs() {
if command -v pyenv 1>/dev/null 2>&1; then
for env_ver in $(pyenv versions --bare); do
pyenv shell $env_ver
pyenv version
PIP_REQUIRE_VIRTUALENV="0" pip3 install --quiet --upgrade pip gnureadline wheel virtualenv virtualenvwrapper
pyenv shell --unset
done
pyenv shell --unset
unset PYENV_VERSION
pyenv version
fi
}
# Clone a repo and install pre-commit hooks if configured.
function git.clone() {
git clone $1
subdir="${1##*/}"
subdir="${subdir%.git}"
pushd "${subdir}" > /dev/null
if [ -e .pre-commit-config.yaml ]; then
if [ ! -e ./configure-hooks.sh ]; then
echo "Error: pre-commit hooks required, but install script is not present!"
popd > /dev/null
return 1
fi
which pre-commit >/dev/null || {
echo "Error: pre-commit hooks required, but 'pre-commit' is not installed!"
popd > /dev/null
return 1
}
./configure-hooks.sh --install
fi
popd > /dev/null
return 0
}
if [ -f ~/dotfiles/shellfunctions-personal ]; then
source ~/dotfiles/shellfunctions-personal
fi
if [ -f ~/dotfiles/shellfunctions-work ]; then
source ~/dotfiles/shellfunctions-work
fi