summaryrefslogtreecommitdiffstats
path: root/dotfiles/zshrc
blob: 35026f3406e84d0ac7a7ae13fed03913ec38987c (plain)
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
# Some variables are defined in ../commandline.nix

# For users using home-manager
if [ -f "$HOME/.nix-profile/etc/profile.d/hm-session-vars.sh" ]; then
	. "$HOME/.nix-profile/etc/profile.d/hm-session-vars.sh"
fi

# Grml Zsh configuration {{{
# ==========================

# With prompt specific sequences
local dominated="%{${dominant_escape_code}%}"
local dimmed="%{${dim_fg_escape_code}%}"

# Use the dominant color in the prompt
# In the username item for non-root, in the host item for root
if [ "$EUID" -ne 0 ]; then
	zstyle ':prompt:grml:left:items:user' pre "${dominated}%B"
else
	zstyle ':prompt:grml:left:items:host' pre "${dominated}"
	zstyle ':prompt:grml:left:items:host' post "%f"
fi

# Remove the command-path completion of sudo set by grml, since Nix has
# non-standard PATHs
zstyle -d ':completion:*:sudo:*' command-path

# Change the style of VCS prompt info
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' stagedstr "s"
zstyle ':vcs_info:*' unstagedstr "u"
zstyle ':vcs_info:*' formats "${dimmed}(%f%s${dimmed})-[${dominated}%b${dimmed}]-{%f%c%u${dimmed}}%f " "zsh: %r"
zstyle ':vcs_info:*' actionformats "${dimmed}(%f%s${dimmed})-[${dominated}%b${dimmed}|%F{yellow}%a${dimmed}]-{%f%c%u${dimmed}}%f " "zsh: %r"

# }}}

# Completion system {{{
# =====================

# Change completion messages
zstyle ':completion:*:corrections'  format "${dominated}%BFTFY: %b${dominated}(errors: %e)%f"
zstyle ':completion:*:descriptions' format "${dominated} -- So you want a %B%d%b${dominated}? --%f"
zstyle ':completion:*:messages'     format "${dominated}You got mail: %f%d"
zstyle ':completion:*:warnings'     format "%F{red}I'm sorry Dave, but I cannot allow you to find%f %d"

# Always activate selection in completion
zstyle ':completion:*' menu select

# Case insensitive completion and substring completion
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'

