zsh+fzf=awesome

This commit is contained in:
David Chen 2020-03-04 22:48:38 -08:00
parent e3d1fcc5d8
commit 46cd84d3a6
21 changed files with 507 additions and 108 deletions

35
zsh/aliases.zsh Normal file
View file

@ -0,0 +1,35 @@
alias av='source venv/bin/activate'
alias c='clear'
alias cdiff='colordiff'
alias cs='calcurse'
alias dv='deactivate'
alias gc='git config credential.helper store'
alias ipy='ipython'
alias l='ls -la'
alias lg='lazygit'
alias ms='mailsync'
alias mt='neomutt'
alias r='echo $RANGER_LEVEL'
alias pu='python3 -m pudb'
alias ra='ranger'
# ra() {
#if [ -z "$RANGER_LEVEL" ]
#then
#ranger
#else
#exit
#fi
#}
alias s='neofetch'
alias g='onefetch'
alias sra='sudo -E ranger'
alias sudo='sudo -E'
alias vim='nvim'
alias gs='git config credential.helper store'
alias bat='sudo tlp bat'
alias ac='sudo tlp ac'
alias gy='git-yolo'
alias nb='newsboat -r'
alias nt="sh -c 'cd $(pwd); st' > /dev/null 2>&1 &"
alias ta='tmux a'
alias t='tmux'

25
zsh/env.zsh Normal file
View file

@ -0,0 +1,25 @@
export GOPATH=$HOME/go
export PATH=$PATH:$HOME/.gem/ruby/2.6.0
export PATH=$PATH:$HOME/go/bin
export PATH=$PATH:/home/linuxbrew/.linuxbrew/bin
export PATH=$PATH:$HOME/.local/bin
export PATH=$PATH:$HOME/.config/bin
export PATH=$PATH:/snap/bin
#export TERM=xterm-256color
export PATH=$PATH:/home/david/prog/flutter/bin
export TERM=xterm-256color
export TERM_ITALICS=true
export RANGER_LOAD_DEFAULT_RC="false"
#export TERM=screen-256color
export EDITOR=nvim
export XDG_CONFIG_HOME=/home/david/.config
export ZSH_AUTOSUGGEST_USE_ASYNC=1
export ZSH_AUTOSUGGEST_MANUAL_REBIND=1
# fzf
export FZF_DEFAULT_OPTS='--bind ctrl-e:down,ctrl-u:up --preview "[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (ccat --color=always {} || highlight -O ansi -l {} || cat {}) 2> /dev/null | head -500"'
export FZF_DEFAULT_COMMAND='ag --hidden --ignore .git -g ""'
export FZF_COMPLETION_TRIGGER='\'
export FZF_TMUX=1
export FZF_TMUX_HEIGHT='80%'
export fzf_preview_cmd='[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (ccat --color=always {} || highlight -O ansi -l {} || cat {}) 2> /dev/null | head -500'

59
zsh/fzf.zsh Normal file
View file

