-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc-extra
285 lines (240 loc) · 10.6 KB
/
.zshrc-extra
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
##########################################################################
# Zsh sensible configuration #
# (https://github.com/hanjianwei/zsh-sensible/blob/master/sensible.zsh). #
##########################################################################
# It's likely I'll be setting EDITOR=nvim somewhere in my zsh, which will
# cause zsh to switch to vi-mode if I re-exec (exec zsh). Prevent this.
bindkey -e
# Keep a very long history.
HISTFILE="$HOME/.zsh_history"
HISTSIZE=500000
SAVEHIST=500000
setopt BANG_HIST # Treat the '!' character specially during expansion.
setopt EXTENDED_HISTORY # Write the history file in the ':start:elapsed;command' format.
setopt INC_APPEND_HISTORY # Write to the history file immediately, not when the shell exits.
setopt SHARE_HISTORY # Share history between all sessions.
setopt HIST_EXPIRE_DUPS_FIRST # Expire a duplicate event first when trimming history.
setopt HIST_IGNORE_DUPS # Do not record an event that was just recorded again.
setopt HIST_IGNORE_ALL_DUPS # Delete an old recorded event if a new event is a duplicate.
setopt HIST_IGNORE_SPACE # Commands prefixed with space are not recorded.
setopt HIST_FIND_NO_DUPS # Do not display a previously found event.
setopt HIST_IGNORE_SPACE # Do not record an event starting with a space.
setopt HIST_SAVE_NO_DUPS # Do not write a duplicate event to the history file.
setopt HIST_VERIFY # Do not execute immediately upon history expansion.
setopt HIST_BEEP # Beep when accessing non-existent history.
setopt COMPLETE_IN_WORD # Complete from both ends of a word.
setopt PATH_DIRS # Perform path search even on command names with slashes.
setopt TRANSIENTRPROMPT # Clear the right prompt (RPS1) on command entry (only useful when using themes that actually define RPS1).
setopt PROMPT_SUBST # Interpret variables/functions every time when rendering a prompt.
unsetopt correct_all # Disable the extremely annoying auto-correct.
unsetopt CASE_GLOB # Case-insensitive matching.
# Colors.
# coreutils ls(1) uses LS_COLORS.
export LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:"
# macOS ls(1) uses $LSCOLORS.
export LSCOLORS="Gxfxcxdxbxegedabagacad"
if [[ "$OSTYPE" == darwin* ]]; then
alias ls="ls -G"
else
alias ls="ls --color=tty"
fi
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
# Partial-word, and then substring completion.
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
# Allow arrow selection.
zstyle ':completion:*:*:*:*:*' menu select
# Keybindings
#
# NOTE: In order to reliabibly configure keybinds for your current terminal
# when you can't use the $terminfo array, use <ctrl-V><key-combination> to
# see the raw value.
# Edit the current command in $EDITOR, press <ctrl-X><ctrl-E>. I'm using
# this longish shortcut because it's the same as bash's. Hoping I'll get
# used to it...
autoload -z edit-command-line
zle -N edit-command-line
bindkey "^X^E" edit-command-line
# Make search up and down work, so partially type and hit up/down to find
# relevant stuff. The defaults are (zsh -d -f "bindkey | less"):
# (up|down)-line-or-history, which I like less.
#
# $ man zshzle # Search for "History Control".
# $ man zshcontrib # Search for "up-line-or-beginning-search".
autoload -U up-line-or-beginning-search
zle -N up-line-or-beginning-search
bindkey '^[[A' up-line-or-beginning-search # [UpArrow]
autoload -U down-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey '^[[B' down-line-or-beginning-search # [DownArrow]
# xterm bindings for ctrl-right/left arrow.
bindkey '^[[1;5C' forward-word # [Ctrl-RightArrow] - move forward one word
bindkey '^[[1;5D' backward-word # [Ctrl-LeftArrow] - move backward one word
# Apparently my urxvt has the following...
bindkey '^[Oc' forward-word # [Ctrl-RightArrow] - move forward one word
bindkey '^[Od' backward-word # [Ctrl-LeftArrow] - move backward one word
# iTerm2, bind fn-right/left arrow.
bindkey '^[[H' backward-word
bindkey '^[[F' forward-word
##############################
# Random user configuration. #
##############################
# Minimal tmux/fzf integration: CTRL-E to select and enter a tmux session.
hash tmux &>/dev/null && hash fzf &>/dev/null && source ${0:A:h}/zsh/tmux-fzf.zsh
# Aliases
alias gl='git pull'
alias l='ls -lah'
alias ll='ls -lh'
alias grep='grep --color=auto'
alias stamp="date +'%s'"
alias luajit="rlwrap luajit"
alias rg='rg -S' # Default to smart-case.
# Misc
# Allow expanding aliases in zsh. Press ctrl-alt-e, just like in Bash.
expand-aliases() {
unset 'functions[_expand-aliases]'
functions[_expand-aliases]=$BUFFER
(($+functions[_expand-aliases])) &&
BUFFER=${functions[_expand-aliases]#$'\t'} &&
CURSOR=$#BUFFER
}
zle -N expand-aliases
bindkey '\e^E' expand-aliases
export LESS="-R -S"
# Set manpager to nvim if possible. Highlighted linkable manpages!
if command -v nvim >/dev/null 2>&1 ; then
export MANPAGER='nvim --clean -c "colo sorbet" +Man!'
fi
# Use the fastest way available to supply fzf(1) with filenames instead of find.
if hash fd &>/dev/null ; then
export FZF_DEFAULT_COMMAND='fd --type file --follow --hidden --exclude .git'
elif hash rg &>/dev/null ; then
export FZF_DEFAULT_COMMAND="rg --files --hidden -g '!.git/'"
elif hash ag &>/dev/null ; then
export FZF_DEFAULT_COMMAND="ag -i --nocolor --nogroup --hidden \
--ignore .git \
--ignore .svn \
--ignore .hg \
--ignore .DS_Store \
-g ''"
fi
# runargs is basically the same as xargs(1) but implemented as a shell
# function so you don't have ugly long-running xargs processes all over the
# place. Forces stdin to be the TTY (because the real stdin are the
# filenames, of course).
runargs() {
local -a args
if [ -n "$ZSH_VERSION" ] ; then
# Stupid hack to get Vim's sh filetype to not barf on the quotes. Which
# I require because I want shellcheck to run on all my shell files, but
# setting the zsh filetype disables shellcheck because shellcheck
# removed support for zsh...
local in=/dev/stdin
args=(${(@f)"$(<$in)"})
elif [ -n "$BASH_VERSION" ] ; then
readarray -t args
else
return 1
fi
(( ${#args[@]} == 0 )) && return 0
"$@" "${args[@]}" </dev/tty
}
# vimin opens files passed on stdin in $EDITOR (in splits, so needs vimlike).
vimin() {
runargs "${EDITOR:-vim}" -O
}
# fe <file|pattern>... - Open the selected file(s) with the default editor
fe() {
# If stdin is not a TTY, source file(s) from stdin instead of ag(1).
if [ ! -t 0 ] ; then
fzf -0 -1 -m --query="$*" | vimin
return
fi
declare -al query
declare -al paths
for arg ; do
if [[ "$arg" = */ ]] ; then
paths+=("$arg")
else
query+=("$arg")
fi
done
if command -v fd >/dev/null 2>&1 ; then
fd --type f --no-ignore
else
find -type f
fi | fzf -0 -1 -m --query="${query[@]}" | vimin
}
# vimp opens the argument if it can be found your $PATH.
vimp() {
which -p "$1" | vimin
}
# fkill - kill process
fkill() {
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
if [ "x$pid" != "x" ] ; then
kill -${1:-9} "$pid"
fi
}
# mkcd creates and cd's into a directory.
mkcd() {
[ -n "$1" ] && mkdir -p "$@" && cd "$1"
}
# Print the header (the first line of input) and then run the specified
# command on the body (the rest of the input) use it in a pipeline, e.g. ps
# | body grep somepattern
body() {
IFS= read -r header
printf '%s\n' "$header"
"$@"
}
# Enhanced less invocation that will keep a single header line around in the
# prompt.
hless() {
IFS= read -r header
less -PM"$header" -M
}
# Searches text for a string but highlights instead of filtering. If you
# want to combine this with a pager, it can be piped to less because it has
# --color=always. Alternatively, one can use less -p PATTERN.
greph() {
local pat="$1"
shift
grep --color=always -E "$pat|$" $@
}
# execute this to try to import any running ssh-agent sessions
ssh-reagent() {
for agent in /tmp/ssh-*/agent.*(N) ; do
export SSH_AUTH_SOCK=$agent
if ssh-add -l 2>&1 > /dev/null; then
echo "Found working SSH Agent:"
ssh-add -l
return 0
fi
done
echo "Cannot find ssh agent - maybe you should reconnect and forward it?"
echo 'Or run `eval $(ssh-agent) && ssh-add <your-key...>`'
return 1
}
# Evaluate something in lua.
le() {
# If no arguments are given, evaluate stdin as a lua expression. We can't
# use "lua -" as specified in the man page because that would avoid
# printing the result if the user didn't specifically call print(),
# write() or others. It's cumbersome to type those all the time, so do it
# by default.
local expr
(( $# == 0 )) && expr="$(</dev/stdin)" || expr="$*"
# Dirty hack to force printing of the passed in expression.
case "$expr" in
*print*|*write*) ;;
*) expr="print($expr)" ;;
esac
( eval "$(luarocks path)" &&
LUA_INIT="@$HOME/.lua/init.lua" luajit -e "$expr" )
}
# Start a luajit interpreter with some libraries preloaded.
lj() {
# Add local and global rocks to the lua search path, then preload those
# libraries and some other helper code from init.lua.
( eval "$(luarocks path)" && luajit -i $HOME/.lua/init.lua --examples )
}