-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
343 lines (303 loc) · 10.2 KB
/
.zshrc
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
# .zshrc
bindkey -v # use vim keybindings
zstyle :compinstall filename '/home/me/.zshrc'
autoload -Uz compinit
compinit -u
# display if tab completion has no match
zstyle ':completion:*:warnings' format '%F{red}No matches%f'
ZSH_HOME=/home/me/sync/config/zsh
source "$ZSH_HOME/git.zsh"
if [ -f "/usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh" ]; then
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
else
source "$ZSH_HOME/zsh-autosuggestions.zsh"
fi
if [ -f "/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]; then
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
else
source "$ZSH_HOME/zsh-syntax-highlighting.zsh"
fi
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=magenta'
source "$ZSH_HOME/command-not-found.zsh"
source "$ZSH_HOME/agnoster.zsh-theme"
# initialize zoxide; cd ("z") alternative
if command -v zoxide 2>&1 >/dev/null ; then
eval "$(zoxide init zsh)"
else
echo "zoxide not available"
fi
# enable completion for ".."
zstyle ':completion:*' special-dirs true
#PROMPT='%F{green}%n%f@%F{magenta}%m%f %F{red}%B%~%b%f %# '
#RPROMPT='[%F{yellow}%?%f]'
setopt prompt_subst # allow functions in PROMPT
setopt interactivecomments # allow comments outside of scripts
setopt autocd # use "cd" in front of stand-alone path
HISTFILE=~/.zhistory
HISTSIZE=200000
SAVEHIST=100000
setopt hist_ignore_space # ignore commands in history starting with at least one space
#setopt share_history # share history between multiple instances; do not set inc_append_history at the same time
setopt inc_append_history # immediately write to history file, do not wait until exit
setopt hist_ignore_dups # do not record consecutive duplicates
setopt hist_expire_dups_first # delete duplicate entries first when trimming history
# always start in command mode (vicmd) instead of insert mode (viins)
#zle-line-init() { zle -K vicmd; }
#zle -N zle-line-init
#alias ls='ls --color=auto'
if command -v eza 2>&1 >/dev/null ; then
alias ls='eza --header'
fi
alias l='ls'
alias la='ls -a'
alias ll='ls -l'
alias mv='mv -vi'
alias cp='cp -vi'
alias ln='ln -v'
alias mkdir='mkdir -v'
if command -v nvim 2>&1 >/dev/null ; then
alias vim='nvim'
fi
alias sudo='sudo ' # enable alias checking after sudo
alias lessh='LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s" less -R ' # enable src highlighting in less
#alias venv='source venv/bin/activate' # use functions to allow parameters
#alias open='xdg-open' # use functions to allow parameters
# generate shell completions
compdef _gnu_generic dua
compdef _gnu_generic eza
compdef _gnu_generic blkid
venv() {
function get_venv_path()
{
venv_path="${1}"
if [ ! "${venv_path}" ]; then
venv_path="venv"
fi
echo "${venv_path}"
}
function activate_venv()
{
venv_path="${1}"
source "${venv_path}"/bin/activate
}
if [ "${1}" = "exit" ]; then
deactivate
elif [ "${1}" = "create" ]; then
venv_path="$(get_venv_path "${2}")"
python -m venv "${venv_path}"
activate_venv "${venv_path}"
elif [ "${1}" = "jupyter" ]; then
venv_path="$(get_venv_path "${2}")"
activate_venv "${venv_path}"
pip install jupyter jupyterlab-lsp
else
venv_path="$(get_venv_path "${1}")"
activate_venv "${venv_path}"
fi
}
open() {
if [ ! "${OPEN_LIMIT}" ]; then
OPEN_LIMIT=16
fi
if [ $# -gt "${OPEN_LIMIT}" ]; then
echo "Trying to open ${#} different files - limit is set to ${OPEN_LIMIT}"
return
fi
for arg in "${@}"; do
xdg-open "${arg}" & # start them in parallel in background
done
for arg in "${@}"; do
fg # bring them to foreground
done
}
update() {
# in case of failure, run:
# sudo rm -vi /etc/pacman.d/gnupg &&
# sudo pacman-key --init &&
# sudo pacman-key --populate
if [ "${1}" = "mirror" ] || [ "${1}" = "full" ]; then
reflector --protocol https --latest 16 --fastest 8 --age 24 --sort rate --country France,Germany,Switzerland,Austria --verbose > /tmp/mirrorlist &&
sudo \mv -i /tmp/mirrorlist /etc/pacman.d/mirrorlist &&
elif [ "${1}" = "full" ]; then
sudo pkgfile --update &&
fi
systemd-inhibit sudo pacman -Sy --noconfirm archlinux-keyring &&
systemd-inhibit sudo pacman -Su
}
merge_dirs() {
local src="${1}"
local dest="${2}"
if [ "${3}" = "-v" ] || [ "${4}" = "-v" ]; then
local verbose=1
elif [ "${3}" = "-vv" ] || [ "${4}" = "-vv" ]; then
local verbose=2
elif [ "${3}" = "-vvv" ] || [ "${4}" = "-vvv" ]; then
local verbose=3
fi
if [ "${3}" = "--dry-run" ] || [ "${4}" = "--dry-run" ]; then
echo "Do not perform filesystem changes"
local dry_run=1
fi
local function merge_file() {
local srcfile="${file}"
local file="${file#"${src}/"}"
if ! [ -f "${srcfile}" ]; then
if [[ ${verbose} -ge 2 ]]; then
echo "Not a file ${srcfile} - skipping..."
fi
continue
fi
if [[ ${verbose} -ge 3 ]]; then
echo "Processing ${srcfile}..."
fi
local destfile="${dest}/${file}"
if [ -e "${destfile}" ]; then
if [ -f "${destfile}" ]; then
if diff -q --binary "${srcfile}" "${destfile}" 2>&1 >/dev/null ; then
if [[ ${verbose} -ge 3 ]]; then
echo "Discarding ${srcfile}..."
fi
if [ ! ${dry_run} ]; then
local trashdest="/tmp/merge_dirs_trash"
\mkdir -p "${trashdest}/$(dirname "${file}")"
\mv "${srcfile}" "${trashdest}/${file}"
fi
echo "discard ${srcfile}" >> "/tmp/merge_dirs_log"
else
if [[ ${verbose} -ge 3 ]]; then
echo "Conflicting ${srcfile}..."
fi
echo "conflict ${srcfile}" >> "/tmp/merge_dirs_log"
fi
else
if [[ ${verbose} -ge 3 ]]; then
echo "Type conflicting ${srcfile}..."
fi
echo "typechange ${srcfile}" >> "/tmp/merge_dirs_log"
fi
else
if [[ ${verbose} -ge 1 ]]; then
local mkdir_options="-v"
local mv_options="-v"
fi
if [ ! ${dry_run} ]; then
local destdir="$(dirname "${destfile}")"
\mkdir -p ${mkdir_options} "${destdir}"
\mv ${mv_options} "${srcfile}" "${destfile}"
fi
echo "move ${srcfile}" >> "/tmp/merge_dirs_log"
fi
}
echo "====== START $(date) ======" >> "/tmp/merge_dirs_log"
for file in "${src}"/**/*(N) ; do
merge_file "${file}"
done
for file in "${src}"/**/.*(N) ; do
merge_file "${file}"
done
for file in "${src}"/**/.*/**/*(N) ; do
merge_file "${file}"
done
for file in "${src}"/**/.*/**/.*(N) ; do
merge_file "${file}"
done
}
resume() {
if [ "${1}" ]; then
bg # continue in background
else
fg # continue in foreground
fi
}
git_setup_repo() {
git_url="${1}"
target_directory="${2}"
if [ ! "${git_url}" ]; then
echo "Please specify git url as first argument!"
return
fi
if [ ! "${target_directory}" ]; then
target_directory="$(basename "${git_url}")"
target_directory="${target_directory%.git}"
echo "No target directory specified - falling back to '${target_directory}'!"
fi
if [ -d "${target_directory}" ]; then
echo "'${target_directory}' does already exist!"
return
fi
git clone --bare "${git_url}" "${target_directory}/.git" &&
cd "${target_directory}" &&
default_branch="$(git remote show origin | grep 'HEAD branch: ' | cut -d ' ' -f 5)" &&
default_branch="$(echo "${default_branch}" | sed -e 's/\//_/g')" &&
git worktree add "${default_branch}" "${default_branch}" &&
cd "${default_branch}" &&
git submodule update --init --recursive --progress
}
git_add_fork() {
new_origin="${1}"
if [ ! "${new_origin}" ]; then
echo "Please specify git url of fork as first argument!"
return
fi
upstream_remote="$(git remote get-url origin)" &&
git remote set-url origin "${new_origin}" &&
git remote add upstream "${upstream_remote}" &&
git remote -v &&
git fetch --all
}
docker_latest_bash() {
docker exec -it "$(docker ps --latest --format "{{.ID}}")" bash
}
# custom shortcuts
bindkey '^[c' vi-cmd-mode # alt-c: enter cmd mode
# search history:
bindkey '^P' history-beginning-search-backward
bindkey '^N' history-beginning-search-forward
# OR
_history-incremental-search-backward () {
zle .history-incremental-search-backward $BUFFER
}
zle -N history-incremental-search-backward _history-incremental-search-backward
# this line is actually not necessary since this is default.
bindkey '^R' history-incremental-search-backward
# create a zkbd compatible hash;
# to add other keys to this hash, see: man 5 terminfo
typeset -g -A key
key[Home]="${terminfo[khome]}"
key[End]="${terminfo[kend]}"
key[Insert]="${terminfo[kich1]}"
key[Backspace]="${terminfo[kbs]}"
key[Delete]="${terminfo[kdch1]}"
key[Up]="${terminfo[kcuu1]}"
key[Down]="${terminfo[kcud1]}"
key[Left]="${terminfo[kcub1]}"
key[Right]="${terminfo[kcuf1]}"
key[PageUp]="${terminfo[kpp]}"
key[PageDown]="${terminfo[knp]}"
key[Shift-Tab]="${terminfo[kcbt]}"
key[Control-Left]="${terminfo[kLFT5]}"
key[Control-Right]="${terminfo[kRIT5]}"
# setup key accordingly
[[ -n "${key[Home]}" ]] && bindkey -- "${key[Home]}" beginning-of-line
[[ -n "${key[End]}" ]] && bindkey -- "${key[End]}" end-of-line
[[ -n "${key[Insert]}" ]] && bindkey -- "${key[Insert]}" overwrite-mode
[[ -n "${key[Backspace]}" ]] && bindkey -- "${key[Backspace]}" backward-delete-char
[[ -n "${key[Delete]}" ]] && bindkey -- "${key[Delete]}" delete-char
[[ -n "${key[Up]}" ]] && bindkey -- "${key[Up]}" up-line-or-history
[[ -n "${key[Down]}" ]] && bindkey -- "${key[Down]}" down-line-or-history
[[ -n "${key[Left]}" ]] && bindkey -- "${key[Left]}" backward-char
[[ -n "${key[Right]}" ]] && bindkey -- "${key[Right]}" forward-char
[[ -n "${key[PageUp]}" ]] && bindkey -- "${key[PageUp]}" beginning-of-buffer-or-history
[[ -n "${key[PageDown]}" ]] && bindkey -- "${key[PageDown]}" end-of-buffer-or-history
[[ -n "${key[Shift-Tab]}" ]] && bindkey -- "${key[Shift-Tab]}" reverse-menu-complete
[[ -n "${key[Control-Left]}" ]] && bindkey -- "${key[Control-Left]}" backward-word
[[ -n "${key[Control-Right]}" ]] && bindkey -- "${key[Control-Right]}" forward-word
# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then
autoload -Uz add-zle-hook-widget
function zle_application_mode_start { echoti smkx }
function zle_application_mode_stop { echoti rmkx }
add-zle-hook-widget -Uz zle-line-init zle_application_mode_start
add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop
fi