-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.bashrc_additions
More file actions
159 lines (132 loc) · 4.32 KB
/
.bashrc_additions
File metadata and controls
159 lines (132 loc) · 4.32 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
#### home-bash-configuration -> .bashrc_additions: begin
# --- GnuPG / direnv setup ---
# See `info "(gnupg) Invoking GPG-AGENT"`
export GPG_TTY=$(tty)
# Export empty DIRENV_LOG_FORMAT to silence direnv log output. See
# https://github.com/direnv/direnv/issues/68
export DIRENV_LOG_FORMAT=
eval "$(direnv hook bash)"
# --- History & shell options for convenience ---
shopt -s autocd # Type just a directory name to cd into it
shopt -s cdspell # Fixe small typos in cd paths
shopt -s checkwinsize # Update $LINES/$COLUMNS when resizing terminal
HISTCONTROL=ignoredups:erasedups # Avoid clutter in history
# Enlarge history beyond defaults
HISTSIZE=100000
HISTFILESIZE=200000
# --- Functions ---
# In Bash, a script runs in a subshell, so `cd` only changes the directory
# within that subshell. To change the state of the current shell (such as the
# working directory with `cd`), the logic must be implemented as a shell
# function.
# $* vs. $@
# "$*" concatenates all args into a single string, separated by the first
# character of $IFS (normally a space)
# "$@" treats each arg as its own word.
# -- is a POSIX‑style "end of options" marker
# Clone with git-clone (Scheme procedure) and cd into repo
gicl() {
git-clone -- "$@" || return
# Last argument = repo URL
local url=${!#}
# Remove a possible trailing slash
url=${url%/}
# Strip path and optional .git suffix
local repo=${url##*/}
# Remove trailing .git suffix
repo=${repo%.git}
cd -- "$repo" || return
}
# mkdir + cd
mkcd() {
mkdir -p -- "$*" || return
cd -- "$*" || return
}
# alias for mkcd
take() { mkcd "$@"; }
# Shortcuts for moving up dirs
..() { cd ..; }
...() { cd ../..; }
....() { cd ../../..; }
# Directory shortcuts. ${VARNAME:?} guards against unset VARNAME
bin() { cd "$HOME/bin"; }
cheat() { cd "${cheat:?}"; }
corona() { cd "${dec:?}/corona_cases"; }
dec() { cd "${dec:?}"; }
dema() { cd "${dev:?}/emacs"; }
der() { cd "${der:?}"; }
desk() { cd ~/Desktop; }
dev() { cd "${dev:?}"; }
dgl() { cd "${dgl:?}"; }
dgx() { cd "${dgx:?}"; }
dgxp() { cd "${dgxp:?}"; }
dotf() { cd "${dotf:?}"; }
down() { cd ~/Downloads; }
dspd() { cd "${dspd:?}"; }
dspg() { cd "${dspg:?}"; }
dspc() { cd "${dspc:?}"; }
dtf() { cd "${dtf:?}"; }
dtfg() { cd "${dtfg:?}"; }
notes() { cd "${dev:?}/notes/notes"; }
rr() { cd "$HOME/.config/rash"; }
tmp() { cd /tmp; }
utils() { cd "${dec:?}/utils"; }
vesmir() { cd "${der:?}/vesmir"; }
yas() { cd "${dev:?}/yasnippet"; }
# Quick jump back
cd-() { cd -; }
cdd() { cd -; }
# --- Centralized "change-to-project-directory" shortcuts ---
# -A make associative arrays
# declare -A CDMAP=(
# # variable-based dirs (fail fast if unset)
# # [latest]="$HOME/.cache/guix/checkouts/${latestRepo:?}"
# [owid]="${dec:?}/owid"
# [trackle]="${dev:?}/trackle"
# [ufo]="${dec:?}/ufo"
# [utils]="${dec:?}/utils"
# [zark]="${dec:?}/zark"
# )
cdx() {
local key=$1
if [[ -z $key ]]; then
printf 'Usage: cdx <shortcut>\nAvailable: %s\n' "${!CDMAP[@]}" | sort
return 1
fi
local target=${CDMAP[$key]}
if [[ -z $target ]]; then
printf 'cdx: unknown shortcut "%s"\n' "$key" >&2
return 1
fi
cd -- "$target" || return
}
# Usage
# $ cdx corona # goes to $dec/corona_cases
# $ cdx rr # goes to ~/.config/rash
# $ cdx latest # goes to ~/.cache/guix/checkouts/$latestRepo
# Benefits
# Single function (cdx) instead of dozens of small ones.
# Associative array keeps shortcuts centralized and easy to extend.
# Usage hint if you run cdx with no args.
# Error message if you mistype a key.
# --- Bash completion for cdx ---
_cdx_completions() {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "${!CDMAP[@]}" -- "$cur") )
}
complete -F _cdx_completions cdx
# Usage
# $ cdx <TAB>
# latest owid trackle ufo utils zark rr
# Create a quick test repo
create_test_repo() {
mkdir foo && cd foo || return
git init
git config user.email "${GIT_AUTHOR_EMAIL:-x@y.com}"
git config user.name "${GIT_AUTHOR_NAME:-Jim Beam}"
echo "some content" > foo.txt
git add foo.txt
git commit -m "1st commit"
"${EDITOR:-emacs -nw}" foo.txt
}
#### home-bash-configuration -> .bashrc_additions: end