blob: b0a6c7a5d52a37b97d7eb19345818253a551a2c5 (
plain)
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
# ███████╗███████╗██╗ ██╗██████╗ ██████╗
# ╚══███╔╝██╔════╝██║ ██║██╔══██╗██╔════╝
# ███╔╝ ███████╗███████║██████╔╝██║
# ███╔╝ ╚════██║██╔══██║██╔══██╗██║
# ███████╗███████║██║ ██║██║ ██║╚██████╗
# ╚══════╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
if [[ -n "$SSH_CLIENT" ]]; then
export KEYTIMEOUT=1
else
export KEYTIMEOUT=15
fi
# Tmux default session
if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ]; then
tmux a -t tmux || exec tmux new -s tmux && exit;
fi
# Enable various options
setopt interactive_comments beep extendedglob nomatch notify completeinword prompt_subst
########## Vi mode ##########
bindkey -v
bindkey -M viins '^?' backward-delete-char
bindkey -M viins '^[[3~' delete-char
bindkey -M vicmd '^[[3~' delete-char
bindkey -v '^?' backward-delete-char
bindkey -r '\e/'
bindkey -s jk '\e'
bindkey "^W" backward-kill-word
bindkey "^H" backward-delete-char # Control-h also deletes the previous char
bindkey "^U" backward-kill-line
bindkey "^[j" history-search-forward # or you can bind it to the down key "^[[B"
bindkey "^[k" history-search-backward # or you can bind it to Up key "^[[A"
# Define the 'autosuggest-execute' and 'autosuggest-accept' ZLE widgets
autoload -Uz autosuggest-execute autosuggest-accept
zle -N autosuggest-execute
zle -N autosuggest-accept
bindkey '^X' autosuggest-execute
bindkey '^Y' autosuggest-accept
# Edit line in vim with alt-e
autoload edit-command-line; zle -N edit-command-line
bindkey '^e' edit-command-line
bindkey '^[e' edit-command-line # alt + e
# Allow CTRL+D to exit zsh with partial command line (non empty line)
exit_zsh() { exit }
zle -N exit_zsh
bindkey '^D' exit_zsh
# Auto-completion
autoload -Uz compinit
if [[ -n ${ZDOTDIR}/.zcompdump(#qN.mh+24) ]]; then
compinit;
else
compinit -C;
fi;
# Accept completion with <tab> or Ctrl+i and go to next/previous suggestions with Vi like keys: Ctrl+n/p
zmodload -i zsh/complist
accept-and-complete-next-history() {
zle expand-or-complete-prefix
}
zle -N accept-and-complete-next-history
bindkey -M menuselect '^i' accept-and-complete-next-history
bindkey '^n' expand-or-complete
bindkey '^p' reverse-menu-complete
zstyle ':completion:*' menu select=1
# Some other useful functionalities
setopt autocd # Automatically cd into typed directory.
stty intr '^q' # free Ctrl+C for copy use Ctrl+q instead
stty lnext '^-' # free Ctrl+V for paste use ^- instead
stty stop undef # Disable ctrl-s to freeze terminal.
stty start undef
#unsetopt BEEP
# More history for cd and use "cd -TAB"
setopt AUTO_PUSHD # pushes the old directory onto the stack
zstyle ':completion:*:directory-stack' list-colors '=(#b) #([0-9]#)*( *)==95=38;5;12'
########## Prompt(s) ##########
# Enable colors
autoload -U colors && colors
# Prompt with Vi insert-mode/normal-mode and blinking '$', note blinking '$' only works on some terminals.
terminfo_down_sc=$terminfo[cud1]$terminfo[cuu1]$terminfo[sc]$terminfo[cud1]
git_branch_test_color() {
local ref=$(git symbolic-ref --short HEAD 2> /dev/null)
if [ -n "${ref}" ]; then
if [ -n "$(git status --porcelain)" ]; then
local gitstatuscolor='%F{196}'
else
local gitstatuscolor='%F{82}'
fi
echo "${gitstatuscolor}${ref}"
else
echo ""
fi
}
# Job indicator
jobs_status_indicator() {
local jobs_output
declare -p jobs_output >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
unset jobs_output
fi
jobs_output=$(jobs -s)
if [[ -n "$jobs_output" ]]; then
local jobs_count=$(echo "$jobs_output" | wc -l)
echo "jobs: ${jobs_count}"
fi
}
remote_indicator() {
if [[ -n "$SSH_CONNECTION" || -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then
echo 'ssh '
else
echo ''
fi
}
# Version control (git)
autoload -Uz add-zsh-hook vcs_info
zstyle ':vcs_info:*' stagedstr ' +%F{15}staged%f'
zstyle ':vcs_info:*' unstagedstr ' -%F{15}unstaged%f'
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' actionformats '%F{5}%F{2}%b%F{3}|%F{1}%a%F{5}%f '
zstyle ':vcs_info:*' formats '%F{208} '$'\uE0A0'' %f$(git_branch_test_color)%f%F{76}%c%F{3}%u%f '
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked
zstyle ':vcs_info:*' enable git
+vi-git-untracked() {
if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
[[ $(git ls-files --other --directory --exclude-standard | sed q | wc -l | tr -d ' ') == 1 ]] ; then
hook_com[unstaged]+='%F{196} !%f%F{15}untracked%f'
fi
}
# Prompt
function insert-mode() {
echo "-- INSERT --"
}
function normal-mode() {
echo "-- NORMAL --"
}
function my_precmd () {
vcs_info
PS1="%{┌─[%F{145}%n%f] %F{39}%0~%f%} ${vcs_info_msg_0_} \$(remote_indicator)\$(jobs_status_indicator)
%{%{$terminfo_down_sc$(insert-mode)$terminfo[rc]%}%{└─%{["%{$(tput setaf 226)%}""%{$(tput blink)%}"%{$%}"%{$(tput sgr0)%}"%{%G]%}%}%}%}"
}
add-zsh-hook precmd my_precmd
function set-prompt() {
if [[ ${KEYMAP} == vicmd || ${KEYMAP} == vi-cmd-mode ]]; then
echo -ne '\e[1 q'
VI_MODE=$(normal-mode)
elif [[ ${KEYMAP} == main || ${KEYMAP} == viins || ${KEYMAP} == '' ]]; then
echo -ne '\e[5 q'
VI_MODE=$(insert-mode)
fi
PS1="%{┌─[%F{145}%n%f] %F{39}%0~%f%} ${vcs_info_msg_0_} \$(remote_indicator)\$(jobs_status_indicator)
%{%{$terminfo_down_sc$VI_MODE$terminfo[rc]%}%{└─%{["%{$(tput setaf 226)%}""%{$(tput blink)%}"%{$%}"%{$(tput sgr0)%}"%{%G]%}%}%}%}"
}
function update-mode-file() {
set-prompt
local current_mode=$(cat ~/.vi-mode)
local new_mode="$VI_MODE"
if [[ "$new_mode" != "$current_mode" ]]; then
echo "$new_mode" >| ~/.vi-mode
fi
if command -v tmux &>/dev/null && [[ -n "$TMUX" ]]; then
tmux refresh-client -S
fi
}
function check-nvim-running() {
if pgrep -x "nvim" > /dev/null; then
VI_MODE=""
update-mode-file
if command -v tmux &>/dev/null && [[ -n "$TMUX" ]]; then
tmux refresh-client -S
fi
else
if [[ ${KEYMAP} == vicmd || ${KEYMAP} == vi-cmd-mode ]]; then
VI_MODE=$(normal-mode)
elif [[ ${KEYMAP} == main || ${KEYMAP} == viins || ${KEYMAP} == '' ]]; then
VI_MODE=$(insert-mode)
fi
update-mode-file
if command -v tmux &>/dev/null && [[ -n "$TMUX" ]]; then
tmux refresh-client -S
fi
fi
}
function zle-line-init() {
zle reset-prompt
case "${KEYMAP}" in
vicmd)
echo -ne '\e[1 q'
;;
main|viins|*)
echo -ne '\e[5 q'
;;
esac
}
function zle-keymap-select() {
update-mode-file
zle reset-prompt
case "${KEYMAP}" in
vicmd)
echo -ne '\e[1 q'
;;
main|viins|*)
echo -ne '\e[5 q'
;;
esac
}
preexec () { print -rn -- $terminfo[el]; echo -ne '\e[5 q' ; }
zle -N zle-line-init
zle -N zle-keymap-select
TRAPWINCH() { # Trap the WINCH signal to update the mode file on window size changes
update-mode-file
}
#function nvim-listener() {
# local prev_nvim_status="inactive"
# local nvim_pid=""
# while true; do
# local current_nvim_pid=$(pgrep -x "nvim")
# if [[ -n "$current_nvim_pid" && "$current_nvim_pid" != "$nvim_pid" ]]; then
# # Neovim started
# prev_nvim_status="active"
# nvim_pid="$current_nvim_pid"
# VI_MODE="" # Clear VI_MODE to show Neovim mode
# update-mode-file
# if command -v tmux &>/dev/null && [[ -n "$TMUX" ]]; then
# tmux refresh-client -S
# fi
# elif [[ -z "$current_nvim_pid" && "$prev_nvim_status" == "active" ]]; then
# # Neovim stopped
# prev_nvim_status="inactive"
# nvim_pid=""
# if [[ ${KEYMAP} == vicmd || ${KEYMAP} == vi-cmd-mode ]]; then
# VI_MODE=$(normal-mode)
# elif [[ ${KEYMAP} == main || ${KEYMAP} == viins || ${KEYMAP} == '' ]]; then
# VI_MODE=$(insert-mode)
# fi
# update-mode-file
# if command -v tmux &>/dev/null && [[ -n "$TMUX" ]]; then
# tmux refresh-client -S
# fi
# fi
# done
#}
# Start Neovim listener in the background
#nvim-listener &!
set-prompt
RPROMPT='%(?..[%F{196}%?%f] )'
########## Useful Commands/Alias ##########
# Define alias for nvim/vim (fallback to vim)
if command -v nvim > /dev/null; then
alias vi='nvim'
else
alias vi='vim'
fi
# Confirmation #
alias mv='mv -i'
alias cp='cp -i'
alias ln='ln -i'
alias rm='rm -i'
alias ls='ls --color=auto --group-directories-first'
alias lsd="lsd --group-directories-first"
alias grep='grep --colour=auto'
alias egrep='egrep --colour=auto'
alias fgrep='fgrep --colour=auto'
# List upto last 10 visited directories using "d" and quickly cd into any specific one
alias d="dirs -v | head -10"
# Using just a number from "0" to "9"
alias 0="cd +0"
alias 1="cd +1"
alias 2="cd +2"
alias 3="cd +3"
alias 4="cd +4"
alias 5="cd +5"
alias 6="cd +6"
alias 7="cd +7"
alias 8="cd +8"
alias 9="cd +9"
alias sc="systemctl"
alias jc="journalctl xe"
alias suspend='systemctl suspend && betterlockscreen --lock dimblur' # Suspend(sleep) and lock screen if using systemctl
alias hibernate='systemctl hibernate' # Hibernate
alias lock='DISPLAY=:0 xautolock -locknow' # Lock my workstation screen from my phone
alias oports="sudo lsof -i -P -n | grep -i 'listen'" # List open ports
alias trash_restore='gio trash --restore "$(gio trash --list | fzf | cut -f 1)"'
alias keyname="xev | sed -n 's/[ ]*state.* \([^ ]*\)).*/\1/p'"
alias wget=wget --hsts-file="$XDG_CACHE_HOME/wget-hsts" # wget does not support environment variables
alias pp='getlast 2>&1 |&tee -a output.txt'
alias -g cap='2>&1 | tee -a output.txt' # Print output of a command NOTE: Must be used in conjunction but no need for "|" symbol
#alias stashrebase='git stash save && git fetch && git rebase origin main && git stash apply'
#alias cfg-stash='config stash save && config fetch && config rebase origin main && config stash apply'
# Time aliases
alias ber='TZ=Europe/Berlin date'
alias nyc='TZ=America/New_York date'
alias sfo='TZ=America/Los_Angeles date'
alias utc='TZ=Etc/UTC date'
### Dotfiles
alias config='git --git-dir=$HOME/.cfg --work-tree=$HOME'
cfg_files=$(config ls-tree --name-only -r HEAD)
export CFG_FILES="$cfg_files"
# Set bare dotfiles repository git environment variables dynamically
function set_git_env_vars() {
# Check if the current command is a package manager command
if [[ "${(%)${(z)history[1]}}" =~ ^(pacman|yay|apt|dnf|brew|npm|pip|gem|go|cargo) ]]; then
return
fi
local git_dir="$(git rev-parse --git-dir -C . 2>/dev/null)"
if [[ -n "$git_dir" ]]; then
local is_bare="$(git -C "$git_dir" rev-parse --is-bare-repository 2>/dev/null)"
if [[ "$is_bare" == "true" ]]; then
export GIT_DIR="$HOME/.cfg"
export GIT_WORK_TREE=$(realpath $(eval echo ~))
else
unset GIT_DIR
unset GIT_WORK_TREE
fi
else
local root_dir="$(git rev-parse --show-toplevel 2>/dev/null)"
if [[ -n "$root_dir" ]]; then
unset GIT_DIR
export GIT_WORK_TREE="$root_dir"
else
export GIT_DIR="$HOME/.cfg"
export GIT_WORK_TREE=$(realpath $(eval echo ~))
fi
fi
}
# Define an auto_cd hook to automatically update Git environment variables
function chpwd() {
set_git_env_vars
}
# Call the function to set Git environment variables when the shell starts up
set_git_env_vars
function gsp
{
# Config file for subtrees
#
# Format:
# <prefix>;<remote address>;<remote branch>
# # Lines starting with '#' will be ignored
GIT_SUBTREE_FILE="$PWD/.gitsubtrees"
if [ ! -f $GIT_SUBTREE_FILE ]; then
echo "Nothing to do - file <`basename $GIT_SUBTREE_FILE`> does not exist."
return
fi
OLD_IFS=$IFS
IFS=$'\n'
for LINE in $(cat $GIT_SUBTREE_FILE); do
# Skip lines starting with '#'.
if [[ $LINE = \#* ]]; then
continue
fi
# Parse the current line.
PREFIX=`echo $LINE | cut -d';' -f 1`
REMOTE=`echo $LINE | cut -d';' -f 2`
BRANCH=`echo $LINE | cut -d';' -f 3`
# Push to the remote.
echo "config subtree pull --prefix=$PREFIX $REMOTE $BRANCH"
config subtree pull --prefix=$PREFIX $REMOTE $BRANCH
done
}
# Print previous command into a file
getlast () {
fc -nl $((HISTCMD - 1))
}
# Enter directory and list contents
cd() {
if [ -n "$1" ]; then
builtin cd "$@" && ls -pvA --color=auto --group-directories-first
else
builtin cd ~ && ls -pvA --color=auto --group-directories-first
fi
}
# cd using "up n" as a command up as many directories, example "up 3"
up() {
# default parameter to 1 if non provided
declare -i d=${@:-1}
# ensure given parameter is non-negative. Print error and return if it is
(( $d < 0 )) && (>&2 echo "up: Error: negative value provided") && return 1;
# remove last d directories from pwd, append "/" in case result is empty
cd "$(pwd | sed -E 's;(/[^/]*){0,'$d'}$;;')/";
}
# cd into $XDG_CONFIG_HOME/$1 directory
c() {
local root=${XDG_CONFIG_HOME:-~/.config}
local dname="$root/$1"
if [[ ! -d "$dname" ]]; then
return
fi
cd "$dname"
}
# Make and cd into directory and any parent directories
mkcd () {
if [[ -z "$1" ]]; then
echo "Usage: mkcd <dir>" 1>&2
return 1
fi
mkdir -p "$1"
cd "$1"
}
# Back up a file. Usage "backupthis <filename>"
backupthis() {
cp -riv $1 ${1}-$(date +%Y%m%d%H%M).backup;
}
# Let FZF use ripgrep by default
if type rg &> /dev/null; then
export FZF_DEFAULT_COMMAND='rg --files'
export FZF_DEFAULT_OPTS='-m --height 50% --border'
fi
# Spawn a clone of current terminal
putstate () {
declare +x >~/environment.tmp
declare -x >>~/environment.tmp
echo cd "$PWD" >>~/environment.tmp
}
getstate () {
. ~/environment.tmp
}
# use ctrl-z to toggle in and out of bg
function toggle_fg_bg() {
if [[ $#BUFFER -eq 0 ]]; then
BUFFER="fg"
zle accept-line
else
BUFFER=""
zle clear-screen
fi
}
zle -N toggle_fg_bg
bindkey '^Z' toggle_fg_bg
# Tmux layout
openSession () {
tmux split-window -h -t
tmux split-window -v -t
tmux resize-pane -U 5
}
# Allow nnn filemanager to cd on quit
nnn() {
declare -x +g NNN_TMPFILE=$(mktemp --tmpdir $0.XXXX)
trap "rm -f $NNN_TMPFILE" EXIT
=nnn $@
[ -s $NNN_TMPFILE ] && source $NNN_TMPFILE
}
# Extract with one command
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Kubernetes
# kubernetes aliases
if command -v kubectl > /dev/null; then
replaceNS() { kubectl config view --minify --flatten --context=$(kubectl config current-context) | yq ".contexts[0].context.namespace=\"$1\"" ; }
alias kks='KUBECONFIG=<(replaceNS "kube-system") kubectl'
alias kam='KUBECONFIG=<(replaceNS "authzed-monitoring") kubectl'
alias kas='KUBECONFIG=<(replaceNS "authzed-system") kubectl'
alias kar='KUBECONFIG=<(replaceNS "authzed-region") kubectl'
alias kt='KUBECONFIG=<(replaceNS "tenant") kubectl'
if command -v kubectl-krew > /dev/null; then
path=($XDG_CONFIG_HOME/krew/bin $path)
fi
rmfinalizers() {
kubectl get deployment "$1" -o json | jq '.metadata.finalizers = null' | kubectl apply -f -
}
fi
# Alias for android-studio
alias android-studio='/opt/android-studio/bin/studio.sh'
# NVM
#nvm() {
# local green_color
# green_color=$(tput setaf 2)
# local reset_color
# reset_color=$(tput sgr0)
# echo -e "${green_color}nvm${reset_color} $@"
#}
if [ -s "$NVM_DIR/nvm.sh" ]; then
nvm_cmds=(nvm node npm yarn)
for cmd in "${nvm_cmds[@]}"; do
alias "$cmd"="unalias ${nvm_cmds[*]} && unset nvm_cmds && . $NVM_DIR/nvm.sh && $cmd"
done
fi
########## Source Plugins, should be last ##########
#source /usr/share/nvm/init-nvm.sh
# Load fzf keybindings and completion
source /usr/local/bin/fzf/shell/key-bindings.zsh
source /usr/local/bin/fzf/shell/completion.zsh
# Suggest aliases for commands
source ~/.config/zsh/plugins/zsh-you-should-use/you-should-use.plugin.zsh
# Load zsh-syntax-highlighting
source ~/.config/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# Load fish like auto suggestions
source ~/.config/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.plugin.zsh
source ~/.config/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
|