-
Notifications
You must be signed in to change notification settings - Fork 2
/
dot_zshrc
344 lines (284 loc) · 7.86 KB
/
dot_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
## -*- shell-script -*-
export PATH="$HOME/bin:$HOME/.local/bin:$HOME/.emacs.d/bin:/usr/local/bin:$PATH"
# Check if a command exists, can be a binary in PATH or a shell
# alias/function.
function has() {
type $1 &>/dev/null
}
###############################################################################
## OPTIONS
###############################################################################
# Keybindings
typeset -A key
key=(
Home "${terminfo[khome]}"
End "${terminfo[kend]}"
Insert "${terminfo[kich1]}"
Delete "${terminfo[kdch1]}"
Up "${terminfo[kcuu1]}"
Down "${terminfo[kcud1]}"
Left "${terminfo[kcub1]}"
Right "${terminfo[kcuf1]}"
PageUp "${terminfo[kpp]}"
PageDown "${terminfo[knp]}"
)
bindkey -e
bindkey ${key[Home]} beginning-of-line
bindkey "^A" beginning-of-line
bindkey ${key[End]} end-of-line
bindkey "^E" end-of-line
bindkey ${key[Insert]} overwrite-mode
bindkey ${key[Delete]} delete-char
bindkey ${key[Left]} backward-char
bindkey ${key[Right]} forward-char
# History
if has atuin; then
eval "$(atuin init zsh)"
fi
# Tab completion
zle -C complete-file complete-word _generic
zstyle ':completion:*' completer _complete _match _approximate
zstyle ':completion:*:match:*' original only
zstyle ':completion:*:approximate:*' max-errors 1 numeric
zstyle ':completion:*:*' original only
zstyle ':completion:complete-file::::' completer _files
zstyle ':completion:*' completer _complete _ignored _files
autoload -Uz compinit
compinit
setopt complete_aliases
setopt hash_list_all
setopt menu_complete
# Directory navigation
setopt auto_cd
setopt auto_pushd
setopt chase_links
setopt pushd_silent
setopt pushd_to_home
# Globbing
setopt extended_glob
setopt glob_dots
setopt mark_dirs
setopt multibyte
setopt null_glob
# Misc
setopt correct
###############################################################################
## COLOURS
###############################################################################
# Framebuffer zenburn colours
if [ "$TERM" = "linux" ]; then
echo -en "\e]P01C1C20"
echo -en "\e]P84d4d4d"
echo -en "\e]P1CE5C00"
echo -en "\e]P9F57900"
echo -en "\e]P2B7CE42"
echo -en "\e]PABDE077"
echo -en "\e]P3B88B10"
echo -en "\e]PBFFC135"
echo -en "\e]P466AABB"
echo -en "\e]PCAACCBB"
echo -en "\e]P5B7416E"
echo -en "\e]PDBB4466"
echo -en "\e]P65E7175"
echo -en "\e]PEA3BABF"
echo -en "\e]P7D6D8D9"
echo -en "\e]PF6C887A"
clear
fi
# Terminal colours
autoload colors zsh/terminfo
colors
colnames=(black red green yellow blue magenta cyan white)
for color in $colnames; do
eval f$color='%{$fg[${color}]%}'
eval fb$color='%{$terminfo[bold]$fg[${color}]%}'
eval b$color='%{$bg[${color}]%}'
done
foregrounds=($fblack $fred $fgreen $fyellow $fblue $fmagenta $fcyan $fwhite
$fbblack $fbred $fbgreen $fbyellow $fbblue $fbmagenta $fbcyan $fbwhite)
backgrounds=($bblack $bred $bgreen $byellow $bblue $bmagenta $bcyan $bwhite)
fdefault="%{$terminfo[sgr0]%}"
# Colourful directory listings
if has dircolors; then
eval $(dircolors -b)
fi
###############################################################################
## PROMPT
###############################################################################
function _prompt_precmd()
{
# Last command status
local cmdstatus=$?
local sadface=$([ "$cmdstatus" != "0" ] && echo " ${fred}:(${fdefault} ")
# Colours
local dircolour=$([ -w "$(pwd)" ] && echo $fbcyan || echo $fbred)
# Host
local prompt_host="${fbyellow}%m${fdefault}"
# Tag
local prompt_tag=$([ ! -z $PROMPT_TAG ] && echo " ${fbmagenta}{${PROMPT_TAG}}${fdefault}")
# Git
local prompt_git=""
if git status &>/dev/null; then
local ref=$(git symbolic-ref HEAD 2> /dev/null) || return 1
local branch=${ref#refs/heads/}
prompt_git=" ${fgreen}[${branch}]${fdefault}"
fi
export PROMPT="${prompt_host}${prompt_tag}${prompt_git}${sadface} ${fbblue}>>>${fdefault} "
export RPROMPT="${dircolour}%2~${fdefault}"
}
# Push to the prompt tag
function ptag_push(){
if [[ -z $PROMPT_TAG ]]; then
PROMPT_TAG=$1
else
PROMPT_TAG=$PROMPT_TAG/$1
fi
}
# Pop from the prompt tag.
function ptag_pop(){
PROMPT_TAG=$(dirname $PROMPT_TAG)
if [[ "$PROMPT_TAG" == "." ]]; then
PROMPT_TAG=""
fi
}
export PS2="${fblue}%B%_%b >${fdefault} "
export PS3="${fyellow}%Bselect%b:${fdefault} "
precmd_functions+=(_prompt_precmd)
###############################################################################
## FUNCTIONS & ALIASES
###############################################################################
# Display information about the state of the world.
function status(){
clear
# Display a fortune
if has fortune; then
fortune | sed "s:^:> :"
fi
# This month's budget
echo "\n$(date +%B) Budget:"
if $IS_GNULINUX; then
local nextm=$(date --date=+1month +"%Y-%m-01")
else
local nextm=$(date -v+1m +"%Y-%m-01")
fi
hledger balance -N -H --end=$nextm --tree discretionary goal saved | sed 1d | sed 's:^ : :'
echo
}
# Run and disown a program
function r()
{
$* &>/dev/null &
disown %%
}
# Run and disown a program, then terminate the shell
function re()
{
$* &>/dev/null &
disown %%
exit
}
if has fzf; then
function __fzf_any() {
local dir="${1:-.}"
find $dir | fzf "$2"
}
function __fzf_file() {
local dir="${1:-.}"
find $dir -type f | fzf "$2"
}
function __fzf_dir() {
local dir="${1:-.}"
find $dir -type d | fzf
}
alias -g ,a='$(__fzf_any . -m)'
alias -g ,f='$(__fzf_file . -m)'
alias -g ,d='$(__fzf_dir . -m)'
alias -g ,=,f
alias -g '.F'="| fzf"
fi
if has nix; then
# Launch a subshell with some packages available
function with() {
if [[ -z $WITH ]]; then
NEW_WITH=$@
else
NEW_WITH=$WITH/$@
fi
ptag_push "with"
PROMPT_TAG=$PROMPT_TAG WITH=$NEW_WITH nix shell "$@" -c zsh
ptag_pop
}
alias nixos-up='sudo nixos-rebuild switch --recreate-lock-file'
alias nixos-gc='sudo nix-collect-garbage -d && sudo nix-store --optimise'
fi
if has stack; then
# Short alias for stack, with abbreviated subcommands.
function s() {
local command=$1
typeset -A subcommands
local subcommands=(b build c clean e exec eg "exec ghci" g ghci i install r runghc t test)
if [[ $command == "-h" ]] || [[ $command == "--help" ]]; then
echo "Usage: s <command or alias> <args>"
echo "Shorthand for 'stack'\n"
echo "Aliases:"
for k in "${(@ko)subcommands}"; do
echo " ${(r:2:: :)${k}} -> $subcommands[$k]"
done
echo "\nFor stack help, see 'stack --help' (or just 's')"
else
shift 2>/dev/null
stack ${=subcommands[$command]:-$command} $*
fi
}
fi
# Short aliases
alias !='sudo'
alias c='clear'
alias e='exit'
alias o=$EDITOR
alias g='git'
alias m='mosh'
alias '?'='status'
alias G=grep
alias d=dwim
# Pipeline
alias -g '.G'='| grep'
alias -g '.S'='| sed'
alias -g '.A'='| awk'
alias -g '.U'='| sort | uniq'
alias -g '.L'='2>&1 | less'
alias -g '.H'='2>&1 | head'
alias -g '.T'='2>&1 | tail'
alias -g '.1'='2>&1'
# Basic commands
alias su='sudo su - '
alias free='free -ht'
alias df='df -h'
alias du='du -shc'
# File management
case $KERNEL_NAME in
"linux")
alias ls='ls --color=auto'
alias la='ls --color=auto -lA'
;;
"darwin")
alias ls='ls -G'
alias la='ls -GlA'
;;
esac
alias lls='env ls'
alias cp='cp -rpv'
alias mv='mv -v'
alias mkdir='mkdir -p'
# Miscellanious
alias bal="hledger balance -M --tree"
alias din='docker run -it --rm'
alias ta='tmux attach-session -d -t'
alias reload='exec zsh'
alias pgfl='pgrep -fl'
###############################################################################
## MISCELLANEOUS
###############################################################################
if has rbenv; then
eval "$(rbenv init - zsh)"
fi