-
Notifications
You must be signed in to change notification settings - Fork 4
/
shrc
378 lines (331 loc) · 8.12 KB
/
shrc
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/sh
echo Loading shell environment ... >&2
# Convenience functions for running Emacs in various ways.
em() {
if [ "$#" -eq 0 ]
then
echo "Starting new Emacs process ..." >&2
emacs & disown
elif emacsclient -n "$@" 2> /dev/null
then
echo "Opened $@ in Emacs server" >&2
else
echo "Opening $@ in a new Emacs process ..." >&2
emacs "$@" & disown
fi
}
[ -e ~/.remote ] && alias em=emacs
emc() {
emacsclient "$@"
}
emt() {
emacs -nw "$@"
}
# Test colors.
fgcol() {
for i in {0..255}; do tput setaf $i; printf "$i "; done; echo
}
bgcol() {
for i in {0..255}; do tput setab $i; printf "$i "; done; echo
}
# Set my Git user details locally in the current repository.
gitme() {
git config user.name "Susam Pal"
git config user.email susam@susam.net
git config user.name
git config user.email
}
# Show the current effective Git user.
gitwho() {
git config user.name
git config user.email
}
# Clone GitHub repository.
ghclone() {
cd ~/git
pwd
if [ $# -eq 1 ]
then
git clone "https://github.com/susam/$1.git"
cd "$1"
else
git clone "https://github.com/$1/$2.git"
cd "$2"
fi
pwd
}
# Go to the top-level directory of the current project.
gcd() {
check_path=$PWD
while [ "$check_path" != / ]
do
[ -d "$check_path/.git" ] && cd "$check_path" && break
check_path=${check_path%/*}
done
}
# OCR PDFs.
ocr()
{
for f in "$@"
do
echo "OCRing $f ..."
ocrmypdf --force-ocr "$f" "${f%.pdf}-ocr.pdf"
mv "$f" "$f.bkp"
mv "${f%.pdf}-ocr.pdf" "$f"
ls -lh "$f.bkp"
ls -lh "$f"
done
}
# Show message box with a message.
alert() {
if command -v osascript
then
osascript -e "tell app \"System Events\" to display alert \"$*\""
else
echo "$*" >&2
fi > /dev/null
}
# Redshift
red()
{
case $1 in
off)
pkill redshift
;;
[0-9]*)
pkill redshift
pkill redshift
redshift -v -l 0:0 -t $1:$1 > ~/redshift.log 2>&1 &
;;
bed)
pkill redshift
pkill redshift
redshift -v -l 13:77 -t 3400:1900 > ~/redshift.log 2>&1 &
;;
"")
pkill redshift
pkill redshift
redshift -v -l 13:77 -t 5000:3400 > ~/redshift.log 2>&1 &
;;
*)
echo 'Usage: red [off | bed | <temperature>]' >&2
;;
esac
}
# Clean environment.
dirclean() {
find . -name "__pycache__" -exec rm -v -r {} +
find . -name "*.pyc" -exec rm -v {} +
find . -name ".DS_Store" -exec rm -v {} +
}
# Search texlive for file
findtl()
{
find /usr/local/texlive -name "*$1*"
}
grepx()
{
grep -r -I --exclude-dir=".[^/]*" --exclude-dir=venv/ --exclude-dir=node_modules/ "$@"
}
findx()
{
find . | grep -vE "/\.|__pycache__" | grep "$1"
}
# Apple documentation for keyboard usage IDs:
# https://developer.apple.com/library/archive/technotes/tn2450/_index.html
# Remap keys in Mac British keyboard to US layout keys.
mbk()
{
# section/plus-minus (0x64) -> backquote/tilde (0x35)
# return (0x28) -> \| (0x31)
# \| (0x31) -> return (0x28)
# non-us ~# (0x32) (absent) -> return (0x28) (transition)
hidutil property --set '{"UserKeyMapping": [
{
"HIDKeyboardModifierMappingSrc": 0x700000064,
"HIDKeyboardModifierMappingDst": 0x700000035
},
{
"HIDKeyboardModifierMappingSrc": 0x700000028,
"HIDKeyboardModifierMappingDst": 0x700000031
},
{
"HIDKeyboardModifierMappingSrc": 0x700000031,
"HIDKeyboardModifierMappingDst": 0x700000028
},
{
"HIDKeyboardModifierMappingSrc": 0x700000032,
"HIDKeyboardModifierMappingDst": 0x700000028
}
]}'
}
# Remap keys in external British keyboard to Macbook US layout keys.
ebk()
{
# return (0x28) -> non-us ~# (but types \|) (0x32)
# \| (0x31) (absent) -> return (0x28) (transition)
# non-us ~# (0x32) (but types \|) -> return (0x28)
# left alt (0xe2) -> left gui (0xe3)
# left gui (0xe3) -> left alt (0xe2)
hidutil property --set '{"UserKeyMapping": [
{
"HIDKeyboardModifierMappingSrc": 0x700000028,
"HIDKeyboardModifierMappingDst": 0x700000032
},
{
"HIDKeyboardModifierMappingSrc": 0x700000031,
"HIDKeyboardModifierMappingDst": 0x700000028
},
{
"HIDKeyboardModifierMappingSrc": 0x700000032,
"HIDKeyboardModifierMappingDst": 0x700000028
},
{
"HIDKeyboardModifierMappingSrc": 0x7000000e2,
"HIDKeyboardModifierMappingDst": 0x7000000e3
},
{
"HIDKeyboardModifierMappingSrc": 0x7000000e3,
"HIDKeyboardModifierMappingDst": 0x7000000e2
}
]}'
}
nok()
{
hidutil property --set '{"UserKeyMapping": []}'
}
# Set environment.
PATH=~/bin:$PATH
PATH=$PATH:/usr/local/smlnj/bin
if command -v opam > /dev/null
then
eval $(opam env)
fi
_begin_esc()
{
[ -n "$BASH_VERSION" ] && printf '\x01' && return
[ -n "$ZSH_VERSION" ] && printf '%%{' && return
}
_end_esc()
{
[ -n "$BASH_VERSION" ] && printf '\x02' && return
[ -n "$ZSH_VERSION" ] && printf '%%}' && return
}
# Set terminal color.
_tput_color()
{
_begin_esc
tput setaf "$1"
_end_esc
}
# Reset terminal color.
_tput_reset()
{
_begin_esc
tput sgr0
_end_esc
}
# Color palette.
if [ -n "$INSIDE_EMACS" ]
then
YELLOW=226
CYAN=87
GREEN=40
ORANGE=208
else
YELLOW=11
CYAN=14
GREEN=2
ORANGE=208
fi
# Create dynamic prompt string for PS1.
_active_prompt()
{
# Git branch
git_branch=$(git branch 2> /dev/null | sed -n "s/^\* *//p")
if [ -n "$git_branch" ]
then
_tput_color "$YELLOW"
printf '[%s] ' "$git_branch"
fi
unset git_branch
# Current session
if [ -f ~/.remote ]
then
_tput_color "$CYAN"
printf "$(id -un)@$(hostname) "
fi
# Current directory
if [ "$PWD" != "$HOME" ]
then
if printf '%s' "$PWD" | grep -q "^$HOME"
then
neat_path="~${PWD#$HOME}"
else
neat_path=$PWD
fi
_tput_color "$GREEN"
printf '%s' "$neat_path"
unset neat_path
fi
# Dollar or beer sign
_tput_color "$ORANGE"
if [ -n "$_PROMPT_MARK" ]
then
printf '%s' "$_PROMPT_MARK"
elif date +"%a" | grep -qE 'Fri|Sat|Sun'
then
printf '%s' "$_books "
else
printf '$ '
fi
_tput_reset
}
# Aliases to modify the ending characters of the primary prompt.
_beer=$(printf "\xf0\x9f\x8d\xba")
_books=$(printf "\xf0\x9f\x93\x9a")
alias dollar='_PROMPT_MARK="$ "'
alias beer='_PROMPT_MARK="$_beer "'
alias books='_PROMPT_MARK="$_books "'
# Aliases for Timeboxing.
alias tm='timebox'
alias tmt='grep "$(date +"%Y-%m-%d")" ~/timebox.log'
alias tml='tmt; echo [ $(tmt | wc -l) ]'
alias tmla='cat ~/timebox.log'
alias tmc='cut -d" " -f1 ~/timebox.log | uniq -c'
alias tmu='sed \$d ~/timebox.log > ~/t.new && mv ~/t.new ~/timebox.log && tml'
alias tmr=': > ~/timebox.log'
# Reclaim C-s for forward incremental search.
stty -ixon
# Aliases for Common Lisp.
alias cl='sbcl --noinform --quit --load'
args() {
for arg in "$@"
do
echo "arg: \"$arg\""
done
}
alias c++11="c++ -g -std=c++11 -Wall -Wextra -pedantic"
# Set the primary prompt string.
PS1='$(_active_prompt)'
# For specific shells only.
if [ -n "$BASH_VERSION" ]
then
echo Loading Bash environment ... >&2
[ -r "/usr/local/etc/profile.d/bash_completion.sh" ] &&
. "/usr/local/etc/profile.d/bash_completion.sh"
elif [ -n "$ZSH_VERSION" ]
then
echo Loading ZSH environment ... >&2
setopt prompt_subst
autoload bashcompinit && bashcompinit
autoload compinit && compinit
fi
[ -f ~/my/bin/shrc ] && . ~/my/bin/shrc
[ -f ~/ws/bin/shrc ] && . ~/ws/bin/shrc
[ -f ~/.cargo/env ] && . ~/.cargo/env
# Attach to an existing Screen session or launch a new one.
# [ -n "$STY$TMUX" ] || screen -dRR
# Attach to an existing tmux or a launch new tmux.
[ -n "$STY$TMUX$INSIDE_EMACS" ] || tmux at || tmux
echo Done >&2