aboutsummaryrefslogtreecommitdiff
path: root/.zshrc
blob: 347f3c0e5cffd87ae6fc787ee2ca6fbf2bb1d28e (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
[ $DEBUG_DOTFILES ] && echo "Loading .zshrc"

# Load initialization
[ $DEBUG_DOTFILES ] && echo "  Loading ~/.config/shell/init"
source ~/.config/shell/init

# load the sourcesfile, which in turn will source more functionality
[ $DEBUG_DOTFILES ] && echo "  Loading ~/.config/shell/sources"
source ~/.config/shell/sources

# autoload compontents {{{
[ $DEBUG_DOTFILES ] && echo "  Autoloading components"
autoload -U colors && colors
autoload -U compinit && compinit
autoload -U promptinit && promptinit
# }}}

# set base options {{{
[ $DEBUG_DOTFILES ] && echo "  Setting base options"
setopt appendhistory autocd autopushd extendedglob histignorespace prompt_subst vi
unsetopt beep nomatch notify
# }}}

# vi mode
[ $DEBUG_DOTFILES ] && echo "  Setting vi-mode"
bindkey -v

# Extend vi mode with some happy good vim stuff
function zle-move-to-buffer-beginning {
	CURSOR=0
}

zle -N zle-move-to-buffer-beginning

# zsh apparently doesn't read inputrc, so fix some "special" keys manually
typeset -g -A key

key[Home]="${terminfo[khome]}"
key[End]="${terminfo[kend]}"
key[Insert]="${terminfo[kich1]}"
key[Backspace]="${terminfo[kbs]}"
key[Delete]="${terminfo[kdch1]}"
key[Up]="${terminfo[kcuu1]}"
key[Down]="${terminfo[kcud1]}"
key[Left]="${terminfo[kcub1]}"
key[Right]="${terminfo[kcuf1]}"
key[PageUp]="${terminfo[kpp]}"
key[PageDown]="${terminfo[knp]}"
key[Shift-Tab]="${terminfo[kcbt]}"

[[ -n "${key[Home]}"      ]] && bindkey -- "${key[Home]}"      beginning-of-line
[[ -n "${key[End]}"       ]] && bindkey -- "${key[End]}"       end-of-line
[[ -n "${key[Insert]}"    ]] && bindkey -- "${key[Insert]}"    overwrite-mode
[[ -n "${key[Backspace]}" ]] && bindkey -- "${key[Backspace]}" backward-delete-char
[[ -n "${key[Delete]}"    ]] && bindkey -- "${key[Delete]}"    delete-char
[[ -n "${key[Up]}"        ]] && bindkey -- "${key[Up]}"        up-line-or-history
[[ -n "${key[Down]}"      ]] && bindkey -- "${key[Down]}"      down-line-or-history
[[ -n "${key[Left]}"      ]] && bindkey -- "${key[Left]}"      backward-char
[[ -n "${key[Right]}"     ]] && bindkey -- "${key[Right]}"     forward-char
[[ -n "${key[PageUp]}"    ]] && bindkey -- "${key[PageUp]}"    beginning-of-buffer-or-history
[[ -n "${key[PageDown]}"  ]] && bindkey -- "${key[PageDown]}"  end-of-buffer-or-history
[[ -n "${key[Shift-Tab]}" ]] && bindkey -- "${key[Shift-Tab]}" reverse-menu-complete

# Bind some emacs-style bindings in insert mode
bindkey -M viins '^a' beginning-of-line
bindkey -M viins '^b' backward-char
bindkey -M viins '^e' end-of-line
bindkey -M viins '^f' forward-char
bindkey -M viins '^k' kill-line
bindkey -M viins '^n' history-beginning-search-forward
bindkey -M viins '^p' history-beginning-search-backward
bindkey -M viins '^u' kill-region
bindkey -M viins '^y' yank

# Bind the vim bindings
bindkey -M vicmd "gg" zle-move-to-buffer-beginning

# configure components {{{
[ $DEBUG_DOTFILES ] && echo "  Setting autocompletions"
zstyle :compinstall filename '/home/tyil/.zshrc'
zstyle ':completion:*' menu select

# Enable comments in interactive zsh
set -k
# }}}

function precmd() # {{{
{
	# format the last command's exitcode
	EXITCODE=$(printf "%03i" "$?")

	# set window title
	echo -en "\e]2;`whoami`@`hostname -f`:`pwd`\a"

	# git info {{{
	if git rev-parse --is-inside-work-tree &> /dev/null
	then
		# Set coloring of the branch name
		if [ "$(git status -uno --porcelain 2> /dev/null)" = "" ]
		then
			GITBRANCHC="%F{2}"
		else
			GITBRANCHC="%F{3}"
		fi

		# Get the branch name
		GITBRANCH=$(git rev-parse --abbrev-ref HEAD)

		if [ "$GITBRANCH" = "HEAD" ]
		then
			GITBRANCH=$(git rev-parse --short HEAD 2> /dev/null)
		fi

		# Set the prompt
		PGIT="%F{8}·%f${GITBRANCHC}${GITBRANCH}%f%F{8}"
	else
		# Not in a git repo, set a regular prompt
		PGIT="%F{8}"
	fi
	# }}}

	# set hostname color depending on local or remote connection {{{
	HOSTNAME_COLOR="%F{5}"

	case "$(ps -o comm= -p $PPID)" in
		sshd|*/sshd|mosh*) HOSTNAME_COLOR="%F{6}" ;;
	esac
	# }}}

	# Color last directory name in case of python venv
	PROMPT_PATH='%F{4}%~%k'

	if [[ -n "$PYTHON_VENV_DIR" ]]
	then
		PROMPT_PATH+='%F{6}#'"$(awk -F/ '{ print $(NF-1) }' <<< "$PYTHON_VENV_DIR")"
	fi
}
# }}}

