aboutsummaryrefslogtreecommitdiff
path: root/config/zsh/.zshrc
blob: 3b091f47724bfa5aec34d563b4ea7141e7dc35ac (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
#!/bin/zsh

# load aliases
# shellcheck source=/dev/null
. "$XDG_CONFIG_HOME/zsh/aliases.zsh"

# prompt
case $(tty) in
    /dev/tty[1-9])
        # %~ path ('~' if $HOME)
        # %B/%b start/stop bold
        # %F/%f start/stop color
        # shellcheck disable=SC2039,SC3003
        NEWLINE=$'\n'
        export PROMPT="${NEWLINE}%B%F{blue}%~%f${NEWLINE}%F{red}> %f%b"
        ;;
    *)
        # pure prompt
        export FPATH="$FPATH:$XDG_DATA_HOME/zsh/pure"
        export ZSH_THEME='pure'
        autoload -U promptinit
        promptinit
        prompt pure
        ;;
esac

# auto complete
autoload -U compinit
zstyle ':completion:*' menu select                          # menu like
zstyle ':completion:*' matcher-list '' \
    'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=*' 'r:|=* l:|=* r:|=*'  # case insensitive
zmodload zsh/complist
COMPFILE="$XDG_CACHE_HOME/zsh/zcompdup-$ZSH_VERSION"
mkdir -p "$(dirname "$COMPFILE")"
compinit -d "$COMPFILE"
# _comp_options+=(globdots)

# vim keybindings in tab completion menu (https://www.youtube.com/watch?v=eLEo4OQ-cuQ)
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
bindkey '^r' history-incremental-search-backward

# vim keybindings in prompt
bindkey -v
export KEYTIMEOUT=1

# setopt pushd_ignore_dups
# setopt extendedglob
setopt auto_cd          # cd without `cd` command
setopt list_rows_first  # cycle through row first in menu
setopt share_history    # share history between shells
setopt extended_history # command timestamp in history
setopt hist_ignore_dups # ignore duplicate entry in history

export HISTSIZE=10000
export SAVEHIST=5000

# executed when changing directory
chpwd() {
    # ls on cd if not too much files
    content="$(find . -maxdepth 1 | wc -l)"
    ([ "$content" -lt 20 ] && ls -l) ||
        echo "$(pwd) contains $content entries"
    [ "$(uname)" = 'Linux' ] && [ "$(stat -c "%U" .)" = "$USER" ] && touch .  # to sort by last cd

    # change conda env if name of the directory is the name of an env
    # [ ! -d "$PWD/.git" ] && return
    # name="$(basename "$PWD")"
    # [ "$name" = $CONDA_DEFAULT_ENV ] && return
    # [ ! -e "$HOME/conda_envs" ] && conda env list > "$HOME/conda_envs"
    # < "$HOME/conda_envs" \
    #     cut -d ' ' -f 1 |
    #     sed -e '/^#/d' -e '/^$/d' -e '/^base$/d' |
    #     grep -q "$name" &&
    #         conda activate "$name"
}

# https://wiki.archlinux.org/title/Zsh#Shortcut_to_exit_shell_on_partial_command_line
exit_zsh() {
    exit
}
zle -N exit_zsh
bindkey '^D' exit_zsh

# shellcheck disable=SC2034,SC2039,SC3030
fignore=(.o .hi) # ignore extensions in autocomplete

# set tab to 4 spaces
tabs 4

GPG_TTY=$(tty)  # fixing gpg fatal error about tty
export GPG_TTY

export BAT_THEME='gruvbox-dark'  # gruvbox theme for bat (fancy cat)

# pluggins
# shellcheck source=/dev/null
. "$XDG_DATA_HOME/zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"  # prompt syntax highlight

export YSU_MESSAGE_POSITION="after"                                 # you-should-use message after command output
. "$XDG_DATA_HOME/zsh/zsh-you-should-use/you-should-use.plugin.zsh" # alias reminder

# install pkgfile package on Arch Linux
# run `pkgfile --update`
if [ "$(uname)" = 'Linux' ] && grep -q Arch /etc/os-release 2> /dev/null
then
    .  /usr/share/doc/pkgfile/command-not-found.zsh
fi
. "/home/charles/.local/share/cargo/env"