@ -0,0 +1,59 @@
export FZF_DEFAULT_OPTS='--bind=ctrl-t:top,change:top --bind ctrl-e:down,ctrl-u:up'
_fzf_fpath=${0:h}/fzf
fpath+=$_fzf_fpath
autoload -U $_fzf_fpath/*(.:t)
unset _fzf_fpath
fzf-redraw-prompt() {
local precmd
for precmd in $precmd_functions; do
$precmd
done
zle reset-prompt
}
zle -N fzf-redraw-prompt
zle -N fzf-find-widget
bindkey '^p' fzf-find-widget
fzf-cd-widget() {
local tokens=(${(z)LBUFFER})
if (( $#tokens <= 1 )); then
zle fzf-find-widget 'only_dir'
if [[ -d $LBUFFER ]]; then
cd $LBUFFER
local ret=$?
LBUFFER=
zle fzf-redraw-prompt
return $ret
fi
fi
}
zle -N fzf-cd-widget
bindkey '^t' fzf-cd-widget
fzf-history-widget() {
local num=$(fhistory $LBUFFER)
local ret=$?
if [[ -n $num ]]; then
zle vi-fetch-history -n $num
fi
zle reset-prompt
return $ret
}
zle -N fzf-history-widget
bindkey '^R' fzf-history-widget
fif() {
if [ ! "$#" -gt 0 ]; then echo "Need a string to search for!"; return 1; fi
rg --files-with-matches --no-messages "$1" | fzf --preview "highlight -O ansi -l {} 2> /dev/null | rg --colors 'match:bg:yellow' --ignore-case --pretty --context 10 '$1' || rg --ignore-case --pretty --context 10 '$1' {}"
}
find-in-file() {
grep --line-buffered --color=never -r "" * | fzf
}
zle -N find-in-file
bindkey '^f' find-in-file

11
zsh/fzf/bcp Executable file
View file

@ -0,0 +1,11 @@
### BREW + FZF
# mnemonic [B]rew [I]nstall [P]lugin
local uninst=$(brew leaves | eval "fzf ${FZF_DEFAULT_OPTS} -m --header='[brew:clean]'")
if [[ $uninst ]]; then
for prog in $(echo $uninst)
do brew uninstall $prog
done
fi

12
zsh/fzf/bip Executable file
View file

@ -0,0 +1,12 @@
### BREW + FZF
# update multiple packages at once
# mnemonic [B]rew [U]pdate [P]lugin
local inst=$(brew search | eval "fzf ${FZF_DEFAULT_OPTS} -m --header='[brew:install]'")
if [[ $inst ]]; then
for prog in $(echo $inst)
do brew install $prog
done
fi

12
zsh/fzf/bup Executable file
View file

@ -0,0 +1,12 @@
### BREW + FZF
# uninstall multiple packages at once, async
# mnemonic [B]rew [C]lean [P]lugin (e.g. uninstall)
local upd=$(brew leaves | eval "fzf ${FZF_DEFAULT_OPTS} -m --header='[brew:update]'")
if [[ $upd ]]; then
for prog in $(echo $upd)
do brew upgrade $prog
done
fi

10
zsh/fzf/cani Executable file
View file

@ -0,0 +1,10 @@
### Caniuse + FZF
# caniuse for quick access to global support list
# also runs the `caniuse` command if installed
local feat=$(ciu | sort -rn | eval "fzf ${FZF_DEFAULT_OPTS} --ansi --header='[caniuse:features]'" | sed -e 's/^.*%\ *//g' | sed -e 's/ .*//g')
if which caniuse &> /dev/null; then
if [[ $feat ]] then; caniuse $feat; fi
fi

80
zsh/fzf/ffind Executable file
View file

@ -0,0 +1,80 @@
#!/usr/bin/env zsh
setopt localoptions
local s_path level
local only_dir
while getopts ':dl:s:' opt; do
case $opt in
d)
only_dir=1
;;
l)
level=$OPTARG
;;
s)
s_path=$OPTARG
;;
\?)
echo "Invalid option -$OPTARG." >&2
;;
esac
done
local cmd="command find -L ${s_path:-.} -mindepth 1 ${level:+-maxdepth $level} \
\\( -path '*/\\.git' \
-o -path '*/venv' \
-o -fstype 'sysfs' \
-o -fstype 'devfs' \
-o -fstype 'devtmpfs' \
-o -fstype 'proc' \\) -prune \
-o -type d -print"
if [[ -z $only_dir ]]; then
cmd="$cmd -o -type f -print -o -type l -print"
fi
cmd="$cmd 2>/dev/null"
if [[ -z $s_path || $s_path == '.' ]]; then
cmd="$cmd | cut -b3-"
fi
local fzf_opts="--height=50% --reverse -m --tiebreak=end \
--preview-window=right:60%:wrap \
--bind=ctrl-alt-u:preview-up,ctrl-alt-e:preview-down"
local fzf_preview_cmd='
t=${$(readlink {}):-{}}
if [[ $(file -i $t) =~ directory ]]; then
(exa --color=always -T -L 1 {} ||
tree -C -L 1 {} ||
echo {} is a directory.) 2>/dev/null
elif [[ $(file -i $t) =~ binary ]]; then
echo {} is a binary file.
else
ccat --color=always {} 2>/dev/null
fi
'
local -a ret_array
local ret=0
eval $cmd | fzf ${(z)fzf_opts} --preview $fzf_preview_cmd ${(z)EXTRA_OPTS} | {
while read item; do
printf "${(q)item} "
done
} | sed -E '$s/ $//'
ret_array=($pipestatus)
# ignore find command error code 1 such as loop detect or permission
if (( $ret_array[1] == 1 )); then
ret=0
fi
local p_ret
for p_ret in $ret_array[2,-1]; do
if (( p_ret )); then
ret=$p_ret
fi
done
return $ret

5
zsh/fzf/ffunctions Executable file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env zsh
setopt localoptions pipefail
print -l ${(k)functions} | fzf --height=50%

13
zsh/fzf/fhistory Executable file
View file

