-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_aliases
More file actions
208 lines (187 loc) · 5.74 KB
/
bash_aliases
File metadata and controls
208 lines (187 loc) · 5.74 KB
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
#!/bin/bash
# Aliases and shell functions for various useful utilities
# An alias for quickly moving up directories
# Run `..` to move up a directory, or `.. <N>` to move up N directories
..() {
# shellcheck disable=SC2164
# we can ignore this issue (`cd` without `|| return`) because cd is the last
cd "$(for ((c=0; c<${1:-1}; ++c)); do echo -n ../; done)"
}
# A calculator command. Run with an argument to calculate that argument,
# run without to open a fake shell (TODO implement moving the cursor and
# history, idk how yet)
calc() {
if [ -z "$1" ]; then
echo -n ">"
while read -r line; do
calc "$line"
echo -n ">"
done
echo
else
awk "BEGIN{ print $* }";
fi
}
# Default to python3 in the shell, regardless of system configuration
alias python=python3
# A function for manipulating aliases programmatically
# If two arguments are provided, alias the first argument to the second one. If a third is supplied,
# add that as a comment explaining the alias. Otherwise, open this alias file in vim.
# Either way, reapply the aliases in this file after making changes.
aliases() {
if [ -n "$2" ]; then
echo "" >> ~/.bash_aliases
if [ -n "$3" ]; then
echo "# $3" >> ~/.bash_aliases
fi
echo "alias $1='$2'" >> ~/.bash_aliases
else
vim ~/.bash_aliases
fi
source ~/.bash_aliases
}
# Reapply the aliases in this file
alias realias="source ~/.bash_aliases"
# Reminders because this happens sometimes
alias :q='echo "You are not in vim, you modron."'
alias :wq='echo "You are not in vim, you modron."'
# Make a new directory and move into that directory
mkcd() {
# shellcheck disable=SC2164
# we can ignore this issue (`cd` without `|| return`) because cd is the last
mkdir -p "$1" && cd "$1";
}
# Make a temporary work directory in `/var/tmp`.
#
# If given, $1 is a base name (will be "/var/tmp/$1.XXXXXX", default 'workdir').
# The remaining arguments are a command to run in the working directory.
temp_workdir() {
if [ -n "${1:+x}" ]; then
local -r dirname="$1"
shift
else
local -r dirname="workdir"
fi
local -r workdir=$(mktemp --directory "/var/tmp/$dirname.XXXXXX")
(
cd "$workdir";
if [ -n "${1:+x}" ]; then
"$@" && PROMPT_LABEL="tempdir $dirname" bash -i
else
PROMPT_LABEL="tempdir $dirname" bash -i
fi
)
if [ -n "$(findmnt | grep "$workdir")" ]; then
echo "Not cleaning up $workdir because mount detected"
return
fi
echo "Cleaning up temporary working directory $workdir"
rm -rf --one-file-system "$workdir"
}
temp_crate() {
local -r cratename="${1:-test-crate}"
temp_workdir "$cratename" cargo init --name "$cratename" .
}
temp_unzip() {
if [ -z "${1:+x}" ] || [ ! -f "$1" ]; then
echo '$1 must be path to a file to unzip'
return 1
fi
local -r zip_path="$(realpath $1)"
local -r workdir="${2:-temp-unzip}"
temp_workdir "$workdir" unzip "$zip_path"
}
temp_untar() {
if [ -z "${1:+x}" ] || [ ! -f "$1" ]; then
echo '$1 must be path to a file to unzip'
return 1
fi
local -r tar_path="$(realpath $1)"
local -r workdir="${2:-temp-untar}"
temp_workdir "$workdir" tar -xf "$tar_path"
}
temp_repo() {
if [ -z "${1:+x}" ]; then
echo '$1 must be a git repo to check out'
return 1
fi
local -r repo_path="$1"
local -r repo_name="$(basename --suffix=.git $repo_path)"
shift
temp_workdir "$repo_name" git clone "$repo_path" . "$@"
}
# List the network interfaces and ips (TODO looks ugly if interfaces don't all have ip addresses)
ips() {
ifconfig | awk '{ if ($1 == "inet" || $1 == "inet6"){ print $2 }; if (/^[^ \t]/){printf "%s ",$1} }'
}
# Try connecting to an SSH server on loop
sshloop() {
while true; do ssh $@; sleep 0.5; done
}
# Like cat, but also outputs the filenames
alias cn='tail -vn +1'
# Make filenames lower-case
lcname() {
if [ -z "$1" ]; then
echo "Please list the files to make lower case"
else
for arg in "$@"; do
mv "$arg" "$(echo "$arg" | awk '{print tolower($0)}')"
done
fi
}
# Run junit more easily (this is the location junit is installed on my laptop, may be different elsewhere)
junit() {
java -cp ".:/usr/share/java/junit-4.13.2.jar" junit.textui.TestRunner "$1"
}
_junit_completion() {
mapfile -t COMPREPLY< <(ls . | grep "^${COMP_WORDS[COMP_CWORD]}" | grep "[.]class$")
for ((i=0; i<${#COMPREPLY[@]}; i++)); do
COMPREPLY[$i]="${COMPREPLY[$i]%.class}"
done
}
complete -F _junit_completion junit
# Go to root of the current repository
repo-root() {
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"
if [ $? -eq 0 ]; then
cd "$REPO_ROOT"
else
echo "Error: not in a repository"
fi
}
# Loop a command until it fails, showing the count
test_loop() {
COUNT=0
while "$@"; do
COUNT=$(( COUNT + 1 ))
echo -e "\n$COUNT successes\n"
done
echo -e "\nFailure after $COUNT successes"
}
# Block the system from sleeping for an interval of time
nosleep() {
if [ -z "$1" ]; then
systemd-inhibit --who=nosleep --why="Requested no sleep indefinitely" sleep 9999d &>/dev/null
else
local -r DURATION="$1"
systemd-inhibit --who=nosleep --why="Requested no sleep for $DURATION" sleep "$DURATION"
fi
}
timer() {
if [ -z "$1" ]; then
echo "Must specify timer duration"
return 1
fi
local -r DURATION="$1"
shift
local -a ARGS
if [ -z "$1" ]; then
ARGS=( "$DURATION timer" )
else
ARGS=( "$@" )
fi
declare -ra ARGS
sleep "$DURATION"
notify-send "Timer elapsed!" "${ARGS[*]}"
}