aboutsummaryrefslogtreecommitdiff
path: root/.zshrc
blob: 857c70edbfff1f58921d4937f0474f04da5fe7e1 (plain) (blame)
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
#!/usr/bin/env zsh


#
# Basics
#

# Load shell-agnostic configs
source ~/.shellrc

# Basic stuff
autoload -U colors && colors
setopt IGNORE_EOF  # Don't quit on Ctrl-d


#
# Prompt
#

setopt prompt_subst

if [ -z "$_IS_SSH" ]; then
	_PROMPT_PRIMARY_COLOUR="magenta"
	_PROMPT_SECONDARY_COLOUR="cyan"
else
	_PROMPT_PRIMARY_COLOUR="cyan"
	_PROMPT_SECONDARY_COLOUR="magenta"
fi
[ -n "$_IS_ROOT" ] && _PROMPT_PRIMARY_COLOUR="red"

PROMPT='%B%F{$_PROMPT_PRIMARY_COLOUR}%n${_PROMPT_AT_COLOUR}@%F{$_PROMPT_PRIMARY_COLOUR}%m%f%b in %B%F{$_PROMPT_SECONDARY_COLOUR}%~%f%b$(git_status)$(lf_status)
%f%b%(?:$ :%F{red}$ )%f'

git_status() {
	ref="$(git symbolic-ref --quiet --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)"
	if [ $? -eq 0 ]; then
		printf " on %s" "%B%F{$_PROMPT_PRIMARY_COLOUR}${ref}%b%f"
		[[ $(git status --short 2>/dev/null | wc -l) -ne 0 ]] && printf "%s" "%F{yellow}*%f"
	fi
}

lf_status() {
	[ -n "$LF_LEVEL" ] && printf " %s" "%B%F{243}(lf$LF_LEVEL)%b%f";
}


#
# History
#

HISTSIZE=1000000
SAVEHIST=1000000
HISTFILE="${XDG_STATE_HOME:-$HOME/.local/state}/zsh/zsh_history"
[ ! -d "$(dirname "$HISTFILE")" ] && mkdir -p "$(dirname "$HISTFILE")"
setopt histignoredups
setopt hist_ignore_space


#
# Tab completion
#

autoload -U compinit
zstyle ':completion:*' menu select
zmodload zsh/complist
[ ! -d "${XDG_CACHE_HOME:-$HOME/.cache}/zsh" ] && mkdir -p "${XDG_CACHE_HOME:-$HOME/.cache}/zsh" 2>/dev/null
compinit -d "${XDG_CACHE_HOME:-$HOME/.cache}/zsh/zcompdump"
_comp_options+=(globdots)             # Include hidden files
zstyle ':completion:*' rehash true    # Look for new completions
bindkey '^[[Z' reverse-menu-complete  # Shift-Tab
setopt completealiases


#
# Bindings
#

# Vim-style
bindkey -v
bindkey -M viins '^?' backward-delete-char
bindkey -M viins '^H' backward-delete-char

# Other bindings
bindkey '^R' history-incremental-search-backward
autoload edit-command-line; zle -N edit-command-line  # Edit line in $EDITOR with C-e:
bindkey '^e' edit-command-line


#
# Window title
#

xterm_title_precmd()  { print -Pn -- '\e]2;%n@%m %~\a' }
xterm_title_preexec() { print -Pn -- '\e]2;%n@%m %~ %# ' && print -n -- "${(q)1}\a" }
autoload -Uz add-zsh-hook
add-zsh-hook -Uz precmd xterm_title_precmd
add-zsh-hook -Uz preexec xterm_title_preexec


#
# Multi-dot cd (...)
#

expand-multiple-dots() {
    local MATCH
    if [[ $LBUFFER =~ '(^| )\.\.\.+' ]]; then
        LBUFFER=$LBUFFER:fs%\.\.\.%../..%
    fi
}
expand-multiple-dots-then-expand-or-complete() {
    zle expand-multiple-dots
    zle expand-or-complete
}
expand-multiple-dots-then-accept-line() {
    zle expand-multiple-dots
    zle accept-line
}
zle -N expand-multiple-dots
zle -N expand-multiple-dots-then-expand-or-complete
zle -N expand-multiple-dots-then-accept-line
bindkey '^I' expand-multiple-dots-then-expand-or-complete
bindkey '^M' expand-multiple-dots-then-accept-line