Skip to content

Instantly share code, notes, and snippets.

@sharethewisdom
Created October 18, 2019 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharethewisdom/78b5cd2762a263cad0531d60662f6f8c to your computer and use it in GitHub Desktop.
Save sharethewisdom/78b5cd2762a263cad0531d60662f6f8c to your computer and use it in GitHub Desktop.
Site-wide zsh configuration with autocompletion (predict completer and short options tag order).
# vim: set sw=2 sts=2 :
# /etc/zsh/zshrc
[[ -o nointeractive ]] && return
unlimit
limit stack 8192
limit core 0
limit -s
export ZBEEP='\e[?5h\e[?5l'
export DIRSTACKSIZE=1000
export HISTSIZE=7000
export SAVEHIST=7000
export KEYTIMEOUT=1
export ZLE_RPROMPT_INDENT=1
export LISTPROMPT=''
export CORRECT_IGNORE='_*'
export HISTORY_IGNORE='((vim|cd|rm|cat|git|man|mpv|info|llpp|less|mkdir|chmod|run-help) *|*http://*|*https://*|[[:IFS:]./]*)'
export FIGNORE="aux:log:toc:bak:auxlock:fls:fmt:bcf:lof:out:run.xml:pyg:bbl:blg:synctex.gz:fdb_latexmk:dvi:idx:ilg:ind:xdv"
unsetopt beep # turn off beep
unsetopt bg_nice # lower priority of bg processes
unsetopt extended_history
setopt menu_complete
setopt interactive_comments
# setopt ignore_close_braces
setopt complete_aliases
setopt list_packed
setopt auto_pushd
setopt pushd_ignore_dups
setopt pushd_silent
setopt pushd_to_home
setopt hist_ignore_all_dups # ignore duplicate entries of previous events
setopt hist_ignore_space # prefix command with a space to skip it's recording
setopt hist_reduce_blanks # Remove extra blanks from each command added to history
setopt hist_verify # Don't execute immediately upon history expansion
setopt inc_append_history # Write to history file immediately, not when shell quits
zshaddhistory() {
emulate -L zsh
setopt extendedglob
[[ $1 != ${~HISTORY_IGNORE} ]]
}
bindkey -v
function _systemctl-alternate() {
emulate -L zsh
if (( ${#BUFFER} == 0 )); then
HISTNO=${${(k)history[(R)systemctl *]}[1]}
else
local alter words i j
alter=(cat edit start status 'show -p "Wants"' restart stop enable disable)
words=("${(z)BUFFER}"); i=1; j=0
[[ $words[$i] = sudo ]] && (( i++ ))
[[ $words[$i] = systemctl ]] && (( i++ )) || return 0
while [[ $words[$i] = -* ]]; do (( i++ )) done
j=${alter[(ie)$words[$i]]}
if [[ $j -lt ${#alter} ]]; then
(( j++ ))
else
j=1
fi
BUFFER="$words[1,((i-1))] $alter[$j] $words[((i+1)),$#words]"
fi
CURSOR=${#BUFFER}
}
zle -N systemctl-alternate _systemctl-alternate
bindkey -M viins -- '\es' systemctl-alternate
bindkey -M vicmd -- '\es' systemctl-alternate
function digest() {
openssl dgst -binary -sha256 $1 | openssl base64
}
function ctrlz() {
if [[ $#BUFFER == 0 ]]; then
fg >/dev/null 2>&1 && zle redisplay
else
zle push-input
fi
}
zle -N ctrlz
function _redir() {
integer l=$#LBUFFER
zle .self-insert
(( $l >= $#LBUFFER )) && LBUFFER[-1]=" $LBUFFER[-1]"
}
zle -N redir _redir
for op in \| \< \> \& ; do
bindkey -M viins -- "$op" redir
done
alias ps='ps --ho-headers'
alias aw='WIKI_LANG="nl" wiki-search'
alias xclip='xclip -r'
alias dmerr='dmesg -H -L -l err,warn'
alias jn='journalctl -p5 -e -n40 -o cat -S yesterday'
alias rm='systemd-inhibit rm --preserve-root -vi --'
alias chmod='nocorrect chmod --preserve-root -v'
alias chown='nocorrect chown --preserve-root -v'
alias ffmpeg='ffmpeg -hide_banner'
alias gpgtrust='echo -e "trust\n5\ny\n" | gpg --command-fd 0 --edit-key'
alias f='find . -maxdepth 6 -type f -name'
alias x='xclip -o -se c'
if [ $commands[pinfo] ]; then
alias info="pinfo -a"
else
alias info="info --vi-keys --init-file $XDG_CONFIG_HOME/info/info.conf"
fi
# "do the right extraction"
if [ $commands[dtrx] ]; then
function () {
local ext
for ext in {zip,tar,tar.gz,tar.bz2,xz,rar}; do
alias -s "$ext"=dtrx
done
}
fi
bindkey '^Z' ctrlz
unalias run-help
autoload -U run-help
bindkey -M vicmd -- '' run-help
bindkey -M viins -- '' run-help
bindkey -M visual -- '' run-help
bindkey -M vicmd -- '' which-command
bindkey -M visual -- '' which-command
bindkey -M viins -- '[3~' delete-char
bindkey -M viins -- '' vi-backward-word
bindkey -M viins -- '' vi-forward-word
bindkey -M viins -- ' ' magic-space
alias ..='cd ..'
function _dotdot() {
if [[ $LBUFFER = *.. ]]; then
LBUFFER+=/..
else
LBUFFER+=.
fi
}
zle -N dotdot _dotdot
bindkey -M viins -- . dotdot
bindkey -M viins -s '\el' ' 2>&1|less^M'
# ci"
autoload -U select-quoted
zle -N select-quoted
for m in visual viopp; do
for c in {a,i}{\',\",\`}; do
bindkey -M $m $c select-quoted
done
done
# ci{, ci(, di{ etc..
autoload -U select-bracketed
zle -N select-bracketed
for m in visual viopp; do
for c in {a,i}${(s..)^:-'()[]{}<>bB'}; do
bindkey -M $m $c select-bracketed
done
done
autoload -U edit-command-line
zle -N edit-command-line
bindkey -M viins -- '\ee' edit-command-line
[[ $COLORTERM = *(24bit|truecolor)* ]] || zmodload zsh/nearcolor
zmodload zsh/complist
bindkey -M menuselect '^M' send-break
autoload -U zstyle+
autoload -U colors && colors
autoload -U compinit && compinit
compdef _find f
compdef _gpg gpgtrust
autoload -U promptinit && promptinit
zstyle ':zle:*kill*:' word-style space
zstyle ':mime:*' tty-browsers w3m
# :completion:function:completer:command:argument:tag
#zstyle ':completion:*:complete:-command-:*:options'
zstyle ':completion:*:complete:git:*' force-list always
zstyle ':completion:*:complete:git:*' tag-order \
'main-porcelain-commands:-freq:freqently\ used\ commands aliases'
zstyle ':completion:*:complete:git:*:main-porcelain-commands-freq' \
ignored-patterns '(am|archive|bisect|bundle|clean|cherry-pick|citool|fetch|gc|gui|ls-files|log|notes|range-diff|show|stash|status|submodule|subtree|worktree)*'
zstyle ':completion:*' tag-order \
'options:-single-letter:single\ letter\ options *' \
'options:-short:short\ options *' \
'options:-long:long\ options *'
zstyle ':completion:*:options-single-letter' ignored-patterns '???*'
zstyle ':completion:*:options-single-letter' prefix-needed true
zstyle ':completion:*:options-short' ignored-patterns '--*' '[-+]?'
zstyle ':completion:*:options-short' prefix-needed true
zstyle ':completion:*:options-long' ignored-patterns '[-+](|-|[^-]*)'
zstyle ':completion:*:options-long' prefix-needed true
#zstyle ':completion:*:complete:systemctl:*:commands'
#zstyle ':completion:*:complete:pacman:*:_files'
zstyle ':completion:*:complete:kill:*:processes' force-list always
zstyle ':completion:*:complete:kill:*:processes' list-colors '=(#b) #([0-9]#)*=36=32'
zstyle ':completion:*:complete:mpv:*:*' tag-order '!urls'
zstyle ':completion:*:complete:mpv:*:*' file-patterns '{*.mp4,*.avi}:video-files' '*(-/):directories'
zstyle ':completion:*:complete:mpv:*:*' file-sort time
zstyle ':completion:*' completer _complete _correct _approximate
# zle -C complete expand-or-complete completer
zstyle ':completion:*:*:-command-:*:*' group-order builtins functions commands
zstyle ':completion:*:*:-command-:*:*' rehash yes
zstyle ':completion:*:*:-command-:*:*' verbose yes
zstyle ':completion:*:*:-command-:*:*' menu yes=2 select=long-list
zstyle ':completion:*:*:-command-:*:*' ignore-parents parent pwd
zstyle ':completion:*:*:-command-:*:*' squeeze-slashes true
zstyle ':completion:*:*:-command-:*:*' file-sort time
zstyle ':completion:*:*:-redirect-,2>,*:*' file-patterns '*.log'
# predict-on widget
zstyle ':completion:predict:*' completer _complete
zstyle ':completion:predict:*' verbose no
zstyle ':completion:predict:*' rehash no
setopt prompt_subst
autoload -Uz colors && colors
fg_alert=%{$'\e[38;5;161m'%}
at_normal=%{$'\e[0m'%}
function virtualenv_info {
[ $VIRTUAL_ENV ] && echo "[ %{$fg[magenta]%}virt:%{$reset_color%} "$(basename $VIRTUAL_ENV)" ] "
}
PROMPT='%{$fg[blue]%}%n%{$reset_color%}@%{$fg[yellow]%}%m%{$reset_color%} %{$fg_bold[blue]%}%(4~|%4>>%-2~%<<%(5~=%{$reset_color%}%F{white}>%{$reset_color%}%{$fg_bold[blue]%}=%{$reset_color%}%{$fg_bold[blue]%}/)%2>/>%2d%<<%20>>%1d%<<|%{$fg_bold[blue]%}%~%{$reset_color%})%{$reset_color%}%(24l.
. ) $(virtualenv_info)%# '
RPROMPT='%{%}%(1j.%j bg.)%{%} [%{$fg_bold[blue]%}%?%{$reset_color%}]'
autoload -U promptinit && promptinit
function _update-post-highlight() {
if (( $#POSTDISPLAY > 0 )); then
region_highlight=("$#BUFFER $(($#BUFFER + $#POSTDISPLAY)) fg=blue")
fi
}
function _rbuffer-to-postdisplay() {
if [ -n $RBUFFER ] && (( $#RBUFFER > 0 )); then
POSTDISPLAY="$RBUFFER"
RBUFFER=""
fi
_update-post-highlight
}
function _postdisplay-to-rbuffer() {
if (( $#POSTDISPLAY > 0 )); then
RBUFFER="$POSTDISPLAY"
POSTDISPLAY=""
fi
_update-post-highlight
}
function _postdisplay-word-to-lbuffer() {
if (( $#POSTDISPLAY > 0 )); then
local words=("${(z)POSTDISPLAY}")
if [[ -n $words ]]; then
LBUFFER+="$words[1]"
[[ "${words[1][-1]}" != [/\=.] ]] && LBUFFER+=" "
POSTDISPLAY="$words[2,-1]"
fi
fi
region_highlight=("$#BUFFER $(($#BUFFER + $#POSTDISPLAY)) fg=blue")
#_update-post-highlight
}
function _magic-space-consume() {
let "oldpos = CURSOR"
let "diff = 1"
zle .magic-space
if (( oldpos < CURSOR )); then
(( diff += CURSOR - oldpos ))
fi
POSTDISPLAY="$POSTDISPLAY[$diff,-1]"
_update-post-highlight
}
function _delete-char() {
unset POSTDISPLAY
zle .delete-char
}
function _self-insert-menu() {
unset POSTDISPLAY
zle .self-insert
if [[ $LBUFFER[-2] == [[:blank:]] ]]; then
zle list-choices
fi
}
function _self-insert-tilde() {
unset POSTDISPLAY
zle .self-insert
if [[ $LBUFFER[-2] == [[:blank:]] ]]; then
LBUFFER+='/'
zle list-choices
fi
}
function _self-insert-consume() {
(( $#RBUFFER < 1 )) && unset POSTDISPLAY
if (( $#RBUFFER < 3 )) && [ $functions[insert-and-predict] ]; then
insert-and-predict
_rbuffer-to-postdisplay
else
zle .self-insert
fi
POSTDISPLAY="$POSTDISPLAY[1,-1]"
_update-post-highlight
}
function _backdel-clear() {
# todo: reconsider what I want here
[[ $LBUFFER[-1] == [[:blank:]] ]] && unset POSTDISPLAY
if [ -z $RBUFFER ] || (( $#RBUFFER < 0 )); then
POSTDISPLAY=${LBUFFER[-1]}$POSTDISPLAY
fi
zle .vi-backward-delete-char
_update-post-highlight
}
function _forward-word() {
if [ -n $RBUFFER ] && (( $#RBUFFER > 1 )); then
zle .vi-forward-word
elif (( $#POSTDISPLAY > 0 )); then
_postdisplay-word-to-lbuffer \
&& _update-post-highlight
else
zle .vi-forward-word
fi
}
function _accept-line() {
unset POSTDISPLAY
zle .accept-line
}
function _expand-or-complete-or-list-files() {
if [[ $#BUFFER == 0 ]]; then
BUFFER="ls -- "
CURSOR=6
zle list-choices
zle backward-kill-word
else
#(( $#POSTDISPLAY > 0 ))
zle expand-or-complete
_update-post-highlight
fi
}
function _clear-and-push() {
unset POSTDISPLAY
zle .clear-screen
[[ $#BUFFER != 0 ]] && zle push-line-or-edit
}
function _start_predict() {
add-zsh-hook -d precmd _start_predict
autoload -Uz predict-on; predict-on
# todo: when accepting completion, the match is not put in the buffer by pressing Enter
zle -N accept-line _accept-line
zle -N expand-or-complete-or-list-files _expand-or-complete-or-list-files
bindkey -M viins -- '^I' expand-or-complete-or-list-files
zle -N delete-char-or-list delete-no-predict
zle -N highlight-post _update-post-highlight
zle -N magic-space _magic-space-consume
zle -N self-insert _self-insert-consume
zle -N delete-char _delete-char
zle -N self-insert-menu _self-insert-menu
bindkey -M viins -- '-' self-insert-menu
zle -N self-insert-tilde _self-insert-tilde
bindkey -M viins -- '~' self-insert-tilde
zle -N clear-screen _clear-and-push
zle -N vi-backward-delete-char _backdel-clear
#zle -N vi-forward-char
zle -N vi-end-of-line _postdisplay-to-rbuffer
#zle -N vi-add-eol
zle -N vi-forward-word _forward-word
#zle -N vi-forward-word-end
#zle -N vi-forward-blank-word
#zle -N vi-forward-blank-word-end
}
add-zsh-hook precmd _start_predict
# ----------
function zle-keymap-select {
if [[ ${KEYMAP} == vicmd ]] ||
[[ $1 = 'block' ]]; then
echo -ne '\e[1 q'
elif [[ ${KEYMAP} == main ]] ||
[[ ${KEYMAP} == viins ]] ||
[[ ${KEYMAP} = '' ]] ||
[[ $1 = 'beam' ]]; then
echo -ne '\e[5 q'
fi
}
zle -N zle-keymap-select
zle-line-init() {
zle -K viins # initiate `vi insert` as keymap (can be removed if `bindkey -V` has been set elsewhere)
echo -ne "\e[5 q"
}
zle -N zle-line-init
echo -ne '\e[5 q' # Use beam shape cursor on startup.
preexec() { echo -ne '\e[5 q' ;} # Use beam shape cursor for each new prompt.
# end of /etc/zsh/zshrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment