summaryrefslogtreecommitdiff
path: root/.config/zsh/.zshrc
blob: 8b2fd1a7627cf13184bb99fd093adb655e553954 (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
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.config/zsh/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# Created by newuser for 5.8

# Enable colors:
autoload -U colors && colors

# History in cache directory:
HISTSIZE=10000
SAVEHIST=10000
HISTFILE=~/.cache/zsh/history
HISTCONTROL=ereasedups

# Basic auto/tab complete:
autoload -U compinit promptinit
zstyle ':completion:*' menu select
zmodload zsh/complist
compinit
_comp_options+=(globdots)		# Include hidden files.
#promptinit; prompt gentoo


# vi mode
bindkey -v
export KEYTIMEOUT=1

# Use vim keys in tab complete menu:
bindkey -M menuselect 'h' vi-backward-char
bindkey -M menuselect 'k' vi-up-line-or-history
bindkey -M menuselect 'l' vi-forward-char
bindkey -M menuselect 'j' vi-down-line-or-history
bindkey -v '^?' backward-delete-char

bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
bindkey "^[[3~" delete-char
bindkey "^[3;5~" delete-char


# Change cursor shape for different vi modes.
function zle-keymap-select {
  if [[ ${KEYMAP} == vicmd ]] ||
     [[ $1 = 'block' ]]; then
    echo -ne '\e[1 q'
  elif [[ ${KEYMAP} == main ]] ||
       [[ ${KEYMAP} == viins ]] ||
       [[ ${KEYMAP} = '' ]] ||
       [[ $1 = 'beam' ]]; then
    echo -ne '\e[5 q'
  fi
}
zle -N zle-keymap-select
zle-line-init() {
    zle -K viins # initiate `vi insert` as keymap (can be removed if `bindkey -V` has been set elsewhere)
    echo -ne "\e[5 q"
}
zle -N zle-line-init
echo -ne '\e[5 q' # Use beam shape cursor on startup.
preexec() { echo -ne '\e[5 q' ;} # Use beam shape cursor for each new prompt.

# Use ranger to switch directories and bind it to ctrl-o
ranger_cd() {
	temp_file="$(mktemp -t "ranger_cd.XXXXXXXXXX")"
	ranger --choosedir="$temp_file" -- "${@:-$PWD}"
	if chosen_dir="$(cat -- "$temp_file")" && [ -n "$chosen_dir" ] && [ "$chosen_dir" != "$PWD" ]; then
		cd -- "$chosen_dir"
	fi
	rm -f -- "$temp_file"
}
bindkey -s '^o' 'ranger_cd\n'
ranger_pushd() {
	temp_file="$(mktemp -t "ranger_cd.XXXXXXXXXX")"
	ranger --choosedir="$temp_file" -- "${@:-$PWD}"
	if chosen_dir="$(cat -- "$temp_file")" && [ -n "$chosen_dir" ] && [ "$chosen_dir" != "$PWD" ]; then
		pushd -- "$chosen_dir"
	fi
	rm -f -- "$temp_file"
}
bindkey -s '^O' 'ranger_pushd\n'
bindkey -s '^r' 'ranger\n'
bindkey -s '^n' 'setsid kitty -d $(pwd)\n'

bindkey -s '^z' 'bg && disown\n'

mkcd() {
	mkdir $1
	cd $1
}

retry() {
	local SLPT=5
	[ "$1" = "-t" ] && SLPT="$2" && shift 2
	while true; do
		$@ && break
		sleep "$SLPT"
	done
}

alias test_internet='retry ping google.com -c 1'

#alias ls='exa --group-directories-first -l'
#alias la='exa --group-directories-first -la'
alias ls='ls --group-directories-first --color=auto'
alias ll='ls -la --group-directories-first --color=auto'
alias la='ls -a --group-directories-first --color=auto'

alias less='less -R'

alias ci3='$EDITOR $HOME/.config/i3/config'
alias cpb='$EDITOR $HOME/.config/polybar/config.ini'
alias cpbm='$EDITOR $HOME/.config/polybar/modules.ini'
alias czsh='$EDITOR $HOME/.config/zsh/.zshrc'
alias cnv='$EDITOR $HOME/.config/nvim/init.vim'

alias r='ranger'

alias s='swallow'

alias dotfiles='/usr/bin/git --git-dir=${XDG_DATA_HOME:-${HOME}/.local/share}/dotfiles --work-tree=$HOME'

alias luamake=/home/nerdjp/dev/lua-language-server/3rd/luamake/luamake
alias clip='xclip -selection "clipboard"'

alias e='edit.sh'
alias c='config.sh'

pls() {
	sudo !!
}

# Edit line in vim with ctrl-e:
autoload edit-command-line; zle -N edit-command-line
bindkey '^e' edit-command-line

setopt auto_cd

alias cp='cp -i'
alias mv='mv -i'

alias dcp='pwd | xclip -selection clipboard'

alias ffmpeg='ffmpeg -hide_banner'
alias ffprobe='ffprobe -hide_banner'
alias iv='nsxiv'
alias mp='ncmpcpp'
alias vi='nvim'
alias grep='grep --color=auto'

source /usr/share/fzf/key-bindings.zsh
#source /usr/share/fzf/completion.zsh

alias gs='git status'
alias gb='git branch'
alias gc='git checkout'
alias gcb='git checkout -b'
alias gf='git pull'
alias gp='git pull'

smv() {
	[[ -n $1 ]] && mv $(\ls | fzf -m) $1
}

gbc() {
	git branch $@ && git checkout $@
}

fzfg() {
	JOB=$(jobs | fzf | cut -d ' ' -f 1 | sed -e 's/\[/%/' -e 's/\]//')
	[ -n "$JOB" ] && fg "$JOB"
}

evi() {
	if [ -z "${1}" ]; then
		EBUILD_PATH="/var/db/repos/"
	else
		EBUILD_PATH="$(equery w ${1})"
	fi
	[ -n "${EBUILD_PATH}" ] && vi ${EBUILD_PATH}
}

detach() {
	JOBS=$(jobs)
	if [ -n "$JOBS" ]; then
		[ "$(wc -l <<< ${JOBS})" -le 1 ] && { bg && sleep 0.1 && disown && return }
		JOB=$(fzf <<< ${JOBS} | cut -d ' ' -f 1 | sed -e 's/\[/%/' -e 's/\]//')
		echo $JOB
		bg "$JOB" && disown "$JOB"
	fi
}

ZSH_AUTOSUGGEST_STRATEGY=(history completion)

# make kernel
# sync
# update
# myip
# open ports

#eval "$(starship init zsh)"
autoload -Uz vcs_info
zstyle ':vcs_info:*' formats ' %b'

preexec_timer() { SECONDS=0 }
preexec_functions+=( preexec_timer )

precmd_jobs() {
	local IFS="
	"
	for job in $(jobs); do
		if [[ "$job" =~ "running" ]]; then
			printf "\033[38;5;2m %s" "${job##running}"
		elif [[ "$job" =~ "suspended" ]]; then
			printf "\033[38;5;3m %s" "${job##suspended}"
		else
			printf "%s" "${job}"
		fi
	done
}

preexec_rename() {
	printf '\033]2;%s@%s: %s\033\\' ${USER} ${HOST} ${1}
}

precmd_vcs_info() { vcs_info }
precmd_functions+=( precmd_vcs_info precmd_jobs precmd_rename )

preexec_functions+=( preexec_rename )

setopt prompt_subst
PROMPT='%F{magenta}%n@%m%f %F{blue}%1~%f %(?,[,%F{red}[%f) '

#PROMPT='%F{magenta}%f %F{blue}%1~%f %(?,[,%F{red}[%f) '
#
RPROMPT=' %(?,],%F{red}]%f)'

#%F{cyan}${vcs_info_msg_0_}%f'
#%1(S,took $(format-time $SECONDS),)'
#%F{magenta}%D{%I:%m %p}%f'

#paleofetch

# Load zsh-syntax-highlighting; should be last.
source /usr/share/zsh/site-functions/zsh-syntax-highlighting.zsh
source /usr/share/zsh/site-functions/zsh-autosuggestions.zsh

printf '\033]2;%s@%s\033\\' $USER $HOST
cat "${HOME}/doc/ascii-arts/woman.txt"

attach_tmux_group() {
	if [[ -z ${TMUX} ]]; then
		local group="${1:-common}"
		local session=$(tmux list-sessions -F "#S:#{session_group}:#{session_attached}" \
			| grep ":${group}:0" | cut -d ':' -f 1)

		if [[ -n ${session} ]]; then
			exec tmux attach -t "${session}"
		else
			tmux new-window -at "${group}:"
			tmux new -t "${group}"
		fi
	fi
}

#attach_tmux_group