-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzshrc
153 lines (120 loc) · 3.95 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
# Colors
autoload -U colors && colors
# Cache directory
mkdir -p ~/.cache/zsh
# Completion
setopt COMPLETE_ALIASES
setopt NO_LIST_AMBIGUOUS
# Use menu for selection
zstyle ':completion:*' menu select
# Case insensitive
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
# Cache systemd unit files completion
zstyle ':completion:*:*:systemctl:*' cache-path ~/.cache/zsh/systemd
zstyle ':completion:*:*:systemctl:*' use-cache true
# Enable caching for all completions
zstyle ':completion::complete:*' use-cache true
zstyle ':completion::complete:*' cache-path ~/.cache/zsh
# Enable sudo completions
zstyle ':completion::complete:*' gain-privileges 1
# Optimize systemd completion matching
zstyle ':completion:*:*:systemctl:*' matcher-list 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*:*:systemctl:*' group-order services paths systemd-units
# Load completion menu
zmodload zsh/complist
autoload -Uz compinit
compinit -d ~/.cache/zsh/zcompdump
# Report command running time if it is more than 3 seconds
REPORTTIME=3
# Keep a lot of history
HISTFILE=~/.zhistory
HISTSIZE=5000
SAVEHIST=5000
# Do not keep duplicate commands in history
setopt HIST_IGNORE_ALL_DUPS
# Do not remember commands that start with a whitespace
setopt HIST_IGNORE_SPACE
# Bind jk keys to escape insert mode
bindkey -v
bindkey -M viins 'jk' vi-cmd-mode
bindkey -M menuselect '^h' vi-backward-char
# Use vim keys in tab complete menu:
bindkey -M menuselect 'h' vi-backward-char
bindkey -M menuselect 'k' vi-up-line-or-history
bindkey -M menuselect 'l' vi-forward-char
bindkey -M menuselect 'j' vi-down-line-or-history
bindkey -v '^?' backward-delete-char
# Edit line in vim with ctrl-e:
autoload edit-command-line; zle -N edit-command-line
bindkey '^e' edit-command-line
# Ctrl-R
bindkey '^R' history-incremental-search-backward
# Search through previous commands
bindkey "\e[A" up-line-or-beginning-search
bindkey "\e[B" down-line-or-beginning-search
autoload up-line-or-beginning-search
autoload down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
# Don't share history
setopt append_history no_inc_append_history no_share_history
# Ctrl-z to go back to vi after Ctrl-z
fancy-ctrl-z () {
if [[ $#BUFFER -eq 0 ]]; then
BUFFER="fg"
zle accept-line
else
zle push-input
zle clear-screen
fi
}
zle -N fancy-ctrl-z
bindkey '^Z' fancy-ctrl-z
function reload_zsh {
. ~/.zshrc
rehash
}
_git_prompt() {
git_cmd=$(git status --porcelain --branch 2>/dev/null)
[ "$git_cmd" ] || return 0
local branch
case "$git_cmd" in
*No\ commits\ yet*) branch="[no commits yet]" ;;
*no\ branch*) branch=$(git describe --tags --always) ;;
*) branch=$(git rev-parse --abbrev-ref HEAD) ;;
esac
local output="%F{blue}$branch%f"
case "$git_cmd" in *M\ \ *) output="$output %F{yellow}✗%f" ;; esac
case "$git_cmd" in *R\ \ *) output="$output %F{yellow}→%f" ;; esac
case "$git_cmd" in *\ M\ *) output="$output %F{green}✗%f" ;; esac
case "$git_cmd" in *\?\?*) output="$output %F{blue}…%f" ;; esac
case "$git_cmd" in *ahead*) output="$output %F{green}↑%f" ;; esac
case "$git_cmd" in *behind*) output="$output %F{green}↓%f" ;; esac
echo "$output"
}
_setup_prompt() {
local _vim_mode='%F{red}%(!.#.❯)%f'
[ "x$KEYMAP" = "xvicmd" ] && _vim_mode='%(!.%F{green}#.%F{green}❮)%f'
local _ssh_agent=
ssh-add -L >/dev/null 2>/dev/null && _ssh_agent='%F{green}•%f '
local _remote_connection=
[ "$SSH_CONNECTION" ] && _remote_connection='%(!.%F{red}root.%F{green}%n)@%m%f '
local _bg_jobs=
[ "$(jobs | wc -l)" != "0" ] && _bg_jobs='%F{yellow}%f '
PS1="%B${_ssh_agent}${_remote_connection}%F{blue}%~%f ${_bg_jobs}%F{red}${_vim_mode} %f%b"
# Git prompt
RPROMPT="%B$(_git_prompt)%b"
}
_setup_prompt
# Vi mode
zle-keymap-select () {
_setup_prompt
zle reset-prompt
}
zle -N zle-keymap-select
zle-line-init () {
zle -K viins
}
zle -N zle-line-init
# Import functions
[ -f ~/.shell ] && . ~/.shell