aboutsummaryrefslogtreecommitdiff
path: root/.config/zsh/.zshrc
blob: 65c10c82e2a8193d8dd604125e570c030e0ac049 (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

#  ███████╗███████╗██╗  ██╗██████╗  ██████╗
#  ╚══███╔╝██╔════╝██║  ██║██╔══██╗██╔════╝
#    ███╔╝ ███████╗███████║██████╔╝██║
#   ███╔╝  ╚════██║██╔══██║██╔══██╗██║
#  ███████╗███████║██║  ██║██║  ██║╚██████╗
#  ╚══════╝╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝

export PATH=$HOME/bin:/usr/local/bin:/sbin:/usr/sbin:$PATH
#export PYTHONPATH=/usr/local/bin/python3
#if [[ ! $(tmux list-sessions) ]]; then
#  tmux
#fi

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

# Allow CTRL+D to exit zsh with partial command line (non empty line)
exit_zsh() { exit }
zle -N exit_zsh
bindkey '^D' exit_zsh

# 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

export PATH="$HOME/.local/bin:$PATH"
export VIRTUAL_ENV_DISABLE_PROMPT=true
#unsetopt BEEP
# Enable various options
setopt interactive_comments beep extendedglob nomatch notify completeinword prompt_subst

##########    Prompt(s)    ##########

# Enable colors and change prompt:
autoload -U colors && colors	# Load colors
#autoload -U promptinit && promptinit
#prompt fade red
# 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]

function insert-mode () { echo "-- INSERT --" }
function normal-mode () { echo "-- NORMAL --" }


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
}
#PROMPT='%9c$(git_branch_test_color)%F{none} %# '

#echo "${gitstatuscolor} (${ref})"

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 '
    #'%{-[%F{226}'$'\uE0A0''%f%{%F{76}%b%f%}]%} %F{76}%c%F{3}%u%f'
    #'%F{226}'$'\uE0A0''%f%{(%F{76}%b%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
}
#hook_com[unstaged]+=' %F{15}(%f%F{196}!%f%F{15})untracked%f'


#RPROMPT='%F{5}[%F{2}%n%F{5}] %F{3}%3~ ${vcs_info_msg_0_} %f%# '
#add-zsh-hook
function my_precmd () {
    vcs_info
    PS1="%{┌─[%F{145}%n%f] %F{39}%0~%f%} ${vcs_info_msg_0_}
    %{%{$terminfo_down_sc$(insert-mode)$terminfo[rc]%}%{└─%{["%{$(tput setaf 226)%}""%{$(tput blink)%}"%{$%}"%{$(tput sgr0)%}"%{%G]%}%}%}%}"
}

function set-prompt () {
    case ${KEYMAP} in
      (vicmd)      VI_MODE="$(normal-mode)" ;;
      (main|viins) VI_MODE="$(insert-mode)" ;;
      (*)          VI_MODE="$(insert-mode)" ;;
    esac
    PS1="%{┌─[%F{145}%n%f] %F{39}%0~%f%} ${vcs_info_msg_0_}
    %{%{$terminfo_down_sc$VI_MODE$terminfo[rc]%}%{└─%{["%{$(tput setaf 226)%}""%{$(tput blink)%}"%{$%}"%{$(tput sgr0)%}"%{%G]%}%}%}%}"
}
add-zsh-hook precmd my_precmd
RPROMPT='%(?..[%F{196}%?%f] )'
#RPROMPT="%K{172}${vcs_info_msg_0_}%k%(?..[%F{196}%?%f] )"

#autoload -Uz vcs_info
#zstyle ':vcs_info:*' stagedstr 'M' 
#zstyle ':vcs_info:*' unstagedstr 'M' 
#zstyle ':vcs_info:*' check-for-changes true
#zstyle ':vcs_info:*' actionformats '%{(%F{76}%b%F{3}|%F{1}%a%%f%}) '
#zstyle ':vcs_info:*' formats \
#    '%{(%F{76}%b%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{1}??%f'
#fi
#}
#
#
##RPROMPT='%F{5}[%F{2}%n%F{5}] %F{3}%3~ ${vcs_info_msg_0_} %f%# '
#precmd () {
#    print -rP "
#"
#    #PS1="┌─[%F{48}%n%f]-[%F{154}%B%m%b%f]-[%F{202}%#%f%F{39}%0~%f]
#    PS1="%{┌─[%F{48}%n%f] %F{119}%#%f%F{119}%0~%f%}] ${vcs_info_msg_0_}
#    %{%{$terminfo_down_sc$(insert-mode)$terminfo[rc]%}%{└─%{["%{$(tput setaf 226)%}""%{$(tput blink)%}"%{$%}"%{$(tput sgr0)%}"%{%G]%}%}%}%}"
#} && { vcs_info } 
#
#function set-prompt () {
#    case ${KEYMAP} in
#      (vicmd)      VI_MODE="$(normal-mode)" ;;
#      (main|viins) VI_MODE="$(insert-mode)" ;;
#      (*)          VI_MODE="$(insert-mode)" ;;
#    esac
#    PS1="%{┌─[%F{48}%n%f] %F{119}%#%f%F{119}%0~%f%} ${vcs_info_msg_0_}
#    %{%{$terminfo_down_sc$VI_MODE$terminfo[rc]%}%{└─%{["%{$(tput setaf 226)%}""%{$(tput blink)%}"%{$%}"%{$(tput sgr0)%}"%{%G]%}%}%}%}"
#}
#PS1="%{┌─[%F{48}%n%f]-[%F{48}%B%m%b%f]-[%F{48}%#%f%F{48}%0~%f]%}
function zle-line-init zle-keymap-select {
    set-prompt
    zle reset-prompt
    case $KEYMAP in
        vicmd) echo -ne '\e[1 q';;      # block
        viins|main) echo -ne '\e[5 q';; # beam
    esac
}

