about summary refs log tree commit diff
path: root/config/zsh/zshrc.nix
blob: e7553d63d873fb8ef80051645d11264c69246ca6 (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
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
{ stdenv, lib

, coreutils
, zsh-autosuggestions
, zsh-history-substring-search
, zsh-nix-shell
, zsh-syntax-highlighting

 }:

let
  options = {
    # Completion
    always_to_end = true;
    auto_name_dirs = true;
    complete_in_word = true;
    list_packed = true;

    # Expansion and Globbing
    bad_pattern = true;
    brace_ccl = true;
    extended_glob = true;
    equals = true;
    glob_star_short = true;
    magic_equal_subst = true;
    rc_expand_param = true;
    rematch_pcre = true;

    # Input/Output
    correct = true;
    mail_warning = true;
    rc_quotes = true;
    rm_star_wait = true;
    short_loops = true;

    # History
    share_history = true;
    bang_hist = true;
    hist_allow_clobber = true;
    hist_beep = true;
    hist_expire_dups_first = true;
    hist_ignore_dups = true;
    hist_ignore_space = true;
    hist_no_store = true;
    hist_reduce_blanks = true;

    # Prompting
    prompt_sp = true;

    # Scripts and Functions
    c_bases = true;
    c_precedences = true;
    octal_zeroes = true;
  };

  enabledOptions = lib.attrNames (lib.filterAttrs (_: lib.id) options);
  disabledOptions = lib.attrNames (lib.filterAttrs (_: v: !v) options);

in ''

. ${<nixpkgs/nixos/modules/programs/zsh/zinputrc>}

# Disable silver searcher numbers when piped or redirected.
ag() {
    if ! [ -t 1 ]; then
        command ag --no-numbers "$@"
    else
        command ag "$@"
    fi
}

fzf_ctrl_t() {
    if git rev-parse >/dev/null 2>&1; then
        git ls-files -co --exclude-standard
    else
        find . -type f | sed 's/^\.\///g'
    fi
}

HISTFILE="$XDG_DATA_HOME/zsh/history"
REPORTTIME=5
SAVEHIST=9000
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)

${lib.optionalString (enabledOptions == [])
    "setopt ${lib.concatStringsSep " " enabledOptions}"}

${lib.optionalString (disabledOptions == [])
    "unsetopt ${lib.concatStringsSep " " enabledOptions}"}

autoload -Uz colors && colors

zstyle ':completion::complete:*' use-cache on
zstyle ':completion::complete:*' cache-path "$XDG_CACHE_DIR/zsh/zcompcache"
zstyle ':completion:*' auto-description '%d'
zstyle ':completion:*' completer _expand _complete _ignored _match _correct \
                                             _approximate _prefix
zstyle ':completion:*' expand suffix
zstyle ':completion:*' group-name '''
zstyle ':completion:*' insert-unambiguous true
zstyle ':completion:*' list-colors '''
zstyle ':completion:*' list-suffixes true
zstyle ':completion:*' matcher-list \
    'm:{[:lower:]}={[:upper:]}' 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' \
    'r:|[._-/]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' menu select=1
zstyle ':completion:*' original false
zstyle ':completion:*' preserve-prefix '//[^/]##/'
zstyle ':completion:*' select-prompt \
    %SScrolling active: current selection at %p%s
zstyle ':completion:*' squeeze-slashes true
zstyle ':completion:*' use-compctl true
zstyle ':completion:*' verbose true

autoload -U url-quote-magic
zle -N self-insert url-quote-magic

autoload -Uz compinit
compinit -d "$XDG_CACHE_HOME/zsh/zcompdump"

source ${zsh-nix-shell}/share/zsh-nix-shell/nix-shell.plugin.zsh
source ${zsh-syntax-highlighting}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source ${zsh-history-substring-search}/share/zsh-history-substring-search/zsh-history-substring-search.zsh
source ${zsh-autosuggestions}/share/zsh-autosuggestions/zsh-autosuggestions.zsh

zmodload zsh/complist
bindkey -M menuselect "^[[Z" reverse-menu-complete

zmodload zsh/terminfo
bindkey "^[[A" history-substring-search-up
bindkey "^[[B" history-substring-search-down

bindkey "^[[3~" delete-char
bindkey "^[[F" end-of-line
bindkey "^[[H" beginning-of-line
bindkey "^[[1~" beginning-of-line
bindkey "^[[4~" end-of-line
bindkey "^[^[[C" forward-word
bindkey "^[^[[D" backward-word

if [ -n "$TMUX" ]; then
    tmux-page-up() { tmux copy-mode -ue }
    tmux-page-down() { tmux send-keys -X page-down 2>/dev/null }
    zle -N tmux-page-up
    zle -N tmux-page-down
    bindkey "^[[5~" tmux-page-up
    bindkey "^[[6~" tmux-page-down
fi

autoload -U edit-command-line
zle -N edit-command-line
bindkey "^x^e" edit-command-line

if [[ -v TMUX ]]; then
    _tmux_hook() {
        local exit_status="$?"
        tmux set-environment "last_exit_status_$(tmux display-message -p '#D')" "$exit_status"
        tmux refresh-client -S
    }

    typeset -ag precmd_functions;
    if [[ -z $${precmd_functions[(r)_tmux_hook]} ]]; then
        precmd_functions+=_tmux_hook;
    fi
fi

${lib.optionalString stdenv.isDarwin ''
    # Load SSH passphrases from the Keychain.
    # Use an explicit path because upstream OpenSSH
    # doesn't have the keychain functionality.
    (/usr/bin/ssh-add -A &) 2> /dev/null
''}

if command -v fzf-share >/dev/null; then
    source "$(fzf-share)/key-bindings.zsh"
fi

# Aliases
alias beep='printf "\a"'
alias ls=${lib.escapeShellArg (if stdenv.isDarwin then
                                 "/bin/ls -AFh"
                               else
                                 "${coreutils}/bin/ls -AF --si --color=auto")}
alias tree='tree -aRF -I .git'
alias vim=nvim

for command in cargo curl dig find git mutt ri wget; do
    alias "$command=noglob $command"
done

# Prompt
nl=$'\n'
PS1="%F{yellow}%(?..[exit %?]$nl)''${IN_NIX_SHELL+[nix-shell''${NIX_SHELL_PACKAGES:+($NIX_SHELL_PACKAGES)}] }%1(j.[%j job%2(j.s.)] .)%f%# "

''