-
Notifications
You must be signed in to change notification settings - Fork 43
/
.zshrc
372 lines (301 loc) · 9.22 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
#!/bin/zsh
# Start profling:
# zmodload zsh/zprof
local -a precmd_functions
# {{{ setting options
autoload edit-command-line
autoload -U compinit
setopt \
append_history \
auto_cd \
auto_pushd \
chase_links \
complete_aliases \
extended_glob \
extended_history \
hist_ignore_all_dups \
hist_ignore_dups \
hist_ignore_space \
hist_reduce_blanks \
hist_save_no_dups \
hist_verify \
ignore_eof \
list_types \
mark_dirs \
noclobber \
noflowcontrol \
path_dirs \
prompt_percent \
prompt_subst \
rm_star_wait \
share_history
# }}}
# {{{ environment settings
umask 027
extra_path=(
$HOME/bin \
$HOME/.nodenv/bin \
$HOME/.nodenv/shims/ \
$HOME/.cabal/bin \
$HOME/.ghcup/bin \
$HOME/.local/bin \
/sbin \
/usr/sbin \
/usr/local/bin \
/usr/local/sbin \
)
export PATH="${(j|:|)extra_path}:$PATH"
export MANPATH="$HOME/share/man:${MANPATH}"
CDPATH=$CDPATH::$HOME:/usr/local
HISTFILE=$HOME/.zsh_history
HISTFILESIZE=65536 # search this with `grep | sort -u`
HISTSIZE=4096
SAVEHIST=4096
# Output time stats for progs that run for longer than a minute.
REPORTTIME=60
# Report any login/logout of other users.
WATCH=notme
WATCHFMT='%n %a %l from %m at %T.'
export LANG=en_US.UTF-8
export LC_ALL=$LANG
export LC_COLLATE=C
export EDITOR='vim'
export VISUAL=$EDITOR
export LESS='-imJMWR'
export PAGER="less $LESS"
export MANPAGER=$PAGER
export BROWSER='firefox'
export WINEDEBUG=-all
export WINEARCH=win32
export PYTHONSTARTUP=$HOME/.pythonrc.py
# Inline nodenv init to shave ~10ms more off the startup time.
# eval "$(nodenv init -)"
export NODENV_SHELL=zsh
command nodenv rehash 2>/dev/null
nodenv() {
local command
command="${1:-}"
if [ "$#" -gt 0 ]; then
shift
fi
case "$command" in
rehash|shell)
eval "$(nodenv "sh-$command" "$@")";;
*)
command nodenv "$command" "$@";;
esac
}
source "$HOME/.nodenv/libexec/../completions/nodenv.zsh"
# }}}
# {{{ completions
compinit -C
zstyle ':completion:*' list-colors "$LS_COLORS"
zstyle -e ':completion:*:(ssh|scp|sshfs|ping|telnet|nc|rsync):*' hosts '
reply=( ${=${${(M)${(f)"$(<~/.ssh/config)"}:#Host*}#Host }:#*\**} )'
# Custom script in $HOME/bin/c
compdef c=curl
# }}}
# {{{ prompt and theme
# Set vi-mode and create a few additional Vim-like mappings
bindkey -v
bindkey "^?" backward-delete-char
bindkey -M vicmd "^R" redo
bindkey -M vicmd "u" undo
bindkey -M vicmd "ga" what-cursor-position
bindkey -M viins '^p' history-beginning-search-backward
bindkey -M vicmd '^p' history-beginning-search-backward
bindkey -M viins '^n' history-beginning-search-forward
bindkey -M vicmd '^n' history-beginning-search-forward
# Allows editing the command line with an external editor
zle -N edit-command-line
bindkey -M vicmd "v" edit-command-line
# Restore bash/emacs defaults.
bindkey '^U' backward-kill-line
bindkey '^Y' yank
# Set up prompt
if [[ ! -n "$ZSHRUN" ]]; then
source $HOME/.zsh_shouse_prompt
# Fish shell like syntax highlighting for Zsh:
# git clone git://github.com/nicoulaj/zsh-syntax-highlighting.git \
# $HOME/.zsh-syntax-highlighting/
if [[ -d $HOME/.zsh-syntax-highlighting/ ]]; then
source $HOME/.zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS+=( brackets pattern )
fi
fi
# }}}
# {{{ aliases
# ls:
alias ls='ls -F --color'
alias la='ls -A'; compdef la=ls
alias ll='ls -lh'; compdef ll=ls
# Vim:
alias vi=$EDITOR
# fast Vim that doesn't load a vimrc or plugins
alias vv="${EDITOR} -N -u NONE"
# Loads vimrc but no plugins
alias vvv="${EDITOR} -N --noplugin"
alias vimprof="${EDITOR} \
--cmd 'profile start vim-profile.log' \
--cmd 'profile func *' \
--cmd 'profile file *'"
compdef vi=vim
compdef vv=vim
compdef vvv=vim
compdef vimprof=vim
# Aliases that override default names:
alias less='less -imJMW'
alias tree="tree -FC --charset=ascii"
alias info='info --vi-keys'
alias wtf='wtf -o'
alias nnn='nnn -S'
alias ocaml='rlwrap ocaml'
alias mplayer='mplayer -af scaletempo -speed 1'
alias R='R --no-save'
# Aliases around scripts in $HOME/bin:
alias tea-timer="countdown 120 && notify-send 'Tea!' 'Tea is done.'"
alias fetchall-gh='fetchall "git@github.com"'
alias fetchall-gl='fetchall "git@gitlab.com"'
# Aliases that make new things:
alias ducks='du -cks * | sort -rn | head -15'
alias incognito=' export HISTFILE=/dev/null'
alias osx_openports='lsof -iTCP -sTCP:LISTEN -P'
alias clip='xclip -selection clipboard'
alias rs='rsync -avhzC --progress'
compdef rs=rsync
# Print all files under the current path without prefixed path.
# Useful for listing files under one path based on the files in another. E.g.:
# cd /path/to/dotfiles; filesunder | xargs -0 -I@ ls -l $HOME/@
alias filesunder='find . \( -name .git -type d \) -prune -o -type f -printf "%P\0"'
alias filesmissing='find . -maxdepth 2 -xtype l'
# Quickly ssh through a bastian host without having to hard-code in ~/.ssh/config
alias pssh='ssh -o "ProxyCommand ssh $PSSH_HOST nc -w1 %h %p"'
# mkdir and cd at once
mkcd() { mkdir -p -- "$1" && cd -- "$1" }
compdef mkcd=mkdir
# Useful for working with Git remotes; e.g., ``git log IN``, ``git diff OUT``.
alias -g IN='..@{u}'
alias -g IIN='...@{u}'
alias -g OUT='@{u}..'
alias -g OOUT='@{u}...'
alias -g UP='@{u}'
# Selects a random file: ``mplayer RANDOM``
alias -g RANDOM='"$(shuf -e -n1 *)"'
# Output stderr in red. Usage: somecomand RED
alias -g RED='2> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)'
# Drop-in for quick notifications. E.g., sleep 10; lmk
alias lmk='notify-send "Task in $(basename $(pwd)) is done"\
"Task in $(basename $(pwd)) is done"'
# Output names if terminal can handle 256 colors.
alias 256test='echo -e "\e[38;5;196mred\e[38;5;46mgreen\e[38;5;21mblue\e[0m"'
# }}}
# Miscellaneous Functions:
# zshrun A lightweight, one-off application launcher {{{1
# by Mikael Magnusson (I think)
#
# Invokes a command and then immediately closes the terminal window.
# To run a command without closing the terminal use ctrl-j instead of Enter.
#
# Usage:
# sh -c 'ZSHRUN=1 uxterm -geometry 100x4+0+0 +ls'
if [[ -n "$ZSHRUN" ]]; then
unsetopt ignore_eof
unset ZSHRUN
function _accept_and_quit() {
zsh -c "${BUFFER}" &|
exit
}
zle -N _accept_and_quit
bindkey "^J" accept-line
bindkey "^M" _accept_and_quit
PROMPT="zshrun %~> "
RPROMPT=""
fi
# }}}
# Use a fuzzy-finder for common CLI tasks {{{1
alias ..='cd ..'
# cd to a parent directory.
function ...() {
explode_path | tail -n +2 | pick | read -d -r new_dir
cd "$new_dir"
}
# Search and replay a command from the shell history.
# (Will output the command but not execute.)
function _fzy_history() {
tac $HOME/.zsh_history | fzy -p 'History > ' \
| awk '{ print substr($0, index($0, ";") + 1) }'
}
# A completion fallback if something more specific isn't available.
function _fzy_generic_find() {
ffind "$PWD" 2>/dev/null | fzy -p 'Files > ' \
| xargs printf '%s %s\n' "$*"
}
# Invoke a fuzzy-finder to complete history, file paths, or command arguments
# Press ctrl-f to start completion.
# (This idea is stolen from fzf.)
#
# Usage:
# <[empty cli]> - complete from shell history.
# <cmd> - complete from _fzy_<cmd> script or funciton output.
# <cmd> - falls back to generic file path completion.
#
# New completions can be added for a <cmd> by adding a shell function or
# a shell script on PATH with the pattern _fzy_<cmd>. The script will be
# invoked with the command name and any arguments as ARGV and should print the
# full resulting command and any additions to stdout.
pick-completion() {
setopt localoptions localtraps noshwordsplit noksh_arrays noposixbuiltins
local tokens=(${(z)LBUFFER})
local cmd=${tokens[1]}
local cmd_fzy_match
if [[ ${#tokens} -lt 1 ]]; then
cmd_fzy_match=( '_fzy_history' )
else
# Filter (:#) the arrays of the names ((k)) Zsh function and scripts on
# PATH and remove ((M)) entries that don't match "_fzy_<cmdname>":
cmd_fzy_match=${(M)${(k)functions}:#_fzy_${cmd}}
if [[ ${#cmd_fzy_match} -eq 0 ]]; then
cmd_fzy_match=${(M)${(k)commands}:#_fzy_${cmd}}
if [[ ${#cmd_fzy_match} -eq 0 ]]; then
cmd_fzy_match=( '_fzy_generic_find' )
fi
fi
fi
zle -M "Gathering suggestions..."
zle -R
local result=$($cmd_fzy_match "${tokens[@]}")
if [ -n "$result" ]; then
LBUFFER="$result"
fi
zle reset-prompt
}
zle -N pick-completion
bindkey '^F' pick-completion
# }}}
# Manually refresh the tmux status infos {{{1
# Needed to immediate update the Git status display.
function refresh_tmux() {
tmux refresh -S
}
_last_cmd_was_git=0
function last_command_was_git() {
[[ "$1" == git* ]] && _last_cmd_was_git=1
}
function refresh_tmux_on_git() {
if [[ "$_last_cmd_was_git" -eq 1 ]]; then
_last_cmd_was_git=0
tmux refresh -S
fi
}
# }}}
# Run precmd functions
preexec_functions=( last_command_was_git )
chpwd_functions=( refresh_tmux )
precmd_functions=( precmd_prompt refresh_tmux_on_git )
# Allow OS or environment specific overrides:
if [[ -r "$HOME/.zsh_customize" ]]; then
source "$HOME/.zsh_customize"
fi
# End profiling:
# zprof
# EOF