summaryrefslogtreecommitdiff
path: root/zsh/.zshrc
blob: e54be1cbb2b2560117ad76441e706e75eb8a5466 (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
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
## startx automatically {{{
[[ -z $DISPLAY && -z $SSH_CONNECTION && $XDG_VTNR -eq 1 ]] && exec startx
# }}}
## Bindings {{{

# Load fzf bindings here because we will override ^r
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh

bindkey -v
bindkey -a 'k' history-beginning-search-backward
bindkey -a 'j' history-beginning-search-forward
bindkey '^?' backward-delete-char # backspace
bindkey '^n' history-beginning-search-forward
bindkey '^p' history-beginning-search-backward
bindkey '^a' beginning-of-line
bindkey '^b' backward-char
bindkey '^e' end-of-line
bindkey '^f' forward-char
bindkey '^h' backward-delete-char
bindkey '^k' kill-line
bindkey '^u' kill-whole-line
bindkey '^v' visual-mode
bindkey '^w' backward-kill-word
bindkey '^r' history-incremental-search-backward

autoload -z edit-command-line
zle -N edit-command-line
bindkey -M vicmd '^W' edit-command-line
# }}}
## Options and modules {{{

KEYTIMEOUT=1

# History
HISTFILE=$HOME/.zsh_history
HISTSIZE=1000000
SAVEHIST=$HISTSIZE

setopt append_history
setopt extended_history
setopt hist_ignore_space
setopt inc_append_history
setopt no_share_history

# Completion
zstyle ':completion:*' matcher-list 'm:{a-zA-Z-_}={A-Za-z_-}' 'r:|=*' 'l:|=* r:|=*'

# Kill word
autoload -U select-word-style
select-word-style bash

# Prompt
if [[ -n $SSH_CONNECTION ]]; then
PROMPT='%F{red}[%M]%f %0~ $ '
else
PROMPT='%0~ $ '
fi

if [[ -n $VIFM ]]; then
PROMPT="%F{green}[VIFM]%f $PROMPT"
fi
# }}}
## Plugins {{{
[ ! -d ~/.zsh/zgen ] && git clone https://github.com/tarjoilija/zgen.git ~/.zsh/zgen
source "$HOME/.zsh/zgen/zgen.zsh"

FZF_MARKS_PLUGIN_ZSH=$HOME/dotfiles/plugins/fzf-marks/fzf-marks.plugin.zsh
FZF_MARKS_COMMAND='fzf --height 40% --reverse -e'
[ -f "$FZF_MARKS_PLUGIN_ZSH" ] && source "$FZF_MARKS_PLUGIN_ZSH"

if ! zgen saved; then
    echo "Creating a zgen save"
    zgen load rupa/z
    zgen load zsh-users/zsh-completions src
    zgen load zsh-users/zsh-syntax-highlighting
    zgen load zsh-users/zsh-autosuggestions
    zgen save
fi

bindkey '^y' autosuggest-accept
bindkey '^z' z

ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=6'

# }}}
## Use fzf with z {{{

unalias z 2> /dev/null
z() {
  [ $# -gt 0 ] && _z "$*" && return
  cd "$(_z -l 2>&1 | fzf --height 40% --reverse --inline-info +s --tac --query "$*" | sed 's/^[0-9,.]* *//')"
  zle && zle reset-prompt
}
zle -N z

# }}}
## Colors {{{

[[ -f $HOME/.local/colors.zsh ]] && source $HOME/.local/colors.zsh

colorschemes=$(ls ${HOME}/.Xresources)

function colo {

    COLORSCHEME=$1
    XRESOURCE_FILE=${HOME}/.Xresources/$COLORSCHEME

    # Change colors for current session
    if [[ ! -z "$TMUX" ]]; then
        printf '\x1bPtmux;'
        esc='\x1b\x1b'
    else
        esc='\x1b'
    fi

    /usr/bin/cpp ${XRESOURCE_FILE} | tr -d ' \t' | sed -n \
        -e "s/.*background:/${esc}]11;/p" \
        -e "s/.*foreground:/${esc}]10;/p" \
        -e "s/.*cursorColor:/${esc}]12;/p" \
        -e "s/.*borderColor:/${esc}]708;/p" \
        -e "s/.*color\\([0-9][^:]*\\):/${esc}]4;\\1;/p" | tr \\n \\a

    # Change color for future sessions
    xrdb ${XRESOURCE_FILE}

    echo "export COLORSCHEME=$1" > $HOME/.local/colors.zsh
    source $HOME/.local/colors.zsh
}

# Completion for colorschemes (-M -> Case insensitive)
compctl -k "(${colorschemes})" -M 'm:{a-z}={A-Z}' colo

function show256 {
    for i in {0..255} ; do
        printf "\x1b[48;5;%sm%3d\e[0m " "$i" "$i"
        if (( i == 15 )) || (( i > 15 )) && (( (i-15) % 6 == 0 )); then
            printf "\n";
        fi
    done
}

# }}}
## Aliases {{{

# Directories
alias cdd='cd ~/dotfiles'
alias cdp='cd ~/personal'

# Vim
alias n='nvim'
alias ns='nvim -S Session.vim'
alias v='vim'
alias vs="vim -S Session.vim"

# Git
alias g='git'
alias ga='git add'
alias gc='git commit'
alias gco='git checkout'
alias gd='git diff'
alias gl='git pull'
alias gp='git push'
alias gr='git remote'
alias gra='git remote add'
alias gst='git status'
alias rd='cd $(git rev-parse --show-toplevel)'

# GNU Make
alias mi='make install'
alias mc='make clean'
alias mca='make clean-all'

# Misc
alias a='vifm . .'
alias m='cd ~/.mutt/attachments && mutt && cd -'
alias e='nvim'
alias f='fzm'
alias mux='tmuxinator'
alias email='mbsync -a'
alias renet='systemctl restart NetworkManager.service'

# Applications
alias -s pdf='xdg-open'

# Remove global because it breaks: pacman -D --asdeps grep
alias grep='grep --color=auto --exclude-dir={.git,.hg}'

# Pacman
alias pacup='sudo pacman -Syu'
alias pacin='sudo pacman -S'
alias pacrm='sudo pacman -Rs'
alias pacs='pacman -Ss'

# Systemctl
alias sysnet="systemctl restart NetworkManager.service"
alias sysvpn="systemctl restart openvpn-client@ic.service"

# Directories
alias ..="cd .."
alias ...="cd ../.."
# }}}