dotfiles/shell/.config/shell/rc.d/alias-fuzzy-finding.sh
2019-12-29 23:12:13 +01:00

83 lines
2.8 KiB
Bash

#!/bin/sh
# check for existence of fuzzy finders. If found, substitute fzf with it.
# order is skim > fzy > fzf > nothing
if type sk >/dev/null 2>&1; then
alias fzf=sk
elif type fzy >/dev/null 2>&1; then
alias fzf=fzy
elif type fzf >/dev/null 2>&1; then
alias fzf=fzf
else
echo "[WARNING]: No fuzzy finder found - install skim/fzf/fzy to enable functionality"
fi
# check for existence of greplikes. If found, substitute fzf with it.
# order is skim > fzy > fzf > nothing
if type rg >/dev/null 2>&1; then
alias rg=rg
elif type ag >/dev/null 2>&1; then
alias rg=ag
elif type ack >/dev/null 2>&1; then
alias rg=ack
else
echo "[WARNING]: No grep-like found - install ripgrep/the silver surfer (ag)/ack to enable functionality"
fi
# TODO Allow yanking of urls, c-e to open in editor, preview window with a-p and more
# FZF FILES AND FOLDERS
fzf_cd_weighted() {
cd "$(fasd -d | sk --nth=1 --with-nth=2 --tac | cut -d' ' -f2- | sed 's/^[ \t]*//;s/[\t]*$//')" || echo "cd not successful"
}
fzf_cd_all() {
cd "$(sk --cmd 'find / -type d -not \( -path /proc -prune \)')" || echo "cd not successful"
}
fzf_files_weighted() {
xdg-open "$(fasd -f | sk --nth=1 --with-nth=2 --tac | cut -d' ' -f2- | sed 's/^[ \t]*//;s/[\t]*$//')" || echo "xdg-open not successful"
}
fzf_files_all() {
xdg-open "$(sk --cmd 'find / -type d -not \( -path /proc -prune \)')" || echo "xdg-open not successful"
}
fzf_mru_weighted() {
target="$(fasd -t | sk --nth=1 --with-nth=2 --tac | cut -d' ' -f2- | sed 's/^[ \t]*//;s/[\t]*$//')"
if [ -f "$target" ]; then
xdg-open "$target" || echo "xdg-open not successful"
elif [ -d "$target" ]; then
cd "$target" || echo "cd not successful"
fi
}
# FZF NOTES
fzf_notes() {
sk --cmd "command rg --follow --ignore-case --line-number --color never --no-messages --no-heading --with-filename '' /home/marty/Nextcloud/Notes" --ansi --multi --tiebreak='length,begin' --delimiter=':' --with-nth=3.. --preview='cat {1}' --preview-window=:wrap
}
# FZF BIBTEX REFERENCES
fzf_bibtex() {
target=$(sk --cmd "bibtex-ls -cache $XDG_CACHE_HOME ~/Nextcloud/Library/academia/academia.bib" --ansi --with-nth=.. --preview-window=:wrap --prompt "Citation> " --preview="papis edit -e cat ref=$(echo {} | bibtex-cite | sed 's/^@//')")
papis edit ref="$(echo "$target" | bibtex-cite | sed 's/^@//')"
}
# set up fuzzy file and directory search
alias f=fzf_files_weighted
alias F=fzf_files_all
alias d=fzf_cd_weighted
alias D=fzf_cd_all
alias ru=fzf_mru_weighted
# set up fuzzy bibtex reference search
alias ref=fzf_bibtex
# Display fuzzy-searchable history
alias zhfind="history | fzf --tac --height 20"
# Fuzzy search packages to install
alias fzay="yay -Slq | fzf -m --preview 'yay -Si {1}' | xargs -ro yay -S"
# Fuzzy uninstall packages
alias uzay="yay -Qeq | fzf -m --preview 'yay -Qi {1}' | xargs -ro yay -Rs"