summary refs log tree commit diff stats
path: root/user/zsh/zshrc
blob: 8b66be2491ebb5a022e7c6cc4cd62da21da9b903 (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
# -*- mode: sh; -*-
setopt hist_ignore_space
setopt inc_append_history_time
setopt transient_rprompt

WORDCHARS=${${WORDCHARS//[-.=]}//[\/]}

zmodload zsh/terminfo

bindkey '\e[3~' delete-char

bindkey '\C-hd' describe-key-briefly

dc () {
  if [[ -x ./docker-compose ]]
  then
    ./docker-compose "$@"
  else
    docker-compose "$@"
  fi
}
compdef '_dispatch docker-compose docker-compose' dc

field () {
  awk "{ print \$${1:-0} }"
}
alias f=field

delink () {
  if [[ ! -h "$1" ]]
  then
    echo "$1 is not a symlink"
    return 1
  fi
  local src=$(readlink "$1")
  rm "$1"
  cp "$src" "$1"
  chmod u+w "$1"
}

mvlink () {
	src="$1"
	target="$2"
	if [[ -h "$src" ]]
	then
		echo "$src is already a symlink"
		return src
	fi
	if [[ -f "$target" ]]
	then
		echo "$target already exists"
	fi
	if [[ -d "$target" ]]
	then
		target="$target/$(basename $src)"
	fi
	mv "$src" "$target"
	ln -s "$target" "$src"
}

backward-argument () {
  local WORDCHARS="\!\`~#@$%^&*()-_=+[{]}\|;:,<.>/?\'\""
  zle backward-word
}

forward-argument () {
  local WORDCHARS="\!\`~#@$%^&*()-_=+[{]}\|;:,<.>/?\'\""
  zle forward-word
}

backward-kill-argument () {
  local WORDCHARS="\!\`~#@$%^&*()-_=+[{]}\|;:,<.>/?\'\""
  zle backward-kill-word
}

kill-argument () {
  local WORDCHARS="\!\`~#@$%^&*()-_=+[{]}\|;:,<.>/?\'\""
  zle kill-word
}

zle -N backward-argument
zle -N forward-argument
zle -N kill-argument
zle -N backward-kill-argument
bindkey '\e^b' backward-argument
bindkey '\e^f' forward-argument
bindkey '\e^d' backward-kill-argument
bindkey '\e^k' kill-argument

autoload -Uz edit-command-line
zle -N edit-command-line
bindkey '^x^e' edit-command-line

sort=${commands[gsort]:-$commands[sort]}

ds () {
  du -hd1 "$@" | $sort -h
}
zle-upify() {
  buf="$(echo "$BUFFER" | sed 's/[ |]*$//')"
  tmp="$(mktemp)"
  eval "$buf |& up -o '$tmp' 2>/dev/null"
  cmd="$(tail -n +2 "$tmp")"
  rm -f "$tmp"
  BUFFER="$BUFFER | $cmd"
  zle end-of-line
}
zle -N zle-upify

bindkey '\e^u' zle-upify
bindkey '\e^p' zle-upify

# returns the first ghq root, whereas $GHQ_ROOT returns all
hash -d p=$ghq_root[0]
hash -d go=${GOPATH:-$HOME/go}

zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ${XDG_CACHE_HOME:-$HOME/.cache}/zsh/completion

zle -C hist-complete complete-word _generic
zstyle ':completion:hist-complete:*' completer _history
bindkey '\e ' hist-complete

zstyle ':completion:*' matcher-list 'm:{a-zA-Z-_}={A-Za-z_-}' 'r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' completer _expand _complete _match

zstyle -e ':completion:*' hosts 'reply=($(< ~/.hosts))'
zstyle ':completion:*:ssh:*' tag-order '! users'

# General configuration

if [[ $TERM == "dumb" ]]
then
  PROMPT='%B%F{green}%n %F{blue}%~%b%f
%# '
  unsetopt zle
else
  AGKOZAK_USER_HOST_DISPLAY=${+SSH_CLIENT}
  AGKOZAK_SHOW_STASH=0
  AGKOZAK_SHOW_VIRTUALENV=0
  AGKOZAK_PROMPT_DIRTRIM=0

  check_kubectl_context () {
    if [[ "$PWD" =~ satoshipay ]]
    then
      kubectl_context=$(kubectl config current-context)
      kubectl_namespace=$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"${kubectl_context}\")].context.namespace}")
      if [[ $kubectl_context =~ production ]]
      then
        kubectl_colour=red
        kubectl_release=satoshipay-${kubectl_namespace}
      else
        kubectl_colour=yellow
        if [[ -z $mr ]]
        then
          kubectl_release=satoshipay-${kubectl_namespace}-staging
        else
          kubectl_release=story-${mr}-${kubectl_namespace}
        fi
      fi
      prompt_context="%F{$kubectl_colour}[${kubectl_context}:${kubectl_namespace}]%f"
    else
      prompt_context=
    fi
  }
  if [[ -z $SSH_CLIENT ]]
  then
    precmd_functions+=(check_kubectl_context)
    AGKOZAK_LEFT_PROMPT_ONLY=${+SSH_CLIENT}
    AGKOZAK_CUSTOM_RPROMPT='${prompt_context}%(3V.%F{${AGKOZAK_COLORS_BRANCH_STATUS}%3v%f.)'
  fi
fi

export FZF_CTRL_T_COMMAND='(fd --hidden --follow --exclude ".git" .) 2> /dev/null'
export FZF_ALT_C_COMMAND='fd --type d --hidden --follow --exclude ".git" .'
export FZF_DEFAULT_COMMAND=$FZF_CTRL_T_COMMAND

bindkey '^t' transpose-chars
bindkey '^x^f' fzf-file-widget

zstyle ":anyframe:selector:fzf:" command "fzf --height 40%"

bindkey '\es' anyframe-widget-cd-ghq-repository
bindkey '\ej' anyframe-widget-cd-ghq-repository
bindkey '^x^k' anyframe-widget-kill

unsetopt flow_control       # Let me use ^S and ^Q

esetenv () {
  emacsclient -e "(setenv \"$1\" \"$2\")"
}

ecpenv () {
  esetenv "$1" "${(P)1}"
}
compdef '_parameters' ecpenv