-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzshrc.local
483 lines (398 loc) · 13.8 KB
/
zshrc.local
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
#### Determine if we're running on old Unix
source ~/.bashrc.detect_old_unix
#### Bash completions from homebrew
# TODO: This is nice, but currently this is overriding some zsh completions with worse bash ones
#if [[ ! -z "$HOMEBREW_PREFIX" ]]; then
# # Enable bash completions
# autoload -U +X bashcompinit && bashcompinit
#
# #for f in $HOMEBREW_PREFIX/etc/bash_completion.d/* ; do
# # There's bash-isms in these completions, so ignore errors if those occur
# # source $f 2>/dev/null
# #done
#fi
#### Paths
function python_paths() {
local PYTHON_VER
# Add any locally installed python binaries to the path, using the currently running default python
if (( $+commands[python] )); then
PYTHON_VER=python
fi
# If we have explicit python 3, add that in front to prefer it
if (( $+commands[python3] )); then
PYTHON_VER=python3
fi
local PYTHON_BIN=$(${PYTHON_VER} -m site --user-base)/bin
if [[ -d $PYTHON_BIN ]]; then
PATH="$PYTHON_BIN:$PATH"
fi
}
# Very unlikely to need this immediately, so lazy load it
if (( ! ${+OLD_UNIX} )); then
zsh-defer -a python_paths
fi
#### Prompt Setup
# Update the path and re-init
FPATH="$HOME/.zsh/themes:$FPATH"
promptinit
# If we're in iTerm2, but outside tmux and ssh, we know we have powerline fonts avail
if [[ ($LC_TERMINAL == 'iTerm2' || $TERM_PROGRAM == 'WezTerm' || $TERM_PROGRAM == 'ghostty') && -z $TMUX && -z $SSH_TTY ]]; then
RICH_PROMPT_SUPPORTED=1
else
RICH_PROMPT_SUPPORTED=0
fi
# Export so vim can use this too
export RICH_PROMPT_SUPPORTED
RICH_PROMPT_ENABLED=$RICH_PROMPT_SUPPORTED
prompt eharmon $RICH_PROMPT_ENABLED
# This function makes it possible to easily toggle off the powerline fonts for easy copying
function toggleprompt() {
if (( $RICH_PROMPT_ENABLED )); then
RICH_PROMPT_ENABLED=0
else
RICH_PROMPT_ENABLED=1
fi
prompt eharmon $RICH_PROMPT_ENABLED
}
alias tp=toggleprompt
# And if we forget to toggle, this will clean the clipboard of special characters
if (( $+commands[pbpaste] )); then
function pbclean() {
pbpaste | sed "s/[]//g" | pbcopy
}
fi
# AppleScript and adjacent
if (( $+commands[osascript] )); then
# Find the currently selected path in Finder
alias fpath="osascript -e 'tell application \"Finder\"
if selection is not {}
POSIX path of (selection as alias)
end if
end tell'" # | sed 's/[[:space:]]/\\\ /g'"
# Mount a volume via Finder, in the background. Use `open` instead to mount and select in Finder.
function fmount() {
# TODO: Should print the mounted path instead of the weird AppleScript path
osascript << EOF
tell application "Finder"
mount volume "$1"
end tell
EOF
}
# Show a file or folder in Finder
alias fshow="open -R"
# Show a file or folder contents in Finder
function fopen() {
if [[ -d "$1" ]]; then
open $1
else
open -r $1
fi
}
# bind opt-f to grab the current Finder path onto the line editor
function _finderpath() {
zle -U "\"$(fpath)\""
}
zle -N _finderpath
bindkey '\ef' _finderpath
fi
if (( $+commands[xxd] )); then
# TODO: Need to print a warning if we saw non-hex chars, this just does a simple test and outputs nothing
alias hex2bin='grep -v "[hjklmnopqrstuvwxyz]" | xxd -r -p - -'
fi
function motd() {
if [[ -f "/etc/motd" ]]; then
cat /etc/motd
elif [[ -f "/run/motd.dynamic" ]]; then
cat /run/motd.dynamic
fi
}
#### Vars
# Don't check for mail often
MAILCHECK=3600
# Tell our zshrc to pass -p to the vim options to open multiple files in tabs
VIM_OPTIONS='-p'
if (( $+commands[nvim] )); then
# Use neovim as the man page tool with a custom color scheme -- same as default right now, but overridden in case this changes later
export MANPAGER='nvim +"colo molokai" +"highlight Normal guibg=NONE" +"set titlestring=man:\ %t" +Man!'
fi
#### Includes
# Include hostname-specific magic if it exists
local DOMAIN=${HOST#*.}
if [[ $DOMAIN != "local" && -e ~/.zshrc.$DOMAIN ]]; then
echo "Setting shell for $DOMAIN domain"
source ~/.zshrc.$DOMAIN
fi
# Some bits to support extra features with multiplexors (tmux), if available
if [[ ! -z "$TMUX" ]]; then
source ~/.zsh/zshrc.local.tmux
fi
#### Functions
# Title command which properly supports screen, overriding grml version
function title_precmd() {
# update VCS information
(( ${+functions[vcs_info]} )) && vcs_info
if [[ $TERM == screen* ]] ; then
if [[ -n ${vcs_info_msg_1_} ]] ; then
ESC_print ${vcs_info_msg_1_}
else
ESC_print "zsh"
fi
fi
# adjust title of xterm
# see http://www.faqs.org/docs/Linux-mini/Xterm-Title.html
case $TERM in
(xterm*|rxvt*|alacritty|foot|screen*)
local prefix
if [[ -n $SUDO_USER || -n $SSH_TTY ]]; then
prefix="[%n@%m] "
fi
set_title "${(%):-"$prefix"zsh: %(5~|%-1~/…/%3~|%4~)}"
;;
esac
}
# Title command which properly support screen, overriding grml version
function title_preexec() {
# get the name of the program currently running
# set screen window title if running in a screen
if [[ "$TERM" == screen* ]] ; then
local CMD=${1[(wr)^(*=*|sudo|ssh|-*)]}
ESC_print ${CMD}
fi
# adjust title of xterm
case $TERM in
(xterm*|rxvt*|alacritty|foot|screen*)
local prefix
if [[ -n $SUDO_USER || -n $SSH_TTY ]]; then
prefix="[%n@%m] "
fi
set_title "${(%):-"$prefix"}$1"
;;
esac
}
# Install precmd and preexec functions on the stack
typeset -ga precmd_functions
typeset -ga preexec_functions
precmd_functions+='title_precmd'
preexec_functions+='title_preexec'
# zsh has native support directory substitution, but this gives us even more power
function scd() {
if [[ -n "$1" ]]; then
cd $(echo $PWD | sed "$1")
else
echo "Give me a substitution!"
fi
}
# Mount a disk image and try to CD into the most logical partition
if (( $+commands[hdiutil] )); then
function hdicd() {
local output=$(hdiutil mount $1)
print $output
cd $(echo $output | tail -n 1 | awk '{print $3}')
}
fi
# Copy a file from remote directly into the paste buffer
if (( $+commands[pbcopy] )); then
function rpbcopy() {
if [[ -n "$1" && -n "$2" ]]; then
ssh $1 "cat $2" | pbcopy
else
echo "Must specify: pbcopy [user@]host filename"
fi
}
fi
#### Aliases
# Quick alias to make an HTTP server wherever we are
if (( $+commands[python3] )); then
alias tmphttp="python3 -m http.server 8000"
fi
# Give us 'wget' on machines which only have curl
if (( ! $+commands[wget] && $+commands[curl] )); then
alias wget="curl -OLRJw \"\n'%{filename_effective}' saved [%{size_download}]\n\""
fi
# Make it easier to type weechat
if (( $+commands[weechat-curses] )); then
alias weechat="weechat-curses"
fi
# dos2unix if it isn't on the system
if (( ! $+commands[dos2unix] )); then
alias dos2unix="perl -pi -e 's/\r\n|\n|\r/\n/g'"
fi
# And unix2dos
if (( ! $+commands[unix2dos] )); then
alias unix2dos="perl -pi -e 's/\r\n|\n|\r/\r\n/g'"
fi
# Cat plists, binary or otherwise
if (( $+commands[plutil] )); then
alias plcat="plutil -convert xml1 -o -"
fi
# Faster search with Spotlight
if (( $+commands[mdfind] )); then
alias search=mdfind -onlyin
fi
# Pretty-print JSON on macOS
if (( $+commands[plutil] )); then
alias ppj="plutil -convert json -r - -o -"
fi
# Use neovim
if (( $+commands[nvim] )); then
function vim() {
VIM_PLEASE_SET_TITLE='yes' command nvim ${VIM_OPTIONS} "$@"
}
fi
# Quicklook for files
if (( $+commands[qlmanage] )); then
function ql() {
echo -n "Launching QuickLook: "
qlmanage -p $@ &>/dev/null & disown
}
fi
# Disable the annoying grml `ag` alias, since we're used to silver searcher
if (( $+aliases[ag] )); then
unalias ag
fi
# Whisky's CLI
# TODO: Find the application automatically
if [[ -e "/Applications/Whisky.app/Contents/Resources/WhiskyCmd" ]];
then
alias whisky="/Applications/Whisky.app/Contents/Resources/WhiskyCmd"
fi
# If we have sevenzip package installed on macOS, use it over p7zip
if [[ $OSTYPE == darwin* && $+commands[7zz] == 1 ]]; then
alias 7z=7zz
alias 7za=7zz
alias 7zr=7zz
fi
# Insecure ssh connections to allow us to work with old machines when we want
insecure_options="-oKexAlgorithms=+diffie-hellman-group-exchange-sha1 -oPubkeyAcceptedKeyTypes=+ssh-rsa -oHostKeyAlgorithms=+ssh-rsa"
alias insecuressh="ssh $insecure_options"
alias insecurescp="scp $insecure_options"
alias insecuresftp="sftp $insecure_options"
unset insecure_options
#### OS Specific
# Case-insensitve matching on macOS as it usually has a case-insensitive filesystem
if [[ $OSTYPE == darwin* ]]; then
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
fi
# Define some useful IRIX commands
if [[ $OSTYPE == irix* ]]; then
alias halt="sudo /etc/shutdown -y -g0 -p"
alias reboot="sudo /etc/shutdown -y -g0 -i6"
fi
#### Misc
# Use fancy history locking
is-at-least 4.3.7 && setopt HIST_FCNTL_LOCK
# Set the lang, which we have to do now for some reason
export LANG=en_US.UTF-8
# Blue on black is impossible to see, change ls highlighting colors
# grml zshrc already does this for GNU userland, but not BSD
export LSCOLORS="gxfxcxdxbxegedabagacad"
# Setup a hotkey to quickly apply a regex to the current buffer line
autoload -U read-from-minibuffer
function regex-edit() {
# REPLY will be filled by read-from-minibuffer
local REPLY temp
read-from-minibuffer "Regex edit: "
if [ -n $REPLY ]; then
temp="$(echo $BUFFER | sed ${REPLY})" 2>/dev/null
if [ $? = 0 ]; then
BUFFER=$temp
fi
fi
}
zle -N regex-edit
bindkey "^[r" regex-edit
# If you're inside tmux, grep for a line in the current window and add it to the buffer
# This isn't entirely useful as is, but there's probably something nice we can do with this
function read-pane() {
local REPLY temp
read-from-minibuffer "Regex search: "
[[ ! -z $TMUX ]] && temp=$(tmux capture-pane -p | grep $REPLY | head -n 1)
BUFFER="$BUFFER$temp"
}
zle -N read-pane
bindkey "^[t" read-pane
#### Backdir
if (( ! ${+OLD_UNIX} )); then
zsh-defer source ~/.zsh/modules/zsh-bd/bd.zsh
fi
#### Syntax highlighting
# Define this as a function since we need to modify the settings after it's sourced
function load_syntax_highlighting() {
# Setup plugins to load
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)
source ~/.zsh/modules/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# Main
#ZSH_HIGHLIGHT_STYLES[default]:=none
ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=124'
#ZSH_HIGHLIGHT_STYLES[reserved-word]:=fg=yellow
ZSH_HIGHLIGHT_STYLES[alias]='fg=25'
ZSH_HIGHLIGHT_STYLES[builtin]='fg=25'
ZSH_HIGHLIGHT_STYLES[function]='fg=25'
ZSH_HIGHLIGHT_STYLES[command]='fg=25'
#ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline
#ZSH_HIGHLIGHT_STYLES[commandseparator]:=none
ZSH_HIGHLIGHT_STYLES[hashed-command]='fg=25'
#ZSH_HIGHLIGHT_STYLES[path]:=underline
ZSH_HIGHLIGHT_STYLES[globbing]='fg=28'
ZSH_HIGHLIGHT_STYLES[history-expansion]='fg=25'
#ZSH_HIGHLIGHT_STYLES[single-hyphen-option]:=none
#ZSH_HIGHLIGHT_STYLES[double-hyphen-option]:=none
#ZSH_HIGHLIGHT_STYLES[back-quoted-argument]:=none
ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=250'
ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=250'
#ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]:=fg=cyan
#ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]:=fg=cyan
#ZSH_HIGHLIGHT_STYLES[assign]:=none
# Brackets
ZSH_HIGHLIGHT_STYLES[bracket-error]='fg=160,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-1]='fg=28,bold'
ZSH_HIGHLIGHT_STYLES[bracket-level-2]='fg=cyan,bold'
#ZSH_HIGHLIGHT_STYLES[bracket-level-3]:=fg=magenta,bold}
#ZSH_HIGHLIGHT_STYLES[bracket-level-4]:=fg=yellow,bold}
ZSH_HIGHLIGHT_STYLES[bracket-level-5]='fg=25,bold'
#ZSH_HIGHLIGHT_STYLES[cursor-matchingbracket]:=standout}
}
if (( ! ${+OLD_UNIX} )); then
zsh-defer -a +pr load_syntax_highlighting
fi
#### Autosuggestions
if (( ! ${+OLD_UNIX} )); then
ZSH_AUTOSUGGEST_MANUAL_REBIND=1
zsh-defer source ~/.zsh/modules/zsh-autosuggestions/zsh-autosuggestions.zsh
fi
#### fzf for search
# Source fzf if it's installed
function setup_fzf() {
if [[ -e $HOMEBREW_PREFIX/opt/fzf ]]; then
source $HOMEBREW_PREFIX/opt/fzf/shell/completion.zsh
source $HOMEBREW_PREFIX/opt/fzf/shell/key-bindings.zsh
# Reset the ^r action to GRML style:
bindkey -M emacs '^r' history-incremental-pattern-search-backward
bindkey -M vicmd '^r' history-incremental-pattern-search-backward
bindkey -M viins '^r' history-incremental-pattern-search-backward
# And add the fzf style
bindkey '^r^f' fzf-history-widget
fi
}
zsh-defer setup_fzf
#### Google Cloud SDK
if [[ -d ~/google-cloud-sdk ]]; then
source ~/google-cloud-sdk/path.zsh.inc
source ~/google-cloud-sdk/completion.zsh.inc
fi
#### iTerm2 Integration
# Load iTerm2 integration directly from iTerm2.app when it's installed in the default location
# TODO: This means shell integration on remote Linux hosts won't work, but also doesn't require
# we keep it up-to-date in dotfiles as a tradeoff.
# TODO: Find the application automatically
local ITERM_SHELL_INTEGRATION_LOCATION="/Applications/iTerm.app/Contents/Resources/iterm2_shell_integration.zsh"
if [[ $LC_TERMINAL == 'iTerm2' && -e $ITERM_SHELL_INTEGRATION_LOCATION ]]; then
source $ITERM_SHELL_INTEGRATION_LOCATION
fi
#### Completion audit
# We skipped this weak security audit earlier, so kick it off deferred
zsh-defer +1 +2 compaudit
# Print a profile if it was enabled earlier
if [[ -n "$ZSH_PROFILE" ]]; then
zprof
fi