preexec () { print -rn -- $terminfo[el]; echo -ne '\e[5 q' ; }

zle -N zle-line-init
zle -N zle-keymap-select

# Load version control information
#autoload -Uz vcs_info
#precmd() { vcs_info }

# Format the vcs_info_msg_0_ variable
#zstyle ':vcs_info:git:*' formats 'on branch %b'

# Set up the right-side prompt (with git branch name) and throw conditional error Code


## function to return current branch name while suppressing errors.
#function git_branch() {
#    branch=$(git symbolic-ref HEAD 2> /dev/null | awk 'BEGIN{FS="/"} {print $NF}')
#    if [[ $branch == "" ]]; then
#        :
#    else
#        echo ' (' $branch ') '
#    fi
#}

#setopt prompt_subst             # allow command substitution inside the prompt
#PROMPT='%~ $(git_branch) >'     # set the prompt value

#autoload -Uz add-zsh-hook vcs_info
#setopt prompt_subst
#add-zsh-hook precmd my_precmd
#
#zstyle ':vcs_info:git:*' formats '%b'
#
#function my_precmd {
#  local theUser='%B%F{39}%n%f%b'
#  local theHost='%B%F{white}@%m%f%b'
#  local git1="%F{220}~%f$(git_prompt_info)"
#  local rcAndArrow='%(?.%F{white}.%B%F{red}[%?])»%f%b'
#
#  vcs_info
#  local git2color='cyan'
#  [[ "${vcs_info_msg_0_}" == "master" ]] && git2color='196'
#  local git2="||%F{${git2color}}${vcs_info_msg_0_}%f||"
#
#  psvar[1]="${theUser}${theHost} ${git1} ${rcAndArrow} "
#  psvar[2]="${git2}"
#}
#
#PROMPT='${psvar[1]}'
#RPROMPT='${psvar[2]}'



##########    Auto-completion    ##########

#autoload -U promptinit && promptinit
autoload -Uz compinit && compinit

# 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



##########    Vi mode    ##########

export KEYTIMEOUT=25
export EDITOR=$VISUAL
export VISUAL=nvim
bindkey -M viins '^?' backward-delete-char
bindkey -M viins '^[[3~'  delete-char
bindkey -M vicmd '^[[3~'  delete-char
bindkey -r '\e/'
bindkey -M viins 'jj' vi-cmd-mode
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"
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

##########    Useful Commands/Alias    ##########

# 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
}

# 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
# Setup fzf
# ---------
if [[ ! "$PATH" == */root/.local/share/nvim/plugged/fzf/bin* ]]; then
  export PATH="${PATH:+${PATH}:}/root/.local/share/nvim/plugged/fzf/bin"
fi

# Spawn a clone of current terminal 
putstate () {
    declare +x >~/environment.tmp
    declare -x >>~/environment.tmp
    echo cd "$PWD" >>~/environment.tmp
}

getstate () {
    . ~/environment.tmp
}

# 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'}$;;')/";
}

# 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'

# List upto last 10 visited directories using "d" and quickly cd into any specific one
# using just a number from "0" to "9"
alias d="dirs -v | head -10" 
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"

# 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
}

# Use lf to switch directories and bind it to ctrl-o
#lfcd () {
#	tmp="$(mktemp)"
#	lf -last-dir-path="$tmp" "$@"
#	if [ -f "$tmp" ]; then
#		dir="$(cat "$tmp")"
#		rm -f "$tmp" >/dev/null
#		[ -d "$dir" ] && [ "$dir" != "$(pwd)" ] && cd "$dir"
#	fi
#}
#bindkey -s '^o' 'lfcd\n'
#bindkey -s '^a' 'bc -lq\n'
#bindkey -s '^f' 'cd "$(dirname "$(fzf)")"\n'

alias config='/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
#alias cfg='config subtree pull --prefx'
#alias gsp="git subtree push --prefix=_site git@github.com:mertnuhoglu/blog_datascience.git"
#alias gsp="git subtree push.local/bin/scripts https://github.com/srdusr/scripts.git main --squash
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
}
alias vi='nvim'
alias nv='nvim'
alias trash="gio trash"
alias trash_restore='gio trash --restore "$(gio trash --list | fzf | cut -f 1)"'
alias ec='$EDITOR $HOME/.config/zsh/.zshrc'
alias sc="source $HOME/.config/zsh/.zshrc"

# Print previous command into a file
getlast () {
    fc -nl $((HISTCMD - 1))
}

alias pp='getlast 2>&1 |&tee -a output.txt'

# Print output of a command NOTE: Must be used in conjunction but no need for "|" symbol
alias -g cap='2>&1 | tee -a output.txt'

# confirmation #
alias mv='mv -i'
alias cp='cp -i'
alias ln='ln -i'
alias rm='rm -i'

# suspend(sleep)/hibernate and lock screen if using systemctl
alias suspend='systemctl suspend && betterlockscreen --lock dimblur'
alias hibernate='systemctl hibernate'

##########    Source Plugins, should be last    ##########

# load zsh-vi-mode
#source /usr/share/zsh/plugins/zsh-vi-mode/zsh-vi-mode.plugin.zsh

# Load zsh-syntax-highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh 2>/dev/null

# Load fzf keybindings and completion
source /usr/share/fzf/key-bindings.zsh
source /usr/share/fzf/completion.zsh
source /usr/share/fzf-marks/fzf-marks.plugin.zsh 2>/dev/null

# Suggest aliases for commands
source /usr/share/zsh/plugins/zsh-you-should-use/you-should-use.plugin.zsh 2>/dev/null

# Load fish like auto suggestions
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.plugin.zsh
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh