-
Notifications
You must be signed in to change notification settings - Fork 0
/
bashrc_addons
265 lines (235 loc) · 8.34 KB
/
bashrc_addons
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
# Colored ls
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
# Timestamps in Bash history
export HISTTIMEFORMAT='%F %T '
# Lines which begin with a space character are not saved in the
# history list.
export HISTCONTROL=ignorespace
# Setting locale: en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
# Avoid cowsay for ansible
export ANSIBLE_NOCOWS=1
# On Intel CPUs, Homebrew installs files in `/usr/local'. On Apple
# silicon CPUs, instead files are placed into the `/opt/homebrew'
CPU_BRAND=`sysctl -n machdep.cpu.brand_string`
if [[ "$CPU_BRAND" =~ ^Intel ]]; then
export HOMEBREW_BASEDIR="/usr/local"
else
export HOMEBREW_BASEDIR="/opt/homebrew"
fi
# Add Homebrew bin and sbin dirs to the PATH
export HOMEBREW_BINDIR="$HOMEBREW_BASEDIR/bin:$HOMEBREW_BASEDIR/sbin"
export PATH="$HOMEBREW_BINDIR:$PATH"
# Add ~/bin and ~/.local/bin/ to the PATH
export PATH="~/.local/bin:~/bin:$PATH"
# Add ruby bin dir to the PATH
export PATH="$HOMEBREW_BASEDIR/opt/ruby/bin:$PATH"
# Add TMUX Plugin Manager bin dir to the PATH
export PATH="~/.dotfiles/tmux/plugins/tpm/bin:$PATH"
# Add mactex bin dir to the PATH
export PATH="/Library/TeX/texbin/:$PATH"
# Add python3 bin dir to the PATH
export PATH="$HOMEBREW_BASEDIR/opt/python/bin/:$PATH"
# Set EDITOR
export EDITOR="${HOME}/bin/edit"
##################
# BASH FUNCTIONS #
##################
# Helper functions used to upgrade Python 2/3 packages
pip_list_outdated() {
if [ "X$1" = "X" ]; then
echo "A simple bash function to list all of the outdated python Eggs."
echo "Usage: pip_list_outdated <version>"
echo ""
echo "Example: pip_list_outdated 3"
echo " List all of the python3 outdated Eggs"
return 1
fi
if [ "$1" = "2" ]; then
pip2 list --outdated | awk 'NR > 2 {print $1}'
elif [ "$1" = "3" ]; then
pip3 list --outdated | awk 'NR > 2 {print $1}'
else
echo "A simple bash function to list all of the outdated python Eggs."
echo "Usage: pip_list_outdated <version>"
echo ""
echo "Example: pip_list_outdated 2"
echo " List all of the python2 outdated Eggs"
return 1
fi
}
pip_update() {
# If arguments list is empty, print usage menu an exit
if [ "X$1" = "X" ]; then
echo "A simple bash function to upgrade all of the python Eggs installed via pip."
echo "Usage: pip_update <version>"
echo ""
echo "Example: pip_update 2"
echo " Updates all of the python2 Eggs"
return 1
fi
if [ "$1" = "2" ]; then
if [ -f ~/.pip_update2.blacklist ]; then
LIST=$(comm -3 <(pip_list_outdated 2 | sort) <(cat ~/.pip_update2.blacklist | grep -v \# | sort))
else
LIST=$(pip_list_outdated 2)
fi
if [ "X$LIST" != "X" ]; then
echo $LIST
pip2 install -U $LIST
else
BLACKLISTED_PACKAGES=`cat ~/.pip_update2.blacklist | grep \#`
if [ "$BLACKLISTED_PACKAGES" != "0" ]; then
echo "Some packages were not updated. Please have a look into ~/.pip_update2.blacklist"
else
echo "All packages are up-to-date!"
fi
fi
elif [ "$1" = "3" ]; then
if [ -f ~/.pip_update3.blacklist ]; then
LIST=$(comm -3 <(pip_list_outdated 3 | sort) <(cat ~/.pip_update3.blacklist | grep -v \# | sort))
else
LIST=$(pip_list_outdated 3)
fi
if [ "X$LIST" != "X" ]; then
echo $LIST
pip3 install -U $LIST
else
BLACKLISTED_PACKAGES=`cat ~/.pip_update3.blacklist | grep \#`
if [ "$BLACKLISTED_PACKAGES" != "0" ]; then
echo "Some packages were not updated. Please have a look into ~/.pip_update3.blacklist"
else
echo "All packages are up-to-date!"
fi
fi
else
echo "A simple bash function to upgrade all of the python Eggs installed via pip."
echo "Usage: pip_update <version>"
echo ""
echo "Example: pip_update 2"
echo " Updates all of the python2 Eggs"
return 1
fi
}
# Password generator
pwdgen(){
# First argument is the password lenght
openssl rand -base64 $1
}
# Create a Maven project
mvn-create-project() {
# If arguments list is empty, print usage menu an exit
if [ "$#" -ne 2 ]; then
echo "A simple bash function to create a Maven project."
echo "Usage: mvn-create-project groupId artifactId"
echo ""
echo "Example:"
echo " mvn-create-project com.mycompany.my-app my-app"
return 1
fi
# Call maven
mvn archetype:generate -DgroupId=$1 -DartifactId=$2 \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 -DinteractiveMode=false
}
# Start battery draining...
discharge_battery_start () {
local num_cpus=$(sysctl -n hw.ncpu)
let "num_procs = num_cpus-1"
# We run 'num_cpus-1' (and not 'num_cpu') procs to avoid system
# unresponsiveness
for i in `seq 1 $num_procs`;
do
yes > /dev/null &
done
}
# ... stop battery draining
discharge_battery_stop () {
killall yes
}
###########
# ALIASES #
###########
# IOS XRv consoles
alias c1='minicom -D unix#/tmp/rtr01.pipe'
alias c2='minicom -D unix#/tmp/rtr02.pipe'
alias c3='minicom -D unix#/tmp/rtr03.pipe'
alias c4='minicom -D unix#/tmp/rtr04.pipe'
alias c5='minicom -D unix#/tmp/rtr05.pipe'
alias c6='minicom -D unix#/tmp/rtr06.pipe'
alias c7='minicom -D unix#/tmp/rtr07.pipe'
alias c8='minicom -D unix#/tmp/rtr08.pipe'
# Legacy SSH with DH Group1 + old CBC ciphers
alias sshl='ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 -oCiphers=+aes128-cbc,aes192-cbc,aes256-cbc'
# tmux aliases
alias ts='tmux' # tmux start
alias ta='tmux attach -t' # tmux attach
alias tl='tmux ls' # tmux list
# various aliases
alias o='open'
alias man='man -P eless'
alias ec='$HOMEBREW_BASEDIR/bin/emacsclient --no-wait'
alias ep='$HOMEBREW_BASEDIR/bin/emacs --dump-file="$(echo ~/.emacs.d/.cache/dumps/emacs.pdmp)"'
alias ll='ls -l'
alias la='ls -al'
alias grep='grep --colour=auto'
alias sshx='ssh -X'
alias enaHidden='defaults write com.apple.finder AppleShowAllFiles TRUE; killall Finder'
alias disaHidden='defaults write com.apple.finder AppleShowAllFiles FALSE; killall Finder'
alias oc='octave-cli'
alias bupgrade='brew upgrade && brew upgrade --greedy'
alias bhupgrade='brew upgrade --fetch-HEAD && brew upgrade --greedy'
alias bclean='brew cleanup -s'
alias macosx-update='cd ~ && source pyvenv/bin/activate && pip_update 3 && deactivate && gem update && gem cleanup && update_plugins all && bhupgrade && bclean'
alias unquarantine='xattr -r -d com.apple.quarantine'
alias umlet='/Applications/Umlet/umlet.sh'
alias magit="emacs --no-window-system --no-init-file \
--load ~/.dotfiles/magit/magit-init.el \
--eval '(progn (magit-status) (delete-other-windows))'"
alias git-update-submodules='git submodule foreach --recursive git checkout && \
git submodule foreach --recursive git pull'
# The below alias require realpath to be installed: `brew install coreutils`
alias mc='. $(echo $(dirname $(realpath $(which mc))) | sed "s/bin//")libexec/mc/mc-wrapper.sh'
#alias cat="bat"
alias gcd='git add . && git commit -m "Date: $(date)"'
alias img2jpeg='magick mogrify -monitor -format jpg'
###################
# Bash Completion #
###################
# Use existing homebrew v1 completions #
export BASH_COMPLETION_COMPAT_DIR="$HOMEBREW_BASEDIR/etc/bash_completion.d"
# Turn on homebre v2 bash completion
[[ -r "$HOMEBREW_BASEDIR/etc/profile.d/bash_completion.sh" ]] && . "$HOMEBREW_BASEDIR/etc/profile.d/bash_completion.sh"
###################################
# Showing a fortune in each *term #
###################################
case $TERM in
xterm*|rxvt|Eterm|eterm)
cowthink `fortune`
;;
esac
###################################
# Load iTerm2 "Shell Integration" #
###################################
# Tramp on Emacs gets hung up without this workaround. The same
# workareound is needed for oh-my-bash.sh in bashrc.
[ $TERM = "dumb" ] || \
test -e "${HOME}/.iterm2_shell_integration.bash" && \
source "${HOME}/.iterm2_shell_integration.bash"
###############################
# Create GHCi launcher script #
###############################
if [ ! -f ~/bin/ghci ]; then
echo "#!/bin/bash" > ~/bin/ghci
echo "stack exec -- ghci $@" >> ~/bin/ghci
chmod +x ~/bin/ghci
fi
#######################
# Add personal config #
#######################
if [ -f ~/.bash_personal ]; then
. ~/.bash_personal
fi