forked from jleclanche/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
380 lines (312 loc) · 9.63 KB
/
.zshrc
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/bin/zsh
# My .zshrc
# Jerome Leclanche <[email protected]>
# https://github.com/jleclanche/dotfiles/blob/master/.zshrc
##
# Somebody set us up the prompt
#
# Let's have some colors first
autoload -U colors && colors
if [[ -e /usr/share/zsh/site-contrib/powerline.zsh ]]; then
# Powerline support is enabled if available, otherwise use a regular PS1
. /usr/share/zsh/site-contrib/powerline.zsh
VIRTUAL_ENV_DISABLE_PROMPT=true
else
# Default colors:
# Cyan for users, red for root, magenta for system users
local _time="%{$fg[yellow]%}[%*]"
local _path="%B%{$fg[green]%}%(8~|...|)%7~"
local _usercol
if [[ $EUID -lt 1000 ]]; then
# red for root, magenta for system users
_usercol="%(!.%{$fg[red]%}.%{$fg[magenta]%})"
else
_usercol="$fg[cyan]"
fi
local _user="%{$_usercol%}%n@%M"
local _prompt="%{$fg[white]%}${(r:$SHLVL*2::%#:)}"
PROMPT="$_time $_user $_path $_prompt%b%f%k "
RPROMPT='${vcs_info_msg_0_}' # git branch
if [[ ! -z "$SSH_CLIENT" ]]; then
RPROMPT="$RPROMPT ⇄" # ssh icon
fi
fi
##
# Environment variables
#
# basedir defaults, in case they're not already set up.
# http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
if [[ -z "$XDG_DATA_HOME" ]]; then
export XDG_DATA_HOME="$HOME/.local/share"
fi
if [[ -z "$XDG_CONFIG_HOME" ]]; then
export XDG_CONFIG_HOME="$HOME/.config"
fi
if [[ -z "$XDG_CACHE_HOME" ]]; then
export XDG_CACHE_HOME="$HOME/.cache"
fi
if [[ -z "$XDG_DATA_DIRS" ]]; then
export XDG_DATA_DIRS="/usr/local/share:/usr/share"
fi
if [[ -z "$XDG_CONFIG_DIRS" ]]; then
export XDG_CONFIG_DIRS="/etc/xdg"
else
export XDG_CONFIG_DIRS="/etc/xdg:$XDG_CONFIG_DIRS"
fi
# add ~/bin to $PATH
path=(~/bin $path)
# add ~/.config/zsh/completion to completion paths
# NOTE: this needs to be a directory with 0755 permissions, otherwise you will
# get "insecure" warnings on shell load!
fpath=("$XDG_CONFIG_HOME/zsh/completion" $fpath)
##
# zsh configuration
#
# Keep 1000 lines of history within the shell and save it to ~/.cache/shell_history
HISTSIZE=1000
SAVEHIST=1000
HISTFILE="$XDG_CACHE_HOME/shell_history"
# shell options
setopt autocd # assume "cd" when a command is a directory
setopt histignorealldups # Substitute commands in the prompt
setopt sharehistory # Share the same history between all shells
setopt promptsubst # required for git plugin
# setopt extendedglob
# Extended glob syntax, eg ^ to negate, <x-y> for range, (foo|bar) etc.
# Backwards-incompatible with bash, so disabled by default.
# Colors!
# 256 color mode
export TERM="xterm-256color"
# Color aliases
if command -V dircolors >/dev/null 2>&1; then
eval "$(dircolors -b)"
# Only alias ls colors if dircolors is installed
alias ls="ls -F --color=auto"
alias dir="dir --color=auto"
alias vdir="vdir --color=auto"
fi
alias grep="grep --color=auto"
alias fgrep="fgrep --color=auto"
alias egrep="egrep --color=auto"
# make less accept color codes and re-output them
alias less="less -R"
##
# Completion system
#
autoload -Uz compinit
compinit
zstyle ":completion:*" auto-description "specify: %d"
zstyle ":completion:*" completer _expand _complete _correct _approximate
zstyle ":completion:*" format "Completing %d"
zstyle ":completion:*" group-name ""
zstyle ":completion:*" menu select=2
zstyle ":completion:*:default" list-colors ${(s.:.)LS_COLORS}
zstyle ":completion:*" list-colors ""
zstyle ":completion:*" list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ":completion:*" matcher-list "" "m:{a-z}={A-Z}" "m:{a-zA-Z}={A-Za-z}" "r:|[._-]=* r:|=* l:|=*"
zstyle ":completion:*" menu select=long
zstyle ":completion:*" select-prompt %SScrolling active: current selection at %p%s
zstyle ":completion:*" verbose true
zstyle ":completion:*:*:kill:*:processes" list-colors "=(#b) #([0-9]#)*=0=01;31"
zstyle ":completion:*:kill:*" command "ps -u $USER -o pid,%cpu,tty,cputime,cmd"
##
# Keybinds
#
# Use emacs-style keybindings
bindkey -e
bindkey "$terminfo[khome]" beginning-of-line # Home
bindkey "$terminfo[kend]" end-of-line # End
bindkey "$terminfo[kich1]" overwrite-mode # Insert
bindkey "$terminfo[kdch1]" delete-char # Delete
bindkey "$terminfo[kcuu1]" up-line-or-history # Up
bindkey "$terminfo[kcud1]" down-line-or-history # Down
bindkey "$terminfo[kcub1]" backward-char # Left
bindkey "$terminfo[kcuf1]" forward-char # Right
# bindkey "$terminfo[kpp]" # PageUp
# bindkey "$terminfo[knp]" # PageDown
# Bind ctrl-left / ctrl-right
bindkey "\e[1;5D" backward-word
bindkey "\e[1;5C" forward-word
# Bind ctrl-backspace to delete word.
# NOTE: This may not work properly in some emulators
# bindkey "^?" backward-delete-word
# Bind shift-tab to backwards-menu
# NOTE this won't work on Konsole if the new tab button is shown
bindkey "\e[Z" reverse-menu-complete
# Make ctrl-e edit the current command line
autoload edit-command-line
zle -N edit-command-line
bindkey "^e" edit-command-line
# Make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
function zle-line-init {
printf "%s" ${terminfo[smkx]}
}
function zle-line-finish {
printf "%s" ${terminfo[rmkx]}
}
zle -N zle-line-init
zle -N zle-line-finish
fi
# typing ... expands to ../.., .... to ../../.., etc.
rationalise-dot() {
if [[ $LBUFFER = *.. ]]; then
LBUFFER+=/..
else
LBUFFER+=.
fi
}
zle -N rationalise-dot
bindkey . rationalise-dot
bindkey -M isearch . self-insert # history search fix
##
# Aliases
#
# some more ls aliases
alias l="ls -CF"
alias ll="ls -l"
alias la="ls -A"
alias lh="ls -lh"
alias lash="ls -lAsh"
alias sl="ls"
# Make unified diff syntax the default
alias diff="diff -u"
# simple webserver
alias mkhttp="python -m http.server"
# json prettify
alias json="python -m json.tool"
# octal+text permissions for files
alias perms="stat -c '%A %a %n'"
# expand sudo aliases
alias sudo="sudo "
##
# Functions
#
# make a backup of a file
# https://github.com/grml/grml-etc-core/blob/master/etc/zsh/zshrc
bk() {
cp -a "$1" "${1}_$(date --iso-8601=seconds)"
}
# display a list of supported colors
function lscolors {
((cols = $COLUMNS - 4))
s=$(printf %${cols}s)
for i in {000..$(tput colors)}; do
echo -e $i $(tput setaf $i; tput setab $i)${s// /=}$(tput op);
done
}
# get the content type of an http resource
function htmime {
if [[ -z $1 ]]; then
print "USAGE: htmime <URL>"
return 1
fi
mime=$(curl -sIX HEAD $1 | sed -nr "s/Content-Type: (.+)/\1/p")
print $mime
}
# open a web browser on google for a query
function google {
xdg-open "https://www.google.com/search?q=`urlencode "${(j: :)@}"`"
}
# print a separator banner, as wide as the terminal
function hr {
print ${(l:COLUMNS::=:)}
}
# launch an app
function launch {
type $1 >/dev/null || { print "$1 not found" && return 1 }
$@ &>/dev/null &|
}
alias launch="launch " # expand aliases
# urlencode text
function urlencode {
print "${${(j: :)@}//(#b)(?)/%$[[##16]##${match[1]}]}"
}
# get public ip
function myip {
local api
case "$1" in
"-4")
api="http://v4.ipv6-test.com/api/myip.php"
;;
"-6")
api="http://v6.ipv6-test.com/api/myip.php"
;;
*)
api="http://ipv6-test.com/api/myip.php"
;;
esac
curl -s "$api"
echo # Newline.
}
# Create short urls via http://goo.gl using curl(1).
# Contributed back to grml zshrc
# API reference: https://code.google.com/apis/urlshortener/
function zurl {
if [[ -z $1 ]]; then
print "USAGE: $0 <URL>"
return 1
fi
local url=$1
local api="https://www.googleapis.com/urlshortener/v1/url"
local data
# Prepend "http://" to given URL where necessary for later output.
if [[ $url != http(s|)://* ]]; then
url="http://$url"
fi
local json="{\"longUrl\": \"$url\"}"
data=$(curl --silent -H "Content-Type: application/json" -d $json $api)
# Match against a regex and print it
if [[ $data =~ '"id": "(http://goo.gl/[[:alnum:]]+)"' ]]; then
print $match
fi
}
##
# Extras
#
# Git plugin
autoload -Uz vcs_info
zstyle ":vcs_info:*" enable git
zstyle ":vcs_info:(git*):*" get-revision true
zstyle ":vcs_info:(git*):*" check-for-changes true
local _branch="%c%u%m %{$fg[green]%}%b%{$reset_color%}"
local _repo="%{$fg[green]%}%r %{$fg[yellow]%}%{$reset_color%}"
local _revision="%{$fg[yellow]%}%.7i%{$reset_color%}"
local _action="%{$fg[red]%}%a%{$reset_color%}"
zstyle ":vcs_info:*" stagedstr "%{$fg[yellow]%}✓%{$reset_color%}"
zstyle ":vcs_info:*" unstagedstr "%{$fg[red]%}✗%{$reset_color%}"
zstyle ":vcs_info:git*" formats "$_branch:$_revision - $_repo"
zstyle ":vcs_info:git*" actionformats "$_branch:$_revision:$_action - $_repo"
zstyle ':vcs_info:git*+set-message:*' hooks git-stash
# Uncomment to enable vcs_info debug mode
# zstyle ':vcs_info:*+*:*' debug true
function +vi-git-stash() {
if [[ -s "${hook_com[base]}/.git/refs/stash" ]]; then
hook_com[misc]="%{$fg_bold[grey]%}~%{$reset_color%}"
fi
}
precmd() {
vcs_info
}
# Syntax highlighting plugin
if [[ -e /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]]; then
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
fi
# virtualenvwrapper support
# Remember to set $PROJECT_HOME in your profile file!
if command -V virtualenvwrapper_lazy.sh >/dev/null 2>&1; then
export WORKON_HOME="$XDG_DATA_HOME/virtualenvs"
source virtualenvwrapper_lazy.sh
# Arch linux uses python3 by default, this is required to make python2-compatible projects
alias mkproject2="mkproject -p /usr/bin/python2"
alias mkvirtualenv2="mkvirtualenv -p /usr/bin/python2"
fi
# User profile
if [[ -e "$XDG_CONFIG_HOME/zsh/profile" ]]; then
source "$XDG_CONFIG_HOME/zsh/profile"
fi
# Check if $LANG is badly set as it causes issues
if [[ $LANG == "C" || $LANG == "" ]]; then
>&2 echo "$fg[red]The \$LANG variable is not set. This can cause a lot of problems.$reset_color"
fi