This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
412 lines (354 loc) · 13.2 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
#
# zsh dotfile
# ~/.zshrc
# Name: cpw
#
# General Settings {{{
# -----------------------------------------------------------------------------
setopt auto_name_dirs
setopt auto_pushd
setopt pushd_ignore_dups
setopt pushdminus
setopt multios
setopt cdablevarS
setopt autocd
setopt extendedglob
setopt interactivecomments
setopt nobeep
setopt nocheckjobs
setopt correct
###############################################################################
# History
###############################################################################
HISTSIZE=5000
SAVEHIST=${HISTSIZE}
HISTFILE=~/.zshinfo
setopt histignoredups
setopt share_history
setopt append_history
setopt extended_history
setopt hist_expire_dups_first
setopt hist_ignore_space
setopt hist_verify
setopt inc_append_history
###############################################################################
# Cache
###############################################################################
CACHEDIR="$HOME/.cache/zsh-cache"
fasd_cache="$HOME/.cache/.fasd-init-cache"
if [ "$(command -v fasd)" -nt "$fasd_cache" -o ! -s "$fasd_cache" ]; then
fasd --init auto >| "$fasd_cache"
fi
source "$fasd_cache"
unset fasd_cache
# }}}
# Completion {{{
# -----------------------------------------------------------------------------
# Use (advanced) completion functionality.
autoload -U compinit
compinit -d $CACHEDIR/zcompdump 2>/dev/null
# Use cache to speed completion up and set cache folder path.
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path $CACHEDIR
# Auto-insert first suggestion.
setopt menu_complete
# If the <tab> key is pressed with multiple possible options, print the
# options. If the options are printed, begin cycling through them.
zstyle ':completion:*' menu select
# Set format for warnings.
zstyle ':completion:*:warnings' format 'Sorry, no matches for: %d%b'
# Use colors when outputting file names for completion options.
zstyle ':completion:*' list-colors ''
# Do not prompt to cd into current directory.
# For example, cd ../<tab> should not prompt current directory.
zstyle ':completion:*:cd:*' ignore-parents parent pwd
# Completion for kill.
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,cputime,cmd'
# Show completion for hidden files also.
zstyle ':completion:*' file-patterns '*(D)'
# Red dots!
expand-or-complete-with-dots() {
echo -n "\e[31m......\e[0m"
zle expand-or-complete
zle redisplay
}
zle -N expand-or-complete-with-dots
bindkey "^I" expand-or-complete-with-dots
# }}}
# Prompt Style {{{
# -----------------------------------------------------------------------------
# This changes PS1 dynamically depending on insert or command mode.
#
# Generates PS1 given background colour arguments
# $1 = user bgcolour
# $2 = directory bgcolour
generate_ps1() {
setopt PROMPT_SUBST
PS1="%{[38;05;230;48;05;${1}m%} %(!.%S-ROOT-%s.%n) %{[38;05;${1};48;05;${2}m%}⮀%{[00m%}%{[38;05;230;48;05;${2}m%} %1~ %{[00m%}%{[38;05;${2}m%}⮀ %{[00m%}"
}
autoload -U colors && colors
generate_ps1 26 196
zle-keymap-select () {
if [[ $TERM == "rxvt-unicode" || $TERM == "rxvt-unicode-256color" ]]; then
if [ $KEYMAP = vicmd ]; then
generate_ps1 40 196
() { return $__prompt_status }
zle reset-prompt
else
generate_ps1 26 196
() { return $__prompt_status }
zle reset-prompt
fi
fi
}
zle -N zle-keymap-select
zle-line-init () {
zle -K viins
if [[ $TERM == "rxvt-unicode" || $TERM = "rxvt-unicode-256color" ]]; then
generate_ps1 26 196
() { return $__prompt_status }
zle reset-prompt
fi
}
zle -N zle-line-init
# }}}
# zle widgets {{{
# -----------------------------------------------------------------------------
# The ZLE widges are all followed by "zle -<MODE> <NAME>" and bound below in the "Key Bindings" section.
# Delete all characters between a pair of characters. Mimics Vim's "di" text
# object functionality.
delete-in() {
# Create locally-scoped variables we'll need
local CHAR LCHAR RCHAR LSEARCH RSEARCH COUNT
# Read the character to indicate which text object we're deleting.
read -k CHAR
if [ "$CHAR" = "w" ]
then # diw, delete the word.
# find the beginning of the word under the cursor
zle vi-backward-word
# set the left side of the delete region at this point
LSEARCH=$CURSOR
# find the end of the word under the cursor
zle vi-forward-word
# set the right side of the delete region at this point
RSEARCH=$CURSOR
# Set the BUFFER to everything except the word we are removing.
RBUFFER="$BUFFER[$RSEARCH+1,${#BUFFER}]"
LBUFFER="$LBUFFER[1,$LSEARCH]"
return
# diw was unique. For everything else, we just have to define the
# characters to the left and right of the cursor to be removed
elif [ "$CHAR" = "(" ] || [ "$CHAR" = ")" ] || [ "$CHAR" = "b" ]
then # di), delete inside of a pair of parenthesis
LCHAR="("
RCHAR=")"
elif [ "$CHAR" = "[" ] || [ "$CHAR" = "]" ]
then # di], delete inside of a pair of square brackets
LCHAR="["
RCHAR="]"
elif [ $CHAR = "{" ] || [ $CHAR = "}" ] || [ "$CHAR" = "B" ]
then # di], delete inside of a pair of braces
LCHAR="{"
RCHAR="}"
else
# The character entered does not have a special definition.
# Simply find the first instance to the left and right of the
# cursor.
LCHAR="$CHAR"
RCHAR="$CHAR"
fi
# Find the first instance of LCHAR to the left of the cursor and the
# first instance of RCHAR to the right of the cursor, and remove
# everything in between.
# Begin the search for the left-sided character directly the left of the cursor.
LSEARCH=${#LBUFFER}
# Keep going left until we find the character or hit the beginning of the buffer.
while [ "$LSEARCH" -gt 0 ] && [ "$LBUFFER[$LSEARCH]" != "$LCHAR" ]
do
LSEARCH=$(expr $LSEARCH - 1)
done
# If we hit the beginning of the command line without finding the character, abort.
if [ "$LBUFFER[$LSEARCH]" != "$LCHAR" ]
then
return
fi
# start the search directly to the right of the cursor
RSEARCH=0
# Keep going right until we find the character or hit the end of the buffer.
while [ "$RSEARCH" -lt $(expr ${#RBUFFER} + 1 ) ] && [ "$RBUFFER[$RSEARCH]" != "$RCHAR" ]
do
RSEARCH=$(expr $RSEARCH + 1)
done
# If we hit the end of the command line without finding the character, abort.
if [ "$RBUFFER[$RSEARCH]" != "$RCHAR" ]
then
return
fi
# Set the BUFFER to everything except the text we are removing.
RBUFFER="$RBUFFER[$RSEARCH,${#RBUFFER}]"
LBUFFER="$LBUFFER[1,$LSEARCH]"
}
zle -N delete-in
# Delete all characters between a pair of characters and then go to insert mode.
# Mimics Vim's "ci" text object functionality.
change-in() {
zle delete-in
zle vi-insert
}
zle -N change-in
# Delete all characters between a pair of characters as well as the surrounding
# characters themselves. Mimics Vim's "da" text object functionality.
delete-around() {
zle delete-in
zle vi-backward-char
zle vi-delete-char
zle vi-delete-char
}
zle -N delete-around
# Delete all characters between a pair of characters as well as the surrounding
# characters themselves and then go into insert mode Mimics Vim's "ca" text
# object functionality.
change-around() {
zle delete-in
zle vi-backward-char
zle vi-delete-char
zle vi-delete-char
zle vi-insert
}
zle -N change-around
# Zsh's vi-up/down-line-or-history does what I want but leaves the cursor at the
# beginning rather than front. Perplexing!
vim-up-line-or-history() {
zle vi-up-line-or-history
zle vi-end-of-line
}
zle -N vim-up-line-or-history
vim-down-line-or-history() {
zle vi-down-line-or-history
zle vi-end-of-line
}
zle -N vim-down-line-or-history
# The hackneyed <Nop> bind.
nop() {}
zle -N nop
# Use clipboard rather than system registers.
prepend-x-selection () {
RBUFFER=$(xsel -op </dev/null)$RBUFFER;
zle vi-end-of-line
}
zle -N prepend-x-selection
append-x-selection () {
zle vi-forward-char
RBUFFER=$(xsel -op </dev/null)$RBUFFER;
zle vi-end-of-line
}
zle -N append-x-selection
yank-x-selection () {
print -rn -- $CUTBUFFER | xsel -ip;
}
zle -N yank-x-selection
autoload edit-command-line
zle -N edit-command-line
# }}}
# The Vim setup {{{
# -----------------------------------------------------------------------------
bindkey -v
# Disable flow control. Specifically, ensure that ctrl-s does not stop
# terminal flow so that it can be used in other programs (such as Vim).
setopt noflowcontrol
stty -ixon
# Disable use of ^D.
stty eof undef
# 1 sec <Esc> time delay? zsh pls.
# Set to 10ms for key sequences. (Note "bindkey -rp '^['" removes the
# availability of any '^[...' mappings, so use this instead.)
KEYTIMEOUT=1
###############################################################################
# Insert mode
###############################################################################
bindkey -M viins "^?" backward-delete-char # i_Backspace
bindkey -M viins '^[[3~' delete-char # i_Delete
bindkey -M viins '^[[Z' reverse-menu-complete # i_SHIFT-Tab
# Non-Vim default mappings I use everywhere.
bindkey -M viins "^N" vim-down-line-or-history # i_CTRL-N
bindkey -M viins "^E" vim-up-line-or-history # i_CTRL-E
bindkey -M viins '^V' append-x-selection # i_CTRL-V
# Vim defaults
bindkey -M viins "^A" beginning-of-line # i_CTRL-A
bindkey -M viins "^E" end-of-line # i_CTRL-E
bindkey -M viins "^K" down-line-or-history # i_CTRL-N
bindkey -M viins "^P" up-line-or-history # i_CTRL-P
bindkey -M viins "^H" backward-delete-char # i_CTRL-H
bindkey -M viins "^B" _history-complete-newer # i_CTRL-B
bindkey -M viins "^F" _history-complete-older # i_CTRL-F
bindkey -M viins "^U" backward-kill-line # i_CTRL-U
bindkey -M viins "^W" backward-kill-word # i_CTRL-W
bindkey -M viins "^[[7~" vi-beginning-of-line # i_Home
bindkey -M viins "^[[8~" vi-end-of-line # i_End
# Edit current line in veritable Vim.
bindkey -M viins "^H" edit-command-line # i_CTRL-I
###############################################################################
# Normal mode
###############################################################################
bindkey -M vicmd "ca" change-around # ca
bindkey -M vicmd "ci" change-in # ci
bindkey -M vicmd "cc" vi-change-whole-line # cc
bindkey -M vicmd "da" delete-around # da
bindkey -M vicmd "di" delete-in # di
bindkey -M vicmd "dd" kill-whole-line # dd
bindkey -M vicmd "gg" beginning-of-buffer-or-history # gg
bindkey -M vicmd "G" end-of-buffer-or-history # G
bindkey -M vicmd "^R" redo # CTRL-R
# Non-Vim default mappings I use everywhere.
bindkey -M vicmd 'p' append-x-selection # p
bindkey -M vicmd 'P' prepend-x-selection # P
bindkey -M vicmd 'y' yank-x-selection # y
#bindkey -M vicmd 'Y' yank-to-end-x-selection # Y
bindkey -M vicmd "z" vi-substitute # z
# Vim defaults I don't use but I may as well keep.
bindkey -M vicmd "^H" vi-add-eol # CTRL-E
bindkey -M vicmd "g~" vi-oper-swap-case # g~
bindkey -M vicmd "ga" what-cursor-position # ga
###############################################################################
# Alias
###############################################################################
alias docker_interactive='docker run -it --rm'
alias ls='ls -a --color'
alias lsl='ls -lah --color'
alias grep='grep --color'
# Disable autocorrection for these.
alias ln="nocorrect ln"
alias mv='nocorrect mv'
alias mkdir='nocorrect mkdir'
alias sudo='nocorrect sudo'
# Default flags.
alias cp="nocorrect cp -Rv" # Ensure that cp is always recursive and verbose.
alias df="df -h" # Display sizes in human readable format.
alias du="du -h -c" # Display sizes in human readable format, and total.
alias mount="sudo mount" # Don't require prepending sudo.
alias umount="sudo umount" # Don't require prepending sudo.
# Screen stuff
alias screen_reattach="screen -rd"
# Power Management Controls.
alias poweroff="sudo poweroff" # Don't require prepending sudo.
alias reboot="sudo reboot" # Don't require prepending sudo.
alias suspend="systemctl suspend" # Don't require prepending sudo. Also the best low power suspension state.
# Miscellaneous custom commands.
alias fonts='mkfontdir ~/.fonts;mkfontscale ~/.fonts;xset +fp ~/.fonts;xset fp rehash;fc-cache;fc-cache -fv'
alias sv="sudo vim"
# Restart configs.
alias so="xrdb ~/.Xresources; exec zsh"
# Directory navigation.
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias -- -='cd -'
alias -- --='cd -2'
alias -- ---='cd -3'
alias scrot="scrot -c -d 3 ~/cpw/media/pictures/screencaps/scrot/%Y-%m-%d-%T.png"
alias byzanz-record="cd ~/cpw/media/pictures/screencaps/byzanz && byzanz-record -c -d 10 cpw.gif && cd -"
# Bookmarks
alias godev='cd /home/cpw/cpw/dev'
alias goweb='cd /home/cpw/cpw/web'
alias gomedia='cd /home/cpw/cpw/media'