aboutsummaryrefslogtreecommitdiffstats
path: root/nichijou/home/config/zsh/zshrc
blob: 194f3d6f9becc4249dc8030063e879a46dbaf9f5 (plain) (blame)
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
# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# Allow for substitutions inside prompt, necessary for e.g. git prompts.
setopt PROMPT_SUBST

# Enable zsh's generic vcs info.
autoload -Uz vcs_info
precmd () { vcs_info }
zstyle ':vcs_info:*' formats ' (%F{cyan}%b%f)'
PS1='%B%F{green}%n@%m%f%b:%B%F{blue}%~%f$vcs_info_msg_0_%b%(!.#.$) '

setopt histignorealldups
setopt sharehistory
# Experimental options.
setopt extended_history
setopt hist_ignore_all_dups
setopt hist_ignore_space
setopt hist_reduce_blanks
# Writing the directory name only will cd to it.
setopt auto_cd
# Do not move cursor to the end of line on completion.
setopt complete_in_word
# Allow comments in interactive shell.
setopt interactive_comments
# Disable beep.
unsetopt beep
# Do not autocorrect spelling for arguments.
unsetopt correct
unsetopt correct_all

# Remove duplicate paths.
typeset -U PATH

# Keep 10000 lines of history within the shell and save it to ~/.zsh_history.
HISTSIZE=10000
SAVEHIST=10000
HISTFILE=~/.zsh_history

# Use modern completion system.
autoload -Uz compinit
compinit
# Also complete bash-specific completions.
autoload -U bashcompinit
bashcompinit

zstyle ':completion:*' auto-description 'specify: %d'
zstyle ':completion:*' completer _expand _complete _correct _approximate
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' menu select=2
eval "$(dircolors -b)"
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' list-prompt \
	%SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' \
	'r:|[._-]=* r:|=* l:|=*'
zstyle ':completion:*' menu select=long
zstyle ':completion:*' select-prompt \
	%SScrolling active: current selection at %p%s
zstyle ':completion:*' use-compctl false
zstyle ':completion:*' verbose true

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'

# Source useful defaults.
source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source /usr/share/doc/fzf/examples/key-bindings.zsh
source /usr/share/doc/fzf/examples/completion.zsh
source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh

# Editor and input char assignment.
[[ ${TERM} != 'dumb' ]] && () {

	# Use human-friendly identifiers.
	zmodload -F zsh/terminfo +b:echoti +p:terminfo
	typeset -gA key_info
	key_info=(
	'Control'      '\C-'
	'ControlLeft'  '\e[1;5D \e[5D \e\e[D \eOd \eOD'
	'ControlRight' '\e[1;5C \e[5C \e\e[C \eOc \eOC'
	'Escape'       '\e'
	'Meta'         '\M-'
	'Backspace'    "${terminfo[kbs]}"
	'BackTab'      "${terminfo[kcbt]}"
	'Left'         "${terminfo[kcub1]}"
	'Down'         "${terminfo[kcud1]}"
	'Right'        "${terminfo[kcuf1]}"
	'Up'           "${terminfo[kcuu1]}"
	'Delete'       "${terminfo[kdch1]}"
	'End'          "${terminfo[kend]}"
	'F1'           "${terminfo[kf1]}"
	'F2'           "${terminfo[kf2]}"
	'F3'           "${terminfo[kf3]}"
	'F4'           "${terminfo[kf4]}"
	'F5'           "${terminfo[kf5]}"
	'F6'           "${terminfo[kf6]}"
	'F7'           "${terminfo[kf7]}"
	'F8'           "${terminfo[kf8]}"
	'F9'           "${terminfo[kf9]}"
	'F10'          "${terminfo[kf10]}"
	'F11'          "${terminfo[kf11]}"
	'F12'          "${terminfo[kf12]}"
	'Home'         "${terminfo[khome]}"
	'Insert'       "${terminfo[kich1]}"
	'PageDown'     "${terminfo[knp]}"
	'PageUp'       "${terminfo[kpp]}"
	)

	# Bind the keys.
	local key
	for key (${(s: :)key_info[ControlLeft]}) bindkey ${key} backward-word
	for key (${(s: :)key_info[ControlRight]}) bindkey ${key} forward-word

	if [[ -n ${key_info[Home]} ]]; then
		bindkey ${key_info[Home]} beginning-of-line
	fi
	if [[ -n ${key_info[End]} ]] bindkey ${key_info[End]} end-of-line

	if [[ -n ${key_info[PageUp]} ]]; then
	  bindkey ${key_info[PageUp]} up-line-or-history
	fi
	if [[ -n ${key_info[PageDown]} ]]; then
		bindkey ${key_info[PageDown]} down-line-or-history
	fi

	if [[ -n ${key_info[Insert]} ]]; then
		bindkey ${key_info[Insert]} overwrite-mode
	fi

	if [[ -n ${key_info[Backspace]} ]]; then
		bindkey ${key_info[Backspace]} backward-delete-char
	fi
	if [[ -n ${key_info[Delete]} ]] bindkey ${key_info[Delete]} delete-char

	if [[ -n ${key_info[Left]} ]] bindkey ${key_info[Left]} backward-char
	if [[ -n ${key_info[Right]} ]] bindkey ${key_info[Right]} forward-char

	if [[ -n ${key_info[Up]} ]] bindkey ${key_info[Up]} history-search-backward
	if [[ -n ${key_info[Down]} ]] bindkey ${key_info[Down]} history-search-forward

	# Expandpace.
	bindkey ' ' magic-space

	# Clear.
	bindkey "${key_info[Control]}L" clear-screen

	# Bind <Shift-Tab> to go to the previous menu item.
	if [[ -n ${key_info[BackTab]} ]]; then
		bindkey ${key_info[BackTab]} reverse-menu-complete
	fi

	# Use smart URL pasting and escaping.
	autoload -Uz bracketed-paste-url-magic \
		&& zle -N bracketed-paste bracketed-paste-url-magic
	autoload -Uz url-quote-magic && zle -N self-insert url-quote-magic

	if zstyle -t ':zim:input' double-dot-expand; then
		double-dot-expand() {
			if [[ ${LBUFFER} == *.. ]]; then
				LBUFFER+='/..'
			else
				LBUFFER+='.'
			fi
		}
		zle -N double-dot-expand
		bindkey '.' double-dot-expand
		bindkey -M isearch '.' self-insert
	fi

	autoload -Uz is-at-least && if ! is-at-least 5.3; then
		# Redisplay after completing, and avoid blank prompt after
		# <Tab><Tab><Ctrl-C>
		expand-or-complete-with-redisplay() {
		print -Pn '...'
		zle expand-or-complete
		zle redisplay
		}
		zle -N expand-or-complete-with-redisplay
		bindkey "${key_info[Control]}I" \
			expand-or-complete-with-redisplay
	fi

	# Put into application mode and validate ${terminfo}
	zle-line-init() {
		if (( ${+terminfo[smkx]} )) echoti smkx
	}
	zle-line-finish() {
		if (( ${+terminfo[rmkx]} )) echoti rmkx
	}
	zle -N zle-line-init
	zle -N zle-line-finish
}

