-
Notifications
You must be signed in to change notification settings - Fork 0
/
.common.shrc
157 lines (136 loc) · 4.26 KB
/
.common.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
#!/usr/bin/env bash
source $HOME/.shinit
[ -s $HOME/.openai.shrc ] && source $HOME/.openai.shrc # export OPENAI_API_KEY
# Put all configurations that are not universally applicable into this file.
[ -s $HOME/.machine_specific.shrc ] && source $HOME/.machine_specific.shrc
zsh_completion_path=$HOME/.local/share/zsh/vendor-completions
bash_completion_path=$HOME/.local/share/bash-completion/completions
if [[ ! $fpath =~ $zsh_completion_path ]]; then
fpath=($zsh_completion_path $fpath)
fi
if [[ ! $FPATH =~ $bash_completion_path ]]; then
FPATH=$bash_completion_path:$FPATH
fi
alias ds="{du -sh *; du -sh ./} | sort -rh " #diskspace
alias normalize_unicode='convmv -f utf-8 -t utf-8 --notest --nfc'
palette() {
for i in {0..255}; do print -Pn "%K{$i} %k%F{$i}${(l:3::0:)i}%f " ${${(M)$((i%6)):#3}:+$'\n'}; done
}
backup() {
case $1 in
-h|--help|'')
printf "Usage: %s files|folders...\n" "$0"
return 0
;;
*)
for it in "$@"
do
cp "${it}"{,.bak}
done
;;
esac
}
# Search all file types including pdf, ppt, and open grepped files
grepOpen() {
RG_PREFIX="rga --files-with-matches"
local file
file="$(
FZF_DEFAULT_COMMAND="$RG_PREFIX '$1'" \
fzf --sort --preview="[[ ! -z {} ]] && rga --pretty --context 5 {q} {}" \
--phony -q "$1" \
--bind "change:reload:$RG_PREFIX {q}" \
--preview-window="70%:wrap"
)" &&
echo "opening $file" &&
open "$file"
}
# Search filenames and open them
findOpen() {
local file
file="$(fzf)" &&
echo "opening $file" &&
open "$file"
}
transfer() {
local url=https://transfer.sixtyfive.me
if [ $# -eq 0 ]
then
echo "No arguments specified.\nUsage:\n transfer <file|directory>\n ... | transfer <file_name>" >&2
return 1
fi
if tty -s
then
file="$1"
file_name=$(basename "$file")
if [ ! -e "$file" ]
then
echo "$file: No such file or directory" >&2
return 1
fi
if [ -d "$file" ]
then
file_name="$file_name.zip"
(
pushd "$file" && zip -r -q - .
) | curl --progress-bar --upload-file "-" "$url/$(urlencode $file_name)" | tee /dev/null
else
cat "$file" | curl --progress-bar --upload-file "-" "$url/$(urlencode $file_name)" | tee /dev/null
fi
else
file_name=$1
curl --progress-bar --upload-file "-" "$url/$(urlencode $file_name)" | tee /dev/null
fi
}
# Advanced fzf configuration
# Define common exclusions
_fzf_exclusions=(".git" "node_modules" "__pycache__" ".venv")
# Check if the 'bfs' command exists, otherwise fallback to 'fd'
if command -v bfs &> /dev/null; then
# Create a function to generate the exclusion options for bfs
_fzf_bfs_exclude() {
local exclude_expr=""
for dir in "${_fzf_exclusions[@]}"; do
exclude_expr+=" -name \"$dir\" -prune -or"
done
echo "$exclude_expr"
}
# Invoked when a user types `<command> **<tab>`, where <command> is not `cd`
_fzf_compgen_path() {
eval bfs . -follow -hidden $( _fzf_bfs_exclude ) -type f -print 2>/dev/null
}
# Invoked when a user types `cd **<tab>`
_fzf_compgen_dir() {
eval bfs . -follow -hidden $( _fzf_bfs_exclude ) -type d -print 2>/dev/null
}
else
# Create a function to generate the exclusion options for fd
_fzf_fd_exclude() {
local exclude_opts=""
for dir in "${_fzf_exclusions[@]}"; do
exclude_opts+=" --exclude \"$dir\""
done
echo "$exclude_opts"
}
# Invoked when a user types `<command> **<tab>`, where <command> is not `cd`
_fzf_compgen_path() {
eval fd $( _fzf_fd_exclude ) --type f --hidden --follow . "$1" 2>/dev/null
}
# Invoked when a user types `cd **<tab>`
_fzf_compgen_dir() {
eval fd $( _fzf_fd_exclude ) --type d --hidden --follow . "$1" 2>/dev/null
}
fi
# Advanced customization of fzf options via _fzf_comprun function
# - The first argument to the function is the name of the command.
# - You should make sure to pass the rest of the arguments to fzf.
_fzf_comprun() {
__fzf_comprun__command__=$1
shift
case "$__fzf_comprun__command__" in
cd) fzf --preview 'tree -C {} | head -200' "$@" ;;
export | unset) fzf --preview "eval 'echo \$'{}" "$@" ;;
ssh) fzf --preview 'dig {}' "$@" ;;
*) fzf --preview 'bat -n --color=always {}' "$@" ;;
esac
}
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion