dotfiles/zsh/.config/zsh/.zshrc
Marty Oehme 5dde9db2e4
zsh: Update fzf-tab
Make use of colorful action groups and query input option.

By default, will group the different completion actions (if they have
been set up for the respective zsh completion) as different color sets.

When entering an input and nothing fits (or something else than
intended), it is now possible to simply hit alt+enter instead of just
enter to add whatever you typed into the zle instead of whatever fzf
provided you with (or nothing at all, if it didn't).
2020-10-08 10:28:56 +02:00

196 lines
7.3 KiB
Bash

#!/usr/bin/env zsh
#
CONFDIR="${XDG_CONFIG_HOME:-$HOME/.config}"
ZSHCONFDIR="$CONFDIR/zsh"
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.config/zsh/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block, everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# Set completion style
# The following lines were added by compinstall
zstyle ':completion:*' completer _complete _ignored _approximate
zstyle ':completion:*' matcher-list '' '+m:{[:lower:]}={[:upper:]} r:|[._-]=** r:|=**'
zstyle ':completion:*' menu select=1
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle :compinstall filename "$ZSHCONFDIR/.zshrc"
# End of lines added by compinstall
# load plugins
PLUG_FOLDER="/usr/share/zsh/plugins"
source /usr/share/zsh-theme-powerlevel10k/powerlevel10k.zsh-theme
source /usr/share/oh-my-zsh/plugins/colored-man-pages/colored-man-pages.plugin.zsh
source /usr/share/oh-my-zsh/plugins/command-not-found/command-not-found.plugin.zsh
source /usr/share/nvm/init-nvm.sh
[ -e $PLUG_FOLDER/fzf-tab/fzf-tab.plugin.zsh ] && source $PLUG_FOLDER/fzf-tab/fzf-tab.plugin.zsh
# these need to be sourced after fzf-tab
[ -e $PLUG_FOLDER/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh ] && source $PLUG_FOLDER/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh
[ -e $PLUG_FOLDER/alias-tips/alias-tips.plugin.zsh ] && source $PLUG_FOLDER/alias-tips/alias-tips.plugin.zsh
[ -e $PLUG_FOLDER/zsh-autosuggestions/zsh-autosuggestions.plugin.zsh ] && source $PLUG_FOLDER/zsh-autosuggestions/zsh-autosuggestions.plugin.zsh
unset PLUG_FOLDER
# simple fzf-tab settings
FZF_TAB_COMMAND=(
fzf
--ansi # Enable ANSI color support, necessary for showing groups
--expect='$continuous_trigger,$print_query' # For continuous completion and print query
'--color=hl:$(( $#headers == 0 ? 108 : 255 ))'
--nth=2,3 --delimiter='\x00' # Don't search prefix
--layout=reverse --height='${FZF_TMUX_HEIGHT:=75%}'
--tiebreak=begin -m --bind=tab:down,btab:up,change:top,ctrl-space:toggle --cycle
'--query=$query' # $query will be expanded to query string at runtime.
'--header-lines=$#headers' # $#headers will be expanded to lines of headers at runtime
--print-query
)
zstyle ':fzf-tab:*' command $FZF_TAB_COMMAND
# format colorful groups for different completion actions
zstyle ':completion:*:descriptions' format '[%d]'
# use input as query string when completing zlua
zstyle ':fzf-tab:complete:_zlua:*' query-string input
# (experimental, may change in the future)
# some boilerplate code to define the variable `extract` which will be used later
# please remember to copy them
local extract="
# trim input(what you select)
local in=\${\${\"\$(<{f})\"%\$'\0'*}#*\$'\0'}
# get ctxt for current completion(some thing before or after the current word)
local -A ctxt=(\"\${(@ps:\2:)CTXT}\")
# real path
local realpath=\${ctxt[IPREFIX]}\${ctxt[hpre]}\$in
realpath=\${(Qe)~realpath}
"
# give a preview of commandline arguments when completing `kill`
zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm,cmd -w -w"
zstyle ':fzf-tab:complete:kill:argument-rest' extra-opts --preview=$extract'ps --pid=$in[(w)1] -o cmd --no-headers -w -w' --preview-window=down:3:wrap
# give a preview of directory by exa when completing cd
zstyle ':fzf-tab:complete:cd:*' extra-opts --preview=$extract'exa -1 --color=always $realpath'
# load completion, extended zsh moving syntax, zle edit in vim (or other $EDITOR) possibility
autoload -Uz compinit zmv edit-command-line
compinit
# shellcheck source=alias
[ -f "$CONFDIR/sh/alias" ] && source "$CONFDIR/sh/alias"
# load additional aliases
if [ -d "$CONFDIR/sh/alias.d" ]; then
for _alias in "$CONFDIR/sh/alias.d"/*.sh; do
. "$_alias"
done
unset _alias
fi
if [ -d "$ZSHCONFDIR/alias.d" ]; then
for _alias in "$ZSHCONFDIR/alias.d"/*.sh; do
. "$_alias"
done
unset _alias
fi
# Correct spelling for commands
setopt correct
# turn off the infernal correctall for filenames
unsetopt correctall
# Enable command auto-correction.
ENABLE_CORRECTION="true"
# Speed up autocomplete, force prefix mapping
zstyle ':completion:*' accept-exact '*(N)'
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path "${XDG_CACHE_HOME:-$HOME/.cache}/zshcompcache"
### history
## Set ZSH History defaults
setopt append_history
setopt extended_history
setopt hist_expire_dups_first
setopt hist_ignore_all_dups
setopt hist_ignore_dups
setopt hist_ignore_space
setopt hist_reduce_blanks
setopt hist_save_no_dups
setopt hist_verify
# Share your history across all your terminal windows
setopt share_history
setopt pushd_ignore_dups
#setopt pushd_silent
export HISTSIZE=100000
export SAVEHIST=100000
export HISTFILE="$XDG_DATA_HOME/zsh_history"
export HISTIGNORE="ls:cd:cd -:pwd:exit:date:* --help"
## Set ZSH History aliases
# Show the top 5 commands used in recent history
history_top() {
history | awk "{a[\$2]++} END{for(i in a){printf \"%5d\t%s\n\",a[i],i}}" | sort -rn | head
}
# Display timestamped recent command history
alias history="fc -l -d -D"
### Glob Alias expansion
# Expand aliases inline - see http://blog.patshead.com/2012/11/automatically-expaning-zsh-global-aliases---simplified.html
globalias() {
if [[ $LBUFFER =~ [A-Z0-9]+$ ]]; then
zle _expand_alias
zle expand-word
fi
zle self-insert
}
zle -N globalias
bindkey " " globalias
bindkey "^ " magic-space # control-space to bypass completion
bindkey -M isearch " " magic-space # normal space during searches
# VIM MODE for the shell
# enable vim mode on pressing escape
bindkey -v
# remove the delay for switching modes
export KEYTIMEOUT=1
# space puts a space, even in cmd mode
bindkey -a ' ' magic-space
# always allow backspace/delete to remove letters
bindkey '^?' backward-delete-char
bindkey -a '^?' backward-delete-char
bindkey '^h' backward-delete-char
bindkey -a '^h' backward-delete-char
# and forward with delete
bindkey '^[[3~' delete-char
bindkey -a '^[[3~' delete-char
# always allow removing words with <c-w>
bindkey -a '^w' backward-kill-word
# enable cycling through previous commands with <c-p/n>
bindkey '^P' up-line-or-history
bindkey '^N' down-line-or-history
# search history backwards <c-r>
bindkey '^r' history-incremental-search-backward
# Send command to editor and back for execution
zle -N edit-command-line
bindkey -M vicmd ' ' edit-command-line
# Deduplicate PATH - remove any duplicate entries from PATH
# from: https://unix.stackexchange.com/questions/40749/remove-duplicate-path-entries-with-awk-command
get_var() {
eval 'printf "%s\n" "${'"$1"'}"'
}
set_var() {
eval "$1=\"\$2\""
}
dedup_pathvar() {
pathvar_name="$1"
pathvar_value="$(get_var "$pathvar_name")"
deduped_path="$(perl -e 'print join(":",grep { not $seen{$_}++ } split(/:/, $ARGV[0]))' "$pathvar_value")"
set_var "$pathvar_name" "$deduped_path"
}
dedup_pathvar PATH
dedup_pathvar MANPATH
# shellcheck is not built for zsh specific uses like p10k does
# so we remove it from being analyzed and linted in shellcheck by adding underscore
# To customize prompt, run `p10k configure` or edit ~/.config/zsh/.p10k.zsh.
[[ ! -f "$ZSHCONFDIR/.p10k._zsh" ]] || source "$ZSHCONFDIR/.p10k._zsh"
unset CONFDIR
unset ZSHCONFDIR