summaryrefslogtreecommitdiff
path: root/tw/home/files/zshrc
blob: ca4481e2a2cd76d1bd2af60b06e8c0f703a0ce7a (about) (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
# Zsh configuration  -*- sh -*-

# Load functions from Guix profile, where packages like
# zsh-completions are installed.
if [ -d ~/.guix-home/profile/share/zsh/site-functions ]; then
  fpath+=(~/.guix-home/profile/share/zsh/site-functions)
fi

setopt extendedglob interactivecomments

# So we don't have to call tput. Used in zstyle args below.
autoload -Uz colors
colors

# GPG-agent setup
GPG_TTY=$(tty) && export GPG_TTY

## Key bindings
# vi-like key mappings
bindkey -v

bindkey '^[[A' up-line-or-history               # up
bindkey '^[[B' down-line-or-history             # down

bindkey '^[[1;5A' up-line-or-history            # C-up
bindkey '^[[1;5B' down-line-or-history          # C-down
bindkey '^[[1;5C' vi-forward-word               # C-right
bindkey '^[[1;5D' vi-backward-word              # C-left

bindkey '^[[H' vi-beginning-of-line             # home
bindkey '^[[F' vi-end-of-line                   # end

## History setup
# HISTSIZE should be set through Guix's home environment service.
SAVEHIST=${HISTSIZE=10000000}
HISTFILE=${XDG_DATA_HOME:-$HOME/.local/share}/zsh/history
[ -e "$HISTFILE" ] || mkdir -p "${HISTFILE%/*}"
setopt appendhistory sharehistory incappendhistory extendedhistory histverify histreduceblanks

## Completion setup

# don't need to run 'hash -r' after commands are added/removed
zstyle ':completion:*' rehash true

# enable completion list colours
# https://linuxshellaccount.blogspot.com/2008/12/color-completion-using-zsh-modules-on.html
# This needs $color[C], not $fg[C]; i.e. only the colour number, not the full escape sequence.
zstyle ':completion:*:*:*:*:hosts' list-colors "=*=${color[blue]}"
zstyle ':completion:*:*:*:*:users' list-colors "=*=${color[green]}=${color[red]}"

# formatting and messages
# https://www.masterzen.fr/2009/04/19/in-love-with-zsh-part-one/
zstyle ':completion:*' verbose yes
zstyle ':completion:*:descriptions' format "${fg[green]}%B--- %d%b"
zstyle ':completion:*:messages' format '%d'
zstyle ':completion:*:warnings' format "${fg[red]}%BNo matches for:%b %d"
zstyle ':completion:*:corrections' format "${fg[yellow]}%B%d (%e char)%b"

# https://stackoverflow.com/a/24237590
#zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'

# (mostly) from compinstall
zstyle ':completion:*' auto-description '(param: %d)'
zstyle ':completion:*' completer _expand _complete _ignored _approximate
zstyle ':completion:*' expand prefix suffix
zstyle ':completion:*' file-sort name
zstyle ':completion:*' group-name ''
zstyle ':completion:*' ignore-parents parent pwd directory
zstyle ':completion:*' insert-unambiguous false
eval "$(dircolors)"    # set LS_COLORS
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' list-suffixes true
zstyle ':completion:*' matcher-list '' 'm:{[:lower:]}={[:upper:]}' 'r:|[._-]=** r:|=**' 'l:|=* r:|=*'
zstyle ':completion:*' max-errors 3
zstyle ':completion:*' original true
zstyle ':completion:*' verbose true

autoload -Uz compinit
if mkdir -p "${XDG_CACHE_HOME:-$HOME/.cache}/zsh"; then
  compinit -d "${XDG_CACHE_HOME:-$HOME/.cache}/zsh/zcompdump-$ZSH_VERSION"
else
  # Don't use dump file.
  compinit -D
fi

## Aliases
alias e="${VISUAL-$EDITOR}"
# Use ASYNC_EDITOR so I can continue using the shell while the editor remains
# open in a separate window. If no ASYNC_EDITOR is defined (e.g. on servers),
# then use the regular editor for convenience.
alias em="${ASYNC_EDITOR-${VISUAL-$EDITOR}}"
alias se='sudo -e'
alias ls='\ls --color=auto -h'
alias la='\ls --color=auto -hA'
alias grep='\grep --color=auto'
alias diff='\diff -s --color=auto'
alias cdiff='\diff -s --color=always'
alias xcopy='xclip -in -selection clipboard'
alias xpaste='xclip -out -selection clipboard'
alias ipy='ipython3 --autoindent --automagic --pprint --no-banner --no-confirm-exit --term-title --autocall=1 --colors=Neutral'
alias rot13='caesar 13'
alias pdsh='\pdsh -SR ssh -f 64'
alias wget='\wget --hsts-file="${XDG_CACHE_HOME:-$HOME/.cache}/wget-hsts"'
alias aurora='REQUESTS_CA_BUNDLE=${XDG_CONFIG_HOME:-$HOME/.config}/cern-ca-bundle.crt \aurora'
alias aurora_admin='REQUESTS_CA_BUNDLE=${XDG_CONFIG_HOME:-$HOME/.config}/cern-ca-bundle.crt \aurora_admin'
alias nomad='NOMAD_TOKEN=$(pass cern/ci/nomad-bootstrap-token | head -1) \nomad'
alias nomad-pack='NOMAD_TOKEN=$(pass cern/ci/nomad-bootstrap-token | head -1) \nomad-pack'
alias nomad-diskfree='NOMAD_TOKEN=$(pass cern/ci/nomad-bootstrap-token | head -1) \nomad-diskfree'
alias levant='NOMAD_TOKEN=$(pass cern/ci/nomad-bootstrap-token | head -1) \levant'
alias consul='CONSUL_HTTP_TOKEN=$(pass cern/ci/consul-bootstrap-token | head -1) \consul'
alias vault='VAULT_TOKEN=$(pass cern/ci/vault-root-token | head -1) \vault'
nomad-shell () {
  nomad alloc exec "$1" sh -c 'export TERM=xterm-256color HOME=$NOMAD_TASK_DIR PS1="\\u@\\h \\w \\\$ "; cd; exec bash -i'
}
restic.vin () {
  if [ $# -eq 0 ]; then
    echo 'Pass the repo name as the first parameter.' >&2
  else
    local repo user
    case $1 in
      */*) user=${1%%/*} repo=${1#*/} ;;
      *) user=$USERNAME repo=$1 ;;
    esac
    shift
    echo "running: restic -r vin.wg/$user/$repo $*" >&2
    RESTIC_PASSWORD_COMMAND="pass computers/vin/restic-repos/$user-$repo" \
    RESTIC_REPOSITORY="rest:http://$user:$(pass "computers/vin/restic-server/$user" | head -1)@vin.wg:8181/$user/$repo" \
    command restic "$@"
  fi
}