# Increase the number of errors based on the length of the typed word. But make
# sure to cap (at 7) the max-errors to avoid hanging.
zstyle -e ':completion:*:approximate:*' max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3>7?7:($#PREFIX+$#SUFFIX)/3))numeric)'

# Tmux Completion {{{
# -------------------

# From: https://blog.plenz.com/2012-01/zsh-complete-words-from-tmux-pane.html
# And: https://gist.github.com/blueyed/6856354

_tmux_pane_words() {
	local expl
	local -a w
	if [[ -z "$TMUX_PANE" ]]; then
		_message "not running inside tmux!"
		return 1
	fi

	# Based on vim-tmuxcomplete's splitwords function.
	# https://github.com/wellle/tmux-complete.vim/blob/master/sh/tmuxcomplete
	_tmux_capture_pane() {
		tmux capture-pane -J -p -S -100 $@ |
		# Remove "^C".
		sed 's/\^C\S*/ /g' |
		# copy lines and split words
		sed -e 'p;s/[^a-zA-Z0-9_]/ /g' |
		# split on spaces
		tr -s '[:space:]' '\n' |
		# remove surrounding non-word characters
		=grep -o "\w.*\w"
	}
	# Capture current pane first.
	w=( ${(u)=$(_tmux_capture_pane)} )
	echo $w > /tmp/w1
	local i
	for i in $(tmux list-panes -F '#D'); do
		# Skip current pane (handled before).
		[[ "$TMUX_PANE" = "$i" ]] && continue
		w+=( ${(u)=$(_tmux_capture_pane -t $i)} )
	done
	_wanted values expl 'word from current tmux pane' compadd -a w
}

zle -C tmux-pane-words-prefix   complete-word _generic
zle -C tmux-pane-words-anywhere complete-word _generic

zstyle ':completion:tmux-pane-words-(prefix|anywhere):*' completer _tmux_pane_words
zstyle ':completion:tmux-pane-words-(prefix|anywhere):*' ignore-line current
# Display the menu on first execution of the hotkey.
zstyle ':completion:tmux-pane-words-(prefix|anywhere):*' menu yes select
zstyle ':completion:tmux-pane-words-anywhere:*' matcher-list 'b:=* m:{A-Za-z}={a-zA-Z}'

# }}}

# }}}

# Bindkeys {{{
# ============

bindkey -v

bindkey '^X^T' tmux-pane-words-prefix

bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
bindkey '^[OA' history-substring-search-up
bindkey '^[OB' history-substring-search-down

autoload -rUz run-help-nix
bindkey '^K' run-help

bindkey '^P' push-line-or-edit

# }}}

# Aliases {{{
# ===========

# Some aliases taken from the Prezo zsh configuration framework:
# https://github.com/sorin-ionescu/prezto

# Git {{{
# -------

alias g='git'

# Branch (b)
alias gb='git branch'

# Commit (c)
alias  gc='git commit --verbose'
alias gca='git commit --verbose --all'
alias gcf='git commit --amend --reuse-message HEAD'
alias gcs='git show'

alias gco='git checkout'
alias gcO='git checkout --patch'

# Fetch (f)
alias   gf='git fetch'
alias  gfm='git pull'
alias  gfc='git clone'
alias gfcr='git clone --recurse-submodules'

# Index (i)
alias gia='git add'
alias giA='git add --patch'
alias gid='git diff --cached'
alias gidt=' GIT_EXTERNAL_DIFF=difft gid'
alias giD='git diff --cached --word-diff'

# Log (l)
logMediumFormat='%C(bold)Commit:%C(reset) %C(green)%H%C(red)%d%n%C(bold)Author:%C(reset) %C(cyan)%an <%ae>%n%C(bold)Date:%C(reset)   %C(blue)%ai (%ar)%C(reset)%n%+B'
logBriefFormat='%C(green)%h%C(reset) %s%n%C(blue)(%ar by %an)%C(red)%d%C(reset)%n'
logOnelineFormat='%C(green)%h%C(reset) %s%C(red)%d%C(reset)%n'

alias  gl='git log --topo-order --pretty=format:"${logMediumFormat}"'
alias gls='git log --topo-order --stat --pretty=format:"${logMediumFormat}"'
alias gld='git log --topo-order --stat --patch --full-diff --pretty=format:"${logMediumFormat}"'
alias glg='git log --topo-order --all --graph --pretty=format:"${logOnelineFormat}"'
alias glo='git log --topo-order --pretty=format:"${logOnelineFormat}"'
alias glb='git log --topo-order --pretty=format:"${logBriefFormat}"'
alias glc='git shortlog --summary --numbered'

# Merge (m)
alias  gm='git merge'
alias gmt='git mergetool'

# Push (p)
alias  gp='git push'
alias gpf='git push --force-with-lease'
alias gpt='git push --tags'

# Rebase (r)
alias gr='git rebase'

# Stash (s)
alias gs='git stash'

# Working Copy (w)
alias gws='git status --ignore-submodules=none'
alias gwd='git diff'
alias gwdt=' GIT_EXTERNAL_DIFF=difft gwd'
alias gwD='git diff --word-diff'

# }}}

# Prevents some mistakes
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'

# }}}

# Helpful functions {{{
# =====================

function ssht
{
	/usr/bin/env ssh -t "$@" "tmux new-session -A -s 'ssh-$HOST'"
}

compdef ssht=ssh

# From GRML

function cdt {
    builtin cd "$(mktemp -d)"
    builtin pwd
}

zmodload -a zsh/stat zstat

function sll {
    if [[ -z ${1} ]] ; then
        printf 'Usage: %s <symlink(s)>\n' "${0}"
        return 1
    fi

    local file jumpd curdir
    local -i 10 RTN LINODE i
    local -a    SEENINODES
    curdir="${PWD}"
    RTN=0

    for file in "${@}" ; do
        SEENINODES=()
        ls -l "${file:a}"   || RTN=1

        while [[ -h "$file" ]] ; do
			LINODE=$(zstat -L +inode "${file}")
			for i in ${SEENINODES} ; do
				if (( ${i} == ${LINODE} )) ; then
					builtin cd -q "${curdir}"
					print 'link loop detected, aborting!'
					return 2
				fi
			done
			SEENINODES+=${LINODE}
            jumpd="${file:h}"
            file="${file:t}"

            if [[ -d ${jumpd} ]] ; then
                builtin cd -q "${jumpd}"  || RTN=1
            fi
            file=$(readlink "$file")

            jumpd="${file:h}"
            file="${file:t}"

            if [[ -d ${jumpd} ]] ; then
                builtin cd -q "${jumpd}"  || RTN=1
            fi

            ls -l "${PWD}/${file}"     || RTN=1
        done
        shift 1
        if (( ${#} >= 1 )) ; then
            print ""
        fi
        builtin cd -q "${curdir}"
    done
    return ${RTN}
}

# }}}

# Abbreviations {{{
# =================

# Abbreviation system provided by the grml config
# More info here:
# http://www.zshwiki.org/home/examples/zleiab

abk=(
	'P' '|& ${(z)PAGER}'
	'L' '|& ${(z)PAGER}'

	'HL' ' --help |& ${(z)PAGER}'

	'G'  '|& rg'
	'FG' '|& rg -F'

	'H' '| head'
	'T' '| tail'
	'C' '| wc -l'
)

# }}}

# GPG / SSH agents {{{
# ====================

#function _gpg-agent-update-tty
#{
#	gpg-connect-agent UPDATESTARTUPTTY /bye >/dev/null
#}
#
#add-zsh-hook preexec _gpg-agent-update-tty

# }}}

# vim: fdm=marker