dotfiles/.config/shell/rc.d/alias-fuzzy-finding.sh
Marty Oehme 7e45d94997 Add fuzzy tools to vim and shell
In vim:
Use <leader>c to insert a bibtex reference in your text. By default it
is a pandoc reference (@bibref), but it can be changed to latex style
(\cite(bibref)). <leader>CM inserts a pretty-printed reference to the
selected work, using markdown styling. If you want to insert a citation
while writing, use @@ from insertmode to insert the bibref instead.

The settings add two commands: :CiteRef and :CitePretty which call the
respective functions. You can pass any amount of .bibtex libraries to
the commands and they will be available to fuzzy search through.

:CiteEdit also added to fuzzy find a source and open it in vim for editing.
The function is not working yet, I have to find a way to go from the
fuzzy finder to papis, select the correct file and edit it in vim.

In Shell:
Can cd directories (d, D), open files (f, F), open most recently used
(ru), and edit bibtex references (ref). lowercase is a weighted view
over previously used directories/files, Uppercase is a search of the
whole file structure.

Still outstanding:
Needs the same comfort function additions as vim search, especially
reference search. (i.e., open corresponding document, yank path, open
editor,...)
2019-09-04 17:32:11 +00:00

81 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
echo "[WARNING]: fzy found as fuzzy finder - install skim to use full script functionality"
alias fzf=fzy
elif type fzf >/dev/null 2>&1; then
echo "[WARNING]: fzf found as fuzzy finder - install skim to use full script functionality"
alias fzf=fzf
else
echo "[WARNING]: No fuzzy finder found - install skim 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"