@ -0,0 +1,13 @@
#!/usr/bin/env zsh
setopt localoptions pipefail
# TODO: sort is not stream command, bad performance #
local selected=( $(fc -rl 1 | sort -k 2 -u | sort -rn |
fzf --height 50% -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort --query=$1 +m) )
local ret=$?
if [[ -n $selected ]]; then
print $selected[1]
fi
return $ret

12
zsh/fzf/fp Executable file
View file

@ -0,0 +1,12 @@
### PATH
# mnemonic: [F]ind [P]ath
# list directories in $PATH, press [enter] on an entry to list the executables inside.
# press [escape] to go back to directory listing, [escape] twice to exit completely
local loc=$(echo $PATH | sed -e $'s/:/\\\n/g' | eval "fzf ${FZF_DEFAULT_OPTS} --header='[find:path]'")
if [[ -d $loc ]]; then
echo "$(rg --files $loc | rev | cut -d"/" -f1 | rev)" | eval "fzf ${FZF_DEFAULT_OPTS} --header='[find:exe] => ${loc}' >/dev/null"
fp
fi

28
zsh/fzf/fps Executable file
View file

@ -0,0 +1,28 @@
#!/usr/bin/env zsh
_fps() {
local render
if (( $+commands[grcat] )); then
render='grcat fps.grc'
else
render='cat'
fi
ps -eo user,pid,ppid,pgid,stat,tname,cmd | awk '
BEGIN { "ps -p $$ -o pgid --no-headers | tr -d \"[:blank:]\"" | getline pgid } {
if ($4 != pgid || $2 == pgid)
print
}' | ${(z)render}
}
setopt localoptions pipefail
local fzf_opts="--header-lines=1 -m \
${commands[grcat]:+--ansi} --height=50% \
--min-height=15 --tac --reverse \
--preview-window=down:2"
local fzf_preview_cmd="ps -o pcpu,pmem,vsz,rss,thcount,start_time,time -p {2}"
_fps | fzf ${(z)fzf_opts} --preview=$fzf_preview_cmd |
awk -v sep=${FZF_MUL_DELIM:- } '{ printf "%s%c", $2, sep }' | sed -E "s/${FZF_MUL_DELIM:- }$//"