# setup vi-mode indicators {{{
VIMODE_I="%F{8}"
VIMODE_N="%B%F{3}"
VIMODE_U="%B%F{1}"
# }}}

# set vimode coloring {{{
local vi_mode="$VIMODE_I"
vi_mode_indicator () {
	case ${KEYMAP} in
		(vicmd)      echo $VIMODE_N ;;
		(main|viins) echo $VIMODE_I ;;
		(*)          echo $VIMODE_U ;;
	esac
}
# }}}

# reset mode-marker and prompt whenever the keymap changes {{{
function zle-line-init zle-keymap-select {
	vi_mode="$(vi_mode_indicator)"
	zle reset-prompt
}

zle -N zle-line-init
zle -N zle-keymap-select
# }}}

# set the final prompt
[ $DEBUG_DOTFILES ] && echo "  Setting PS1"
PROMPT='%(!.%F{1}.%F{2})%n%F{8}'
PROMPT=$PROMPT'@'
PROMPT=$PROMPT'${HOSTNAME_COLOR}%M%F{8}'
PROMPT=$PROMPT':'
PROMPT=$PROMPT'${PROMPT_PATH}'
PROMPT=$PROMPT'%k${PGIT}'
PROMPT=$PROMPT' '
PROMPT=$PROMPT'%(?.%F{7}.%F{1})${EXITCODE}%k'
PROMPT=$PROMPT' ${vi_mode}»%b %f${CURSOR_STYLE}'

[ $DEBUG_DOTFILES ] && echo "  Enable gpg-agent"
gpg-connect-agent /bye
export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)

# Apply bundles
if [[ -d "${XDG_CONFIG_DIR:-"${HOME}/.config"}/shell/vendor/zsh" ]]
then
	pushd "${XDG_CONFIG_DIR:-"${HOME}/.config"}/shell/vendor/zsh" > /dev/null

	sources=(
		"feature-syntax-highlighting/zsh-syntax-highlighting.zsh"
	)

	for bundle in "${sources[@]}"
	do
		[[ ! -f "$bundle" ]] && continue

		source "$bundle"
	done

	popd > /dev/null
fi

autoload -U +X bashcompinit && bashcompinit
complete -o nospace -C /usr/bin/terraform terraform