-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
499 lines (415 loc) · 14.4 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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
if [[ -f "$HOME/.zshrc_local_pre" ]]; then
source "$HOME/.zshrc_local_pre"
fi
export SHELL="/bin/zsh"
autoload -U colors && colors
setopt promptsubst
setopt histignorealldups
setopt sharehistory
setopt alwaystoend
setopt automenu
setopt noautolist
setopt nobeep
setopt incappendhistory
setopt histsavenodups
setopt nocaseglob
path_abbrev() {
local full_path=${PWD/#$HOME/\~}
local path_parts=("${(s:/:)full_path}")
local result=""
if [[ ${#path_parts[@]} -eq 1 ]]; then
# If there's only one component (root or home), just return it
echo $full_path
return
fi
if [[ ${path_parts[1]} == "~" ]]; then
result="~/"
else
result="/"
fi
for ((i=2; i<${#path_parts[@]}; i++)); do
if [[ -n ${path_parts[i]} ]]; then
result+="${path_parts[i]:0:1}/"
else
result+="/"
fi
done
# Add the last component without a trailing slash
result+="${path_parts[-1]}"
echo $result
}
# Function to get git information
git_prompt_info() {
local ref
if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
echo "(${ref#refs/heads/})"
fi
}
# Function to get git status
git_prompt_status() {
local STATUS=""
local -a FLAGS
FLAGS=('--porcelain')
if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
if [[ $POST_1_7_2_GIT -gt 0 ]]; then
FLAGS+='--ignore-submodules=dirty'
fi
if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" == "true" ]]; then
FLAGS+='--untracked-files=no'
fi
STATUS=$(command git status ${FLAGS} 2> /dev/null | tail -n1)
fi
if [[ -n $STATUS ]]; then
echo "%{$fg[red]%}"
else
echo "%{$fg[green]%}"
fi
}
# Function to load environment variables from a file, defaults to .env
function loadenv() {
local env_file="${1:-.env}" # Use first argument if provided, otherwise default to .env
if [[ -f "$env_file" ]]; then
echo "Loading environment variables from: $env_file"
# Store original environment
local orig_env=$(env)
# Load the environment file
setopt allexport
source "$env_file"
unsetopt allexport
# Compare and show new/modified variables
echo "\nNewly set/modified environment variables:"
echo "======================================="
local new_env=$(env)
local new_vars=()
while IFS= read -r line; do
if ! echo "$orig_env" | grep -Fq "$line"; then
local var_name="${line%%=*}"
echo "$var_name=${(P)var_name}"
fi
done < <(echo "$new_env")
echo "\nEnvironment variables loaded successfully"
return 0
else
echo "Error: Environment file '$env_file' not found"
return 1
fi
}
# Set the prompt
PS1='%B%{$fg[green]%}%n@%{$fg[green]%}%m%{$reset_color%}:%B%{$fg[blue]%}$(path_abbrev)%{$reset_color%}$(git_prompt_status)$(git_prompt_info)%{$reset_color%}%{$fg[red]%}%(?..%B[%?])%{$reset_color%}%% '
# Function to set the terminal title
set_terminal_title() {
local title="$1"
if (( ${#title} > 20 )); then
title="...${title: -17}"
fi
title=$(printf '%.20s' "${title}")
print -Pn "\e]0;$title\a"
}
# Function to be executed before each prompt
precmd_terminal_title() {
set_terminal_title "$(path_abbrev)"
}
# Function to be executed just before a command is executed
preexec_terminal_title() {
local cmd="$1"
# Remove leading environment variables or sudo
# cmd="${cmd#*=}"
# cmd="${cmd#sudo }"
# Truncate the command if it's too long
set_terminal_title "$cmd"
}
# Add our functions to the respective hook arrays
precmd_functions+=(precmd_terminal_title)
preexec_functions+=(preexec_terminal_title)
bindkey -v
bindkey "^P" up-line-or-search
bindkey "^N" down-line-or-search
bindkey "^R" history-incremental-pattern-search-backward
bindkey "^A" vi-beginning-of-line
bindkey "^E" vi-end-of-line
bindkey -M viins '^[[A' up-line-or-history
bindkey -M viins '^[[B' down-line-or-history
bindkey -M viins '^[[D' backward-char
bindkey -M viins '^[[C' forward-char
bindkey -M viins '^?' backward-delete-char
bindkey -M viins '^[[3~' delete-char
export KEYTIMEOUT=1
cursor_mode() {
# See https://ttssh2.osdn.jp/manual/4/en/usage/tips/vim.html for cursor shapes
cursor_block='\e[2 q'
cursor_beam='\e[6 q'
function zle-keymap-select {
if [[ ${KEYMAP} == vicmd ]] ||
[[ $1 = 'block' ]]; then
echo -ne $cursor_block
elif [[ ${KEYMAP} == main ]] ||
[[ ${KEYMAP} == viins ]] ||
[[ ${KEYMAP} = '' ]] ||
[[ $1 = 'beam' ]]; then
echo -ne $cursor_beam
fi
}
zle-line-init() {
echo -ne $cursor_beam
}
zle -N zle-keymap-select
zle -N zle-line-init
}
cursor_mode
zmodload zsh/complist
bindkey -M menuselect '^M' accept-line
# bindkey -M menuselect '^O' history-incremental-search-forward
bindkey -M menuselect '^O' vi-insert
bindkey -M menuselect '^H' vi-backward-char
bindkey -M menuselect '^K' vi-up-line-or-history
bindkey -M menuselect '^J' vi-down-line-or-history
bindkey -M menuselect '^L' vi-forward-char
bindkey -M menuselect '^?' backward-delete-char
bindkey -M menuselect '^[[Z' reverse-menu-complete
bindkey -M menuselect '^[' undo
# Keep 1000 lines of history within the shell and save it to ~/.zsh_history:
HISTSIZE=1000
SAVEHIST=1000
HISTFILE=~/.zsh_history
fpath=(~/.docker/completions \\$fpath)
autoload -Uz compinit && compinit
autoload -Uz bashcompinit && bashcompinit
_comp_options+=(globdots)
zstyle ':completion:*:*:*:*:descriptions' format '%F{green}-- %d --%f'
zstyle ':completion:*:*:*:*:corrections' format '%F{yellow}!- %d (errors: %e) -!%f'
zstyle ':completion:*:messages' format ' %F{purple} -- %d --%f'
zstyle ':completion:*:warnings' format ' %F{red}-- no matches found --%f'
zstyle ':completion:*' completer _expand_alias _expand _complete _correct _approximate
zstyle ':completion:*' group-name ''
zstyle ':completion:*' menu select
eval "$(dircolors -b)"
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' list-prompt ''
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' select-prompt ''
zstyle ':completion:*' use-compctl false
zstyle ':completion:*' verbose true
zstyle ':completion:*' file-sort modification
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd'
autoload -Uz edit-command-line
zle -N edit-command-line
bindkey -M vicmd v edit-command-line
import_miniconda() {
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$("$HOME/miniforge3/bin/conda" 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "$HOME/miniforge3/etc/profile.d/conda.sh" ]; then
. "$HOME/miniforge3/etc/profile.d/conda.sh" # commented out by conda initialize
else
export PATH="$HOME/miniforge3/bin:$PATH" # commented out by conda initialize
fi
fi
unset __conda_setup
# <<< conda initialize <<<
}
# Grep alias with improved options
alias regrep='grep -IiErn --color=auto --exclude=\*~ --exclude=\*.pyc --exclude-dir=.\* --exclude-dir=__\* --exclude-dir=node_modules'
# Function to find files with a substring in their name
find_files_with_substring() {
find . -iname "*$1*" -not -path '*/\.*' -not -path '*/__*' -not -path '*/node_modules/*'
}
# Alias for the function
alias fh='find_files_with_substring'
# System update alias with error handling and colorful output
alias update='sudo apt update && \
sudo apt -y dist-upgrade && \
sudo apt -y autoremove && \
sudo fwupdmgr update && \
sudo snap refresh && \
echo -e "\e[31m$(sudo snap refresh --list)\e[0m" && \
{ [ -f /var/run/reboot-required ] && echo "\e[31mReboot required\e[0m" || echo "\e[32mNo reboot needed\e[0m"; } || \
echo "\e[33mWarning: Some commands may have failed\e[0m"'
# Tmux aliases
alias ta="tmux a -t" # Attach to tmux session
alias tl="tmux ls" # List tmux sessions
# Improved ls alias
alias ll='ls -alh' # List all files in long format, human-readable sizes
# Git aliases
alias gd='git diff'
alias gst='git status'
alias gp='git push'
alias gl='git pull'
alias gau='git add -u'
alias gcm='git commit -m'
alias gb='git branch'
alias gco='git checkout'
alias glg='git log --oneline --graph --decorate --all' # Pretty git log with all branches
# File and directory management
alias c='clear'
alias h='history'
alias df='df -h'
alias du='du -h'
# Navigation shortcuts
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
# Network related
alias myip='curl http://ipecho.net/plain; echo'
alias ports='netstat -tulanp'
# Process management
alias psa='ps auxf'
alias psg='ps aux | grep -v grep | grep -i -e VSZ -e'
# umask 0077
export EDITOR=vim
# export LC_ALL=en_US.UTF-8
# export LANG=en_US.UTF-8
# export LANGUAGE=en_US.UTF-8
# export LC_ALL=C.utf-8
export COLORTERM=truecolor
export MAKEFLAGS="-j11"
export CFLAGS="-march=native -O3"
export CXXFLAGS="-march=native -O3"
########################################################################################
_hydra_bash_completion()
{
words=($COMP_LINE)
if [ "${words[0]}" == "python" ]; then
if (( ${#words[@]} < 2 )); then
return
fi
file_path=$(pwd)/${words[1]}
if [ ! -f "$file_path" ]; then
return
fi
grep "@hydra.main" $file_path -q
helper="${words[0]} ${words[1]}"
else
helper="${words[0]}"
true
fi
EXECUTABLE=($(command -v $helper))
if [ "$HYDRA_COMP_DEBUG" == "1" ]; then
printf "EXECUTABLE_FIRST='${EXECUTABLE[0]}'\n"
fi
if ! [ -x "${EXECUTABLE[0]}" ]; then
false
fi
if [ $? == 0 ]; then
choices=$( COMP_POINT=$COMP_POINT COMP_LINE=$COMP_LINE $helper -sc query=bash)
word=${words[$COMP_CWORD]}
if [ "$HYDRA_COMP_DEBUG" == "1" ]; then
printf "\n"
printf "COMP_LINE='$COMP_LINE'\n"
printf "COMP_POINT='$COMP_POINT'\n"
printf "Word='$word'\n"
printf "Output suggestions:\n"
printf "\t%s\n" ${choices[@]}
fi
COMPREPLY=($( compgen -o nospace -o default -W "$choices" -- "$word" ));
fi
}
hydra_completion() {
export _HYDRA_OLD_COMP=$(complete -p python 2> /dev/null)
COMP_WORDBREAKS=${COMP_WORDBREAKS//=}
COMP_WORDBREAKS=$COMP_WORDBREAKS complete -o nospace -o default -F _hydra_bash_completion python
}
########################################################################################
# from https://github.com/kadaan/per-directory-history/blob/master/per-directory-history.zsh
# An implementation of per-directory history.
# See README.md for more information.
[[ -z $_per_directory_history_is_global ]] && _per_directory_history_is_global=true
[[ -z $PER_DIRECTORY_HISTORY_BASE ]] && PER_DIRECTORY_HISTORY_BASE="$HOME/.zsh_history_dirs"
[[ -z $PER_DIRECTORY_HISTORY_FILE ]] && PER_DIRECTORY_HISTORY_FILE="zsh-per-directory-history"
[[ -z $PER_DIRECTORY_HISTORY_TOGGLE ]] && PER_DIRECTORY_HISTORY_TOGGLE='^g'
#-------------------------------------------------------------------------------
# toggle global/directory history used for searching - alt-l by default
#-------------------------------------------------------------------------------
function per-directory-history-toggle-history() {
if $_per_directory_history_is_global
then
_per-directory-history-set-directory-history
zle -I
echo "using local history"
else
_per-directory-history-set-global-history
zle -I
echo "using global history"
fi
}
autoload per-directory-history-toggle-history
zle -N per-directory-history-toggle-history
bindkey $PER_DIRECTORY_HISTORY_TOGGLE per-directory-history-toggle-history
bindkey -M vicmd $PER_DIRECTORY_HISTORY_TOGGLE per-directory-history-toggle-history
#-------------------------------------------------------------------------------
# implementation details
#-------------------------------------------------------------------------------
_per_directory_history_path="$PER_DIRECTORY_HISTORY_BASE${PWD:A}/$PER_DIRECTORY_HISTORY_FILE"
function _per-directory-history-change-directory() {
_per_directory_history_path="$PER_DIRECTORY_HISTORY_BASE${PWD:A}/$PER_DIRECTORY_HISTORY_FILE"
if ! $_per_directory_history_is_global
then
fc -P
mkdir -p ${_per_directory_history_path:h}
fc -p $_per_directory_history_path
fi
}
function _per-directory-history-addhistory() {
# respect hist_ignore_space
if [[ -o hist_ignore_space ]] && [[ "$1" == \ * ]]
then
return
fi
# Can't write to history (print -S) from addhistory,
# save command to be added later from preexec hook
_per_directory_history_pending_cmd="${1%%$'\n'}"
}
_per_directory_history_last_cmd=''
function _per-directory-history-preexec() {
if [[ -v _per_directory_history_pending_cmd ]]
then
if [[ "$_per_directory_history_pending_cmd" != "$_per_directory_history_last_cmd" ]]
then
local fn
if $_per_directory_history_is_global
then
mkdir -p ${_per_directory_history_path:h}
fn=$_per_directory_history_path
else
fn=$_per_directory_history_main_histfile
fi
fc -p
print -Sr -- $_per_directory_history_pending_cmd
SAVEHIST=1
fc -A $fn
fc -P
_per_directory_history_last_cmd=$_per_directory_history_pending_cmd
fi
unset _per_directory_history_pending_cmd
fi
}
function _per-directory-history-set-directory-history() {
fc -P
mkdir -p ${_per_directory_history_path:h}
fc -p $_per_directory_history_path
_per_directory_history_is_global=false
}
function _per-directory-history-set-global-history() {
fc -P
fc -p $_per_directory_history_main_histfile
_per_directory_history_is_global=true
}
#add functions to the exec list for chpwd and zshaddhistory
autoload -U add-zsh-hook
add-zsh-hook chpwd _per-directory-history-change-directory
add-zsh-hook zshaddhistory _per-directory-history-addhistory
add-zsh-hook preexec _per-directory-history-preexec
_per_directory_history_main_histfile=$HISTFILE
unset HISTFILE
_per-directory-history-set-directory-history
########################################################################################
if [[ -f "$HOME/.zshrc_local_post" ]]; then
source "$HOME/.zshrc_local_post"
fi