42
zsh/fzf/fzf-find-widget Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env zsh
local ret=0
local tokens=(${(z)LBUFFER})
local -a cmd_opts=()
local -a extra_fzf_opts=()
if [[ $1 == 'only_dir' ]]; then
cmd_opts+='-d'
extra_fzf_opts+='+m'
fi
if (( $#tokens )) && [[ $LBUFFER[-1] != ' ' ]]; then
local is_wave=0
local dir=${(e)tokens[-1]}
if [[ $dir[1] == ~* ]]; then
is_wave=1
fi
dir=${dir/#'~'/$HOME}
local base
if [[ ! -d $dir ]]; then
base=$(basename $dir)
dir=$(dirname $dir)
fi
cmd_opts+="-s $dir"
extra_fzf_opts+=${base:+--query $base}
match=$(EXTRA_OPTS=${(z)extra_fzf_opts} ffind ${(z)cmd_opts})
ret=$?
if (( ret == 0 || ret == 141 )); then
if (( is_wave )); then
match=$(sed "s| $HOME| ~|g" <<<$match)
fi
if (( $#tokens == 1 )); then
LBUFFER=$match
else
LBUFFER="$tokens[0,-2] $match"
fi
fi
else
LBUFFER=$LBUFFER$(EXTRA_OPTS=${(z)extra_fzf_opts} ffind ${(z)cmd_opts})
ret=$?
fi
zle reset-prompt
return $ret

14
zsh/fzf/kp Executable file
View file

@ -0,0 +1,14 @@
### PROCESS
# mnemonic: [K]ill [P]rocess
# show output of "ps -ef", use [tab] to select one or multiple entries
# press [enter] to kill selected processes and go back to the process list.
# or press [escape] to go back to the process list. Press [escape] twice to exit completely.
local pid=$(ps -ef | sed 1d | eval "fzf ${FZF_DEFAULT_OPTS} -m --header='[kill:process]'" | awk '{print $2}')
if [ "x$pid" != "x" ]
then
echo $pid | xargs kill -${1:-9}
kp
fi

14
zsh/fzf/ks Executable file
View file

@ -0,0 +1,14 @@
### SERVER
# mnemonic: [K]ill [S]erver
# show output of "lsof -Pwni tcp", use [tab] to select one or multiple entries
# press [enter] to kill selected processes and go back to the process list.
# or press [escape] to go back to the process list. Press [escape] twice to exit completely.
local pid=$(lsof -Pwni tcp | sed 1d | eval "fzf ${FZF_DEFAULT_OPTS} -m --header='[kill:tcp]'" | awk '{print $2}')
if [ "x$pid" != "x" ]
then
echo $pid | xargs kill -${1:-9}
ks
fi

42
zsh/fzf/util Executable file
View file

@ -0,0 +1,42 @@
### UTILS
# show utils commands and custom aliasses
local helptxt="bcp [brew:clean]
bip [brew:install]
bup [brew:update]
cani [caniuse:features]
fp [find:path]
kp [kill:path]
ks [kill:tcp]
av [alias] source venv/bin/activate
c [alias] clear
cdiff [alias] colordiff
cs [alias] calcurse
dv [alias] deactivate
gc [alias] git config credential.helper store
ipy [alias] ipython
l [alias] ls -la
lg [alias] lazygit
ms [alias] mailsync
mt [alias] neomutt
r [alias] echo $RANGER_LEVEL
pu [alias] python3 -m pudb
ra [alias] ranger
s [alias] neofetch
g [alias] onefetch
sra [alias] sudo -E ranger
sudo [alias] sudo -E
vim [alias] nvim
gs [alias] git config credential.helper store
bat [alias] sudo tlp bat
ac [alias] sudo tlp ac
gy [alias] git-yolo
nb [alias] newsboat -r
nt [alias] sh -c 'cd $(pwd); st' > /dev/null 2>&1 &"
local cmd=$(echo $helptxt | eval "fzf ${FZF_DEFAULT_OPTS} --header='[utils:show]'" | awk '{print $1}')
if [[ -n $cmd ]]; then
eval ${cmd}
fi

6
zsh/plugins.zsh Normal file
View file

@ -0,0 +1,6 @@
export PLUG_DIR=$HOME/.zim
if [[ ! -d $PLUG_DIR ]]; then
curl -fsSL https://raw.githubusercontent.com/zimfw/install/master/install.zsh | zsh
rm ~/.zimrc
ln -s ~/.config/zsh/zimrc ~/.zimrc
fi

15
zsh/tmux.zsh Normal file
View file

@ -0,0 +1,15 @@
tmux_preexec() {
local tmux_event=${TMUX%%,*}-event/client-attached-pane
if [[ -f $tmux_event-$TMUX_PANE ]]; then
eval $(tmux showenv -s)
command rm $tmux_event-$TMUX_PANE 2>/dev/null
fi
}
if [[ -n $TMUX ]]; then
local tmux_event=${TMUX%%,*}-event/client-attached-pane
command rm $tmux_event-$TMUX_PANE 2>/dev/null
autoload -U add-zsh-hook
add-zsh-hook preexec tmux_preexec
fi

49
zsh/vi.zsh Normal file
View file

@ -0,0 +1,49 @@
bindkey '^v' edit-command-line
bindkey -v
bindkey -M vicmd "k" vi-insert
bindkey -M vicmd "K" vi-insert-bol
bindkey -M vicmd "n" vi-backward-char
bindkey -M vicmd "i" vi-forward-char
bindkey -M vicmd "N" vi-beginning-of-line
bindkey -M vicmd "I" vi-end-of-line
bindkey -M vicmd "e" down-line-or-history
bindkey -M vicmd "u" up-line-or-history
bindkey -M vicmd "l" undo
#bindkey -M vicmd "-" vi-rev-repeat-search
bindkey -M vicmd "=" vi-repeat-search
bindkey -M vicmd "h" vi-forward-word-end
#function zle-line-init zle-keymap-select {
#RPS1="${${KEYMAP/vicmd/-- NOR --}/(main|viins)/-- INS --}"
#RPS2=$RPS1
#zle reset-prompt
#}
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
# Use beam shape cursor on startup.
echo -ne '\e[5 q'
# Use beam shape cursor for each new prompt.
preexec() {
echo -ne '\e[5 q'
}
_fix_cursor() {
echo -ne '\e[5 q'
}
precmd_functions+=(_fix_cursor)
zle -N zle-line-init
zle -N zle-keymap-select
KEYTIMEOUT=1

16
zsh/zimrc Normal file
View file

@ -0,0 +1,16 @@
zmodule environment
zmodule git
zmodule input
zmodule termtitle
zmodule utility
zmodule completion
zmodule git-info
zmodule magicmace
zmodule zsh-users/zsh-completions
zmodule zsh-users/zsh-autosuggestions
zmodule zsh-users/zsh-history-substring-search
zmodule zdharma/fast-syntax-highlighting
zmodule supercrabtree/k
zmodule mafredri/zsh-async
zmodule hlissner/zsh-autopair
#zmodule cusxio/delta-prompt

115
zsh/zshrc
View file

@ -1,115 +1,14 @@
export GOPATH=$HOME/go
export PATH=$PATH:$HOME/.gem/ruby/2.6.0
export PATH=$PATH:$HOME/go/bin
export PATH=$PATH:/home/linuxbrew/.linuxbrew/bin
export PATH=$PATH:$HOME/.local/bin
export PATH=$PATH:/snap/bin
#export TERM=xterm-256color
export PATH=$PATH:/home/david/prog/flutter/bin
export TERM=xterm-256color
export TERM_ITALICS=true
export RANGER_LOAD_DEFAULT_RC="false"
#export TERM=screen-256color
export EDITOR=nvim
export XDG_CONFIG_HOME=/home/david/.config
#autoload edit-command-line; zle -N edit-command-line
#bindkey '^v' edit-command-line
alias av='source venv/bin/activate'
alias c='clear'
alias cdiff='colordiff'
alias cs='calcurse'
alias dv='deactivate'
alias gc='git config credential.helper store'
alias ipy='ipython'
alias l='ls -la'
alias lg='lazygit'
alias ms='mailsync'
alias mt='neomutt'
alias r='echo $RANGER_LEVEL'
alias pu='python3 -m pudb'
alias ra='ranger'
# ra() {
#if [ -z "$RANGER_LEVEL" ]
#then
#ranger
#else
#exit
#fi
#}
alias s='neofetch'
alias g='onefetch'
alias sra='sudo -E ranger'
alias sudo='sudo -E'
alias vim='nvim'
alias gs='git config credential.helper store'
alias bat='sudo tlp bat'
alias ac='sudo tlp ac'
alias gy='git-yolo'
alias nb='newsboat -r'
alias nt="sh -c 'cd $(pwd); st' > /dev/null 2>&1 &"
alias ta='tmux a'
alias t='tmux'
bindkey -v
bindkey -M vicmd "k" vi-insert
bindkey -M vicmd "K" vi-insert-bol
bindkey -M vicmd "n" vi-backward-char
bindkey -M vicmd "i" vi-forward-char
bindkey -M vicmd "N" vi-beginning-of-line
bindkey -M vicmd "I" vi-end-of-line
bindkey -M vicmd "e" down-line-or-history
bindkey -M vicmd "u" up-line-or-history
bindkey -M vicmd "l" undo
#bindkey -M vicmd "-" vi-rev-repeat-search
bindkey -M vicmd "=" vi-repeat-search
bindkey -M vicmd "h" vi-forward-word-end
#function zle-line-init zle-keymap-select {
#RPS1="${${KEYMAP/vicmd/-- NOR --}/(main|viins)/-- INS --}"
#RPS2=$RPS1
#zle reset-prompt
#}
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
# Use beam shape cursor on startup.
echo -ne '\e[5 q'
# Use beam shape cursor for each new prompt.
preexec() {
echo -ne '\e[5 q'
}
_fix_cursor() {
echo -ne '\e[5 q'
}
precmd_functions+=(_fix_cursor)
source ~/.config/zsh/env.zsh
source ~/.config/zsh/aliases.zsh
source ~/.config/zsh/plugins.zsh
source ~/.config/zsh/vi.zsh
source ~/.config/zsh/fzf.zsh
zle -N zle-line-init
zle -N zle-keymap-select
KEYTIMEOUT=1
# fzf
export FZF_DEFAULT_OPTS='--bind ctrl-e:down,ctrl-u:up --preview "[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (ccat --color=always {} || highlight -O ansi -l {} || cat {}) 2> /dev/null | head -500"'
export FZF_DEFAULT_COMMAND='ag --hidden --ignore .git -g ""'
export FZF_COMPLETION_TRIGGER='\'
export FZF_TMUX_HEIGHT='80%'
export FZF_PREVIEW_COMMAND='[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (ccat --color=always {} || highlight -O ansi -l {} || cat {}) 2> /dev/null | head -500'
source ~/.config/zsh/key-bindings.zsh
source ~/.config/zsh/completion.zsh
if [ -f ~/.sconfig/zsh/zshrc ]; then
source ~/.sconfig/zsh/zshrc
fi
autopair-init