-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc
358 lines (328 loc) · 11 KB
/
.bashrc
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
# Set directory colors
eval "$(dircolors ~/bash-utils/.dircolors)"
# Define colors
COLOR_CYAN=$(tput setaf 37); export COLOR_CYAN
COLOR_RESET=$(tput sgr0); export COLOR_RESET
# Set default editor
command -v nvim &>/dev/null && __editor=nvim || __editor=vim
export EDITOR="${__editor}"
export VISUAL="${__editor}"
alias v="${__editor}"
alias vi="${__editor}"
alias vim="${__editor}"
# Define starship config
export STARSHIP_CONFIG=$HOME/bash-utils/themes/starship
# Define pager
## do not page if output fits on screen, preserve colors, do not clear screen after quitting, smart case searching
export LESS='--quit-if-one-screen --RAW-CONTROL-CHARS --no-init --ignore-case'
export BAT_PAGER='less'
export PAGER='less'
# Easier navigation: .., ..., ...., ....., ~ and -
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ~='cd ~' # `cd` is probably faster to type though
alias -- -='cd -'
# general aliases
alias cat='bat'
alias desktop='cd $DESKTOP_DIR'
alias df='df --human-readable'
alias du='du --human-readable'
alias fdh='fd --hidden --no-ignore --exclude ".git" --follow --color=always'
alias greppath='path | grep --ignore-case'
alias gw='./gradlew' # requires gradle and gradle wrapped projects
alias here='explorer .'
alias ll='ls -l --almost-all --human-readable --color --group-directories-first'
alias ls='ls --color'
alias md='make-dir'
alias myip='curl -s ifconfig.me'
alias myipv6='curl -s ifconfig.co'
alias rd='rmdir'
alias rg='rg --smart-case'
alias rgh='rg --hidden --glob "!.git" --smart-case'
alias reload='exec bash --login'
alias pack='tar --create --gzip --verbose --file'
alias unpack='tar --extract --gunzip --verbose --file'
alias wiki='cd $WIKI_DIR'
# Determine size of a file or total size of a directory
file-size() {
if du --bytes /dev/null >/dev/null 2>&1; then
local arg="--bytes"
else
local arg=""
fi
if [[ "$#" -gt 0 ]]; then
du --human-readable --summarize "$arg" -- "$@"
else
du --human-readable --summarize "$arg" .[^.]* ./*
fi
}
alias fs='file-size'
# Lists the 20 most used commands
history-stats() {
fc -l -100000000000 | \
awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | \
grep --invert-match "./" | \
column -c 3 -s " " -t | \
sort --numeric-sort --reverse | \
nl | \
head --lines=20
}
# Show what an alias or function does
explain() {
local alias_result declarable alias_part found=false
if [[ "$#" -eq 0 ]]; then
alias_result=$(alias 2>&1)
else
alias_result=$(alias "$1" 2>&1)
fi
if [[ -n $alias_result && ! $alias_result =~ "not found" ]]; then
if hash bat &> /dev/null; then
echo "$alias_result" | bat --plain --language sh
else
echo "$alias_result"
fi
alias_part=${alias_result#*=}
[[ $alias_part =~ ^\'.*\'$ ]] && alias_part=${alias_part:1:${#alias_part}-2}
declarable=$alias_part
found=true
else
declarable="$1"
fi
if declare -f "$declarable" >/dev/null; then
if hash bat &> /dev/null; then
declare -f "$declarable" | bat --style="numbers,grid" --language sh --color always
else
declare -f "$declarable" | cat
fi
found=true
fi
if [[ $found = false ]]; then
command -v "$1"
fi
}
alias ex='explain'
## Customize history
# Show timestamp of command execution in history
HISTTIMEFORMAT="%Y-%m-%d %T "
export HISTFILE=$HOME/.cache/bash_history
export HISTSIZE=100000
export HISTFILESIZE=200000
# Append commands to history file
shopt -s histappend
alias h='history'
history-search() {
# do not print the last line, which is the current call
history | grep --ignore-case "$@" | head --lines=-1
}
alias hs='history-search'
# create a directory recursively and cd to it
make-dir() {
mkdir --parents "$@" && cd "$_";
}
path() {
echo $PATH | sed --regexp-extended 's/:/\n/g'
}
# ng autocomplete
_ng_completion () {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "" -- ${cur}) )
if [[ "$COMP_CWORD" -eq "1" ]]; then
local commands="add analytics build config doc e2e generate help lint new run serve test update version xi18n"
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
fi
}
complete -o default -F _ng_completion ng
### git
# Use Git's colored diff when available; delta is better, though
diffgit() {
git diff --no-index --color-words "$@"
}
## this is overwritten when config-environment.sh is loaded - which isn't in minimal installation
export GIT_PAGER='less'
alias g='git'
alias gc='git clone'
gd() {
local infotext diffresult git_pager diff_line2
git_pager="${GIT_PAGER}"
if [[ $1 == '-s' || $1 == '--simple' ]]; then
shift
[[ "${git_pager}" =~ ^delta ]] && git_pager="${git_pager} --features simple"
else
[[ "${git_pager}" =~ ^delta ]] && git_pager="${git_pager} --features unobtrusive-side-by-side"
fi
if [[ -n "$(git diff 2>/dev/null)" ]]; then
infotext="${COL_CYAN_FG}Showing diff${COL_RESET}"
diffresult=$(git diff --color=always "$@")
elif [[ -n "$(git diff --cached 2>/dev/null)" ]]; then
infotext="${COL_CYAN_FG}Showing staged${COL_RESET}"
diffresult=$(git diff --color=always --cached "$@")
else
infotext="${COL_CYAN_FG}Showing last commit${COL_RESET}"
diffresult=$(git show --color=always "$@")
diff_line2=$(echo "${diffresult}" | sed --silent 2p)
if [[ "${diff_line2}" =~ ^Merge: ]]; then
diff_line2="${diff_line2#* }"
diff_line2="${diff_line2/ /...}"
diffresult="${diffresult}\n\n$(git diff --color=always "${diff_line2}")"
fi
fi
echo -e "${infotext}\n${diffresult}" | eval "${git_pager}"
}
## gl and gs rely upon git aliases from .gitconfig
alias gl='git l'
alias gp='git push'
alias gs='git s'
alias gu='git pull'
# git-bash specific
alias cfg='/mingw64/bin/git --git-dir=$HOME/.git-bash --work-tree=$HOME'
alias priv='git config --global credential.helper store'
alias team='git config --global credential.helper manager-core'
push_wiki() {
priv
wiki
git commit -am "$1"
git push
team
}
# Used for the Ctrl+N shortcut
edit-current-file() {
local valid_files=() file
for file in "$@"; do
# the only way I could find to replace vars (like $HOME) and ~
file=$(eval "echo $file")
if [[ -f $file ]]; then
valid_files+=("$file")
fi
done
# shellcheck disable=SC2068
"$EDITOR" ${valid_files[@]}
}
# Edit current file with Ctrl+N
# shellcheck disable=SC2016
bind -x '"\C-n":"edit-current-file $READLINE_LINE"'
# Clear screen with Alt+Shift+L
bind -x '"\eL": printf "\ec"'
sudo-command-line() {
[[ -z $READLINE_LINE ]] && READLINE_LINE=$(fc -ln -0 | sed 's/^[ \t]*//')
if [[ $READLINE_LINE == sudo\ * ]]; then
READLINE_LINE="${READLINE_LINE#sudo }"
((READLINE_POINT-=5))
elif [[ $READLINE_LINE == $EDITOR\ * ]]; then
READLINE_LINE="${READLINE_LINE#$EDITOR }"
READLINE_LINE="sudoedit $READLINE_LINE"
local len=${#EDITOR}
((READLINE_POINT-=len+8))
elif [[ $READLINE_LINE == sudoedit\ * ]]; then
READLINE_LINE="${READLINE_LINE#sudoedit }"
READLINE_LINE="$EDITOR $READLINE_LINE"
local len=${#EDITOR}
((READLINE_POINT+=len-8))
else
READLINE_LINE="sudo $READLINE_LINE"
((READLINE_POINT+=5))
fi
}
# sudo current/last command with Ctrl+F
bind -x '"\C-f":"sudo-command-line"'
transpose_whitespace_words() {
local prefix=${READLINE_LINE:0:$READLINE_POINT}
local suffix=${READLINE_LINE:$READLINE_POINT}
# cursor is at the end of line
if [[ $suffix =~ ^[[:space:]]*$ && $prefix =~ ([^[:space:]][[:space:]]*)$ ]]; then
prefix=${prefix%${BASH_REMATCH[0]}}
suffix=${BASH_REMATCH[0]}${suffix}
fi
# cursor is in a word (prefix does not end with a space and suffix does not start with a space)
if [[ $suffix =~ ^[^[:space:]] && $prefix =~ [^[:space:]]+$ ]]; then
prefix=${prefix%${BASH_REMATCH[0]}}
suffix=${BASH_REMATCH[0]}${suffix}
fi
# suffix starts with a space
if [[ $suffix =~ ^[[:space:]]+ ]]; then
prefix=${prefix}${BASH_REMATCH[0]}
suffix=${suffix#${BASH_REMATCH[0]}}
fi
if [[ $prefix =~ ([^[:space:]]+)([[:space:]]+)$ ]]; then
local word1=${BASH_REMATCH[1]}
local space=${BASH_REMATCH[2]}
prefix=${prefix%${BASH_REMATCH[0]}}
if [[ $suffix =~ [^[:space:]]+ ]]; then
local word2=${BASH_REMATCH[0]}
suffix=${suffix#$word2}
READLINE_LINE=${prefix}$word2$space$word1$suffix
READLINE_POINT=$((${#prefix} + ${#word2} + ${#space} + ${#word1}))
fi
fi
}
# Swap words with Ctrl+G
bind -x '"\C-g": transpose_whitespace_words'
repeat_last_word() {
local prefix=${READLINE_LINE:0:$READLINE_POINT}
local suffix=${READLINE_LINE:$READLINE_POINT}
local last_word
# cursor is behind a non space
if [[ $prefix =~ [^[:space:]]+$ ]]; then
last_word=${BASH_REMATCH[0]}
# cursor is before a non space
if [[ $suffix =~ ^[^[:space:]]+ ]]; then
last_word=${last_word}${BASH_REMATCH[0]}
fi
elif [[ $prefix =~ ([^[:space:]]+)[[:space:]]+$ ]]; then
last_word=${BASH_REMATCH[1]}
fi
READLINE_LINE=${prefix}${last_word}${suffix}
READLINE_POINT=$((${#prefix} + ${#last_word}))
}
# Repeat last word with Alt+M
bind -x '"\em": repeat_last_word'
# Go directory up with Ctrl+Alt+K
# keycode 201 does not exist, so using it as a hack
# The usage of the macro hides the 'cd ..'
bind -x '"\201": cd ..'
bind '"\e\C-k":"\C-u\201\C-m"'
# Ctrl+Alt+Up (\e[1;7A) not working, using Ctrl+Up instead
bind '"\e[1;5A":"\C-u\201\C-m"'
## Directory cycle
# Make pushd silent, except for help
pushd() {
if [[ $1 = "--help" ]]; then
command pushd "$@" || return
else
command pushd "$@" > /dev/null 2>&1 || return
fi
}
cd() {
if [[ $# -eq 0 ]]; then
command cd || return
else
pushd "$@" || return
fi
}
# keycodes 202 and 203 do not exist, so using it as a hack
# The usage of the macro hides the 'pushd ..'
# Cycle backwards with Ctrl+Alt+H and Ctrl+Alt+Left
bind -x '"\202": pushd +1'
bind '"\e\C-h":"\C-u\202\C-m"'
bind '"\e[1;7D":"\C-u\202\C-m"'
# Cycle forwards with Ctrl+Alt+L and Ctrl+Alt+Right
bind -x '"\203": pushd -0'
bind '"\e\C-l":"\C-u\203\C-m"'
bind '"\e[1;7C":"\C-u\203\C-m"'
# autocomplete alternate git command
__git_complete g __git_main
### Includes
# Include last so that common aliases can be overridden in custom settings
. ~/bash-utils/fzf/fzf_config
. ~/bash-utils/fzf/completion.bash
. ~/bash-utils/fzf/key-bindings.bash
. ~/bash-utils/dockercfg.sh
. ~/bash-utils/.settings
. ~/bash-utils/project_dir.sh
. ~/bash-utils/login.sh
. ~/bash-utils/colors.sh
. ~/bash-utils/count.sh
. ~/bash-utils/themes/theme.sh
# curl -O https://raw.githubusercontent.com/rupa/z/master/z.sh ~/bash-utils && chmod +x ~/bash-utils/z.sh
. ~/bash-utils/z.sh