# Color support
alias ls='ls --group-directories-first --color=auto'
alias grep='grep --color=auto'

# Grep in a directory
alias grepd='grep -rIin'

# List only directories and symbolic links that point to directories.
alias lsd='ls -ld *(-/DN)'

# List all files.
alias l='ls -lhA --time-style=long-iso'
alias ll='ls -lh --time-style=long-iso'

# Colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;'\
'32:locus=01:quote=01'

# Recursively finds all occurrence that satisfies the given expression
f()
{
	find . -iname "*${*}*"
}

# Do not force window, useful for terminal use.
# TODO: Can it be turned into a config file instead?
alias mpv='mpv --force-window=no'

# Use clipboard selection instead of default primary.
alias xclip='xclip -selection clipboard'

# Ask before overwriting. Add a character to prove you are sure of your actions.
alias mvf=mv
alias mv='mv -i'
alias rmf=rm
alias rm='rm -i'

# I'd rather use nvim for everything.
alias vim=nvim
alias vimdiff='nvim -d'

# Recursively rsync the given path.
alias rsyncc='rsync --info=progress2 -avz'

# Download youtube playlist in a sorted manner
# TODO: update
alias youtube-dl-ordered='youtube-dl -i -o
"%(playlist_index)s-%(title)s.%(ext)s"'

# Open the todo.txt file
# This and track can be updated to use xdg documents path if others find them
# useful as well.
alias todo='$EDITOR ~/doc/txt/todo.txt'

# Load keyboard kernel object for colors and brightness control.
# TODO: Use the deb instead or dkms.
alias keyboard='cd ~/dev/github/tuxedo-keyboard/src; sudo insmod ./*.ko; cd'

# Switch to games-specific user fast.
alias vidya='xhost +si:localuser:vidya && sudo -u vidya -i'

# Switch 4k monitor resolution to 720p before switching to vidya then switching
# back to 4k after gaming is done.
alias steam='xrandr --output DP-1 --mode 1280x720 --rate 60; vidya;
xrandr --output DP-1 --mode 3840x2160 --rate 60'

# Prefer cantata-based radio to this
# Plays an online stream using mpv and records it.
# $1 Stream link.
# $2 File path.
radio()
{
	curl -l "$1" | tee "$2" | mpv -
}

# Silly tracking.
track()
{
	echo "$(date)> $1" >>~/doc/txt/log.txt
}

# Records microphone locally.
recordmic()
{
	ffmpeg -f pulse -i alsa_input.pci-0000_00_1f.3.analog-stereo -f pulse \
	-i alsa_output.pci-0000_00_1f.3.analog-stereo.monitor -map 0:0 \
	-map 1:0 "$HOME/tv/record/$1.ogg"
}

# Records audio output.
record()
{
	ffmpeg -f pulse \
	-i alsa_output.pci-0000_00_1f.3.analog-stereo.monitor -map 0:0 \
	"$HOME/tv/record/$1.ogg"
}

# TODO: load dz keyboard layout properly using setxkbmap
# Maybe should be somewhere else e.g., .profile? or somewhere system-wide

export PATH="$HOME/.poetry/bin:$PATH"