# Git aliases
alias ga='git add'
alias gb='git branch'
alias gc='git commit -v'
alias gd='git diff'
alias gdk='git difftool --no-symlinks --dir-diff'
alias gds='git diff --staged'
alias gf='git fetch'
alias gfa='git fetch --all --prune --tags'
alias gk='git checkout'
alias gkb='git checkout -b'
alias gg='git glog'
ggm () { git glog "$(git merge-base "${1-$(git config --get init.defaultBranch)}" "${2-HEAD}")..${2-HEAD}"; }
alias gga='git glog --all'
alias ggr='git grep'
alias gl='git pull'
alias gp='git push'
alias gr='git rebase'
alias gri='git rebase --interactive'
alias gs='git status'

if recsel --version 2>/dev/null | grep -qFx 'recsel (GNU recutils) 1.8'; then
  # Recutils 1.8 has a bug when TMPDIR is on a different mount point, see
  # https://lists.gnu.org/archive/html/bug-recutils/2019-08/msg00001.html.
  for _recutil in rec2csv recdel recfix recfmt recinf recins recsel recset; do
    alias "$_recutil=TMPDIR=. $_recutil"
  done
  unset _recutil
fi

## Plugins
load_plugin () {
  local try_path
  for try_path in {/usr,"$GUIX_ENVIRONMENT","$HOME/.guix-profile","$HOME/.guix-home/profile"}"/share/zsh/plugins/$1/$1"{.plugin,}.zsh; do
    [ -r "$try_path" ] && . "$try_path" && return 0
  done
  return 1
}

if load_plugin zsh-autosuggestions; then
  bindkey '^ ' autosuggest-accept
  bindkey -M vicmd '^I' autosuggest-accept
  ZSH_AUTOSUGGEST_STRATEGY=(history completion)
  ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20
  ZSH_AUTOSUGGEST_USE_ASYNC=1
  ZSH_AUTOSUGGEST_MANUAL_REBIND=1
fi

if load_plugin zsh-history-substring-search; then
  bindkey '^[[A' history-substring-search-up
  bindkey '^[[B' history-substring-search-down
  bindkey -M emacs '^P' history-substring-search-up
  bindkey -M emacs '^N' history-substring-search-down
  bindkey -M vicmd 'k' history-substring-search-up
  bindkey -M vicmd 'j' history-substring-search-down
  HISTORY_SUBSTRING_SEARCH_FUZZY=1
fi

load_plugin fast-syntax-highlighting ||
  load_plugin zsh-syntax-highlighting