-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbashrc
More file actions
429 lines (361 loc) · 11.7 KB
/
bashrc
File metadata and controls
429 lines (361 loc) · 11.7 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
[ -z "$PS1" ] && return
# ----------------------------------------------------------------------------
# Prompt settings
# Display current git branch in bash prompt
# http://asemanfar.com/Current-Git-Branch-in-Bash-Prompt
# http://betterexplained.com/articles/aha-moments-when-learning-git/
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\[\033[00m\]\u@\h\[\033[01;34m\]:\w\[\033[31m\]\$(parse_git_branch)\[\033[00m\]$\[\033[00m\] "
# NOTE: We might override PS1 and PROMPT_COMMAND in our ~/.bashrc_local
# Number of trailing components to retain when expanding \w and \W
export PROMPT_DIRTRIM=3
# Let's use nvimpager if it's installed
if command -v nvimpager &>/dev/null; then
export PAGER=nvimpager
fi
# ----------------------------------------------------------------------------
# History settings
# http://bradchoate.com/weblog/2006/05/19/daily-history-files-for-bash
#export HISTFILE="~/.history/`date +%Y%m%d`.hist"
export HISTFILESIZE=10000
export HISTSIZE=10000
# Erase duplicate lines from history, and don't save commands
# which start with a space. See bash(1) for more options.
export HISTCONTROL=erasedups:ignorespace
# ignore some boring stuff. The " *" bit ignores all command lines
# starting with whitespace, useful to selectively avoid the history
export HISTIGNORE="ls:cd:cd ..:..*: *"
# ignore these while tab-completing
export FIGNORE="CVS:.svn:.git"
# Append to history file instead of overwriting it
shopt -s histappend
# ----------------------------------------------------------------------------
# Shell options
# Check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# Don't use Ctrl-D to exit
set -o ignoreeof
# Some more shell options - see bash(1)
shopt -s dotglob nocaseglob no_empty_cmd_completion
# Correct any minor spelling errors in the directory name, for the cd command>
#shopt -s cdspell
# Use vi-style command line editing
set -o vi
# Enable globstar ** patterns
[ "$BASH_VERSINFO" -ge "4" ] && shopt -s globstar
# ----------------------------------------------------------------------------
function mkcd() {
local dir="$1"
mkdir -p "${dir}"
cd "${dir}"
}
function mkcd-month() {
local dir="./$(date +%Y-%m)"
mkdir -p "${dir}"
cd "${dir}"
}
function mkcd-today() {
local dir="./$(date +%Y-%m-%d)"
mkdir -p "${dir}"
cd "${dir}"
}
function mk-today() {
local dir="${HOME}/journal/$(date +%Y)/$(date +%m-%d)"
mkdir -p "${dir}"
cd "${dir}"
}
function mk-prev() {
local yday=$(gdate -d "yesterday" +%Y/%m-%d)
local dir="${HOME}/journal/${yday}"
if [ -d "${dir}" ]; then
cd "${dir}"
else
echo "Directory for previous day does not exist."
fi
}
function mk-next() {
local tmrw=$(gdate -d "tomorrow" +%Y/%m-%d)
local dir="${HOME}/journal/${tmrw}"
if [ -d "${dir}" ]; then
cd "${dir}"
else
echo "Directory for next day does not exist."
fi
}
function mk-week() {
local year=$(date +%Y)
local week=$(date +%V)
local week_dir="${year}-W${week}"
local dir="${HOME}/blogs/weekly/${week_dir}"
mkdir -p "${dir}"
cd "${dir}"
}
# ----------------------------------------------------------------------------
cd-prev() {
local cwd
cwd="$(pwd)"
# Validate we're inside $HOME/journal/YYYY/MM-DD and extract year and month-day
local _year
_year="$(_journal_get_year "${cwd}")"
if [[ $? -ne 0 ]]; then
echo "cd-prev: not inside ${HOME}/journal/YYYY/MM-DD" >&2
return 1
fi
local _month_day
_month_day="$(_journal_get_month_day "${cwd}")"
if [[ $? -ne 0 ]]; then
echo "cd-prev: not inside ${HOME}/journal/YYYY/MM-DD" >&2
return 1
fi
local target="${_year}-${_month_day}"
local counter=0
while [ $counter -lt 1000 ]; do
target=$(gdate -d "${target} - 1 day" +%Y-%m-%d)
if [[ $? -ne 0 ]]; then
echo "cd-prev: date computation failed for '${target}'" >&2
return 1
fi
local formatted_target="${target:0:4}/${target:5}"
local dir="${HOME}/journal/${formatted_target}"
if [ -d "${dir}" ]; then
cd "${dir}" || return 1
return 0
fi
counter=$((counter + 1))
done
echo "cd-prev: reached iteration limit. Stopped looking on ${target}" >&2
return 1
}
cd-next() {
local cwd
cwd="$(pwd)"
# Validate we're inside $HOME/journal/YYYY/MM-DD and extract year and month-day
local _year
_year="$(_journal_get_year "${cwd}")"
if [[ $? -ne 0 ]]; then
echo "cd-next: not inside ${HOME}/journal/YYYY/MM-DD" >&2
return 1
fi
local _month_day
_month_day="$(_journal_get_month_day "${cwd}")"
if [[ $? -ne 0 ]]; then
echo "cd-next: not inside ${HOME}/journal/YYYY/MM-DD" >&2
return 1
fi
local target="${_year}-${_month_day}"
local counter=0
while [ $counter -lt 1000 ]; do
target=$(gdate -d "${target} + 1 day" +%Y-%m-%d)
if [[ $? -ne 0 ]]; then
echo "cd-next: date computation failed for '${target}'" >&2
return 1
fi
local formatted_target="${target:0:4}/${target:5}"
local dir="${HOME}/journal/${formatted_target}"
if [ -d "${dir}" ]; then
cd "${dir}" || return 1
return 0
fi
counter=$((counter + 1))
done
echo "cd-next: reached iteration limit. Stopped looking on ${target}" >&2
return 1
}
## Internal helper: return the year component from a journal path
## Usage: _journal_get_year <cwd>
## Echoes the year (e.g. 2025) on success, returns non-zero on failure
_journal_get_year() {
local cwd="$1"
if [[ -z "${cwd}" ]]; then
echo "" >&2
return 1
fi
local journal_prefix="${HOME}/journal/"
if [[ "${cwd}" != "${journal_prefix}"* ]]; then
echo "" >&2
return 1
fi
local rel="${cwd#${journal_prefix}}"
local year="${rel%%/*}"
if [[ ! ${year} =~ ^[0-9]{4}$ ]]; then
echo "" >&2
return 1
fi
printf '%s' "${year}"
return 0
}
## Internal helper: return the month-day (MM-DD) component from a journal path
## Usage: _journal_get_month_day <cwd>
## Echoes the month-day (e.g. 10-20) on success, returns non-zero on failure
_journal_get_month_day() {
local cwd="$1"
if [[ -z "${cwd}" ]]; then
echo "" >&2
return 1
fi
local journal_prefix="${HOME}/journal/"
if [[ "${cwd}" != "${journal_prefix}"* ]]; then
echo "" >&2
return 1
fi
local md
md="$(basename -- "${cwd}")"
if [[ ! ${md} =~ ^[0-9]{2}-[0-9]{2}$ ]]; then
echo "" >&2
return 1
fi
printf '%s' "${md}"
return 0
}
prev-dir() {
local current_dir
current_dir="$(basename -- "$(pwd)")"
if [[ ! ${current_dir} =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
echo "Current directory name '${current_dir}' does not match YYYY-MM-DD"
return 1
fi
local parent
parent="$(dirname -- "$(pwd)")"
# Collect sibling date-dirs via globbing (no ls; robust against aliases)
local -a dirs=()
local d name
shopt -s nullglob
for d in "${parent}"/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]; do
[[ -d ${d} ]] || continue
name="$(basename -- "${d}")"
dirs+=("${name}")
done
shopt -u nullglob
if (( ${#dirs[@]} == 0 )); then
echo "No sibling YYYY-MM-DD directories found in '${parent}'."
return 1
fi
# Sort lexicographically (works for YYYY-MM-DD)
local sorted
sorted="$(printf '%s\n' "${dirs[@]}" | LC_ALL=C sort)"
dirs=()
while IFS= read -r name; do
[[ -n ${name} ]] && dirs+=("${name}")
done <<< "${sorted}"
# Find current index
local idx=-1 i
for i in "${!dirs[@]}"; do
if [[ ${dirs[${i}]} == "${current_dir}" ]]; then
idx=${i}
break
fi
done
if (( idx <= 0 )); then
echo "No previous directory found before '${current_dir}'."
return 1
fi
cd -- "${parent}/${dirs[$((idx-1))]}"
}
next-dir() {
local current_dir
current_dir="$(basename -- "$(pwd)")"
if [[ ! ${current_dir} =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
echo "Current directory name '${current_dir}' does not match YYYY-MM-DD"
return 1
fi
local parent
parent="$(dirname -- "$(pwd)")"
local -a dirs=()
local d name
shopt -s nullglob
for d in "${parent}"/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]; do
[[ -d ${d} ]] || continue
name="$(basename -- "${d}")"
dirs+=("${name}")
done
shopt -u nullglob
if (( ${#dirs[@]} == 0 )); then
echo "No sibling YYYY-MM-DD directories found in '${parent}'."
return 1
fi
local sorted
sorted="$(printf '%s\n' "${dirs[@]}" | LC_ALL=C sort)"
dirs=()
while IFS= read -r name; do
[[ -n ${name} ]] && dirs+=("${name}")
done <<< "${sorted}"
local idx=-1 i
for i in "${!dirs[@]}"; do
if [[ ${dirs[${i}]} == "${current_dir}" ]]; then
idx=${i}
break
fi
done
if (( idx < 0 || idx >= ${#dirs[@]} - 1 )); then
echo "No next directory found after '${current_dir}'."
return 1
fi
cd -- "${parent}/${dirs[$((idx+1))]}"
}
# ----------------------------------------------------------------------------
cd-env() {
# TODO: Pass environment as argument to function.
# TODO: Tab completion for the environment name argument (just one arg)
local env_dir
env_dir=$(conda info --json | jq -r .active_prefix)
if [[ -n "${env_dir}" && "${env_dir}" != "null" ]]; then
cd "${env_dir}" || echo "Failed to cd into active conda env directory."
else
echo "$(tput setaf 1)No Conda environment is currently active.$(tput sgr0)"
fi
}
cd-random() {
local dirs
IFS=$'\n' read -r -d '' -a dirs < <(find . -mindepth 1 -maxdepth 1 -type d -printf '%P\n' && printf '\0')
if [[ ${#dirs[@]} -eq 0 ]]; then
echo "No subdirectories found."
return 1
fi
local random_index=$(( RANDOM % ${#dirs[@]} ))
local target="${dirs[$random_index]}"
cd "$target"
}
# ----------------------------------------------------------------------------
ssh-eval-keychain() {
if ! command -v keychain &> /dev/null; then
echo "Error: keychain command not found"
echo "Install with homebrew or from https://www.funtoo.org/Keychain"
return 1
fi
eval $(keychain --eval id_rsa)
}
# ----------------------------------------------------------------------------
if [[ -r /usr/local/etc/profile.d/bash_completion.sh ]]; then
source /usr/local/etc/profile.d/bash_completion.sh
elif [[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]]; then
source "/opt/homebrew/etc/profile.d/bash_completion.sh"
fi
# Set up bash completion and key bindings for fzf
[ -f ~/.fzf.bash ] && source ~/.fzf.bash
# Invoke completion
if [ -f ~/.invoke-completion.sh ]; then
source ~/.invoke-completion.sh
fi
# Set up iTerm2 shell integration if available
# https://iterm2.com/documentation-shell-integration.html
test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash"
# Prevent ranger from loading its default configs
#export RANGER_LOAD_DEFAULT_RC=FALSE
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
source ~/.bash_aliases
fi
if [ -f ~/.bash_aliases2 ]; then
source ~/.bash_aliases2
fi
# Local bashrc file.
# Use this for machine-specific initializations (e.g. sourcing virtualenvwrapper.sh)
if [ -f ~/.bashrc_local ]; then
source ~/.bashrc_local
fi