config/fish/conf.d/fzf.fish

78 lines
2.1 KiB
Fish

# https://github.com/junegunn/fzf
set -gx FZF_DEFAULT_OPTS '--layout=reverse'
set -gx FZF_DEFAULT_COMMAND 'fd --type=file'
function __fzf_files
set -l output (
fd \
--type=file \
--hidden \
--follow \
--color=always \
--exclude=.git \
--strip-cwd-prefix \
2>/dev/null |
fzf \
--ansi \
--preview='bat --style=numbers --color=always {}' \
--preview-window='right,70%,wrap,hidden,<200(bottom,50%,hidden)' \
--bind='?:toggle-preview' \
--expect='ctrl-e' \
--header='ctrl-e to edit, ? to preview'
)
set -l key $output[1]
set -l file $output[2]
if test (count (string split ' ' "$file")) -gt 1
set file "\"$file\""
end
switch $key
case ctrl-e
commandline -r "edit $file"
case '*'
commandline -i $file
end
commandline -f repaint
end
function __fzf_git_log
if not git rev-parse --git-dir >/dev/null 2>&1
echo '__fzf_search_git_log: Not in a git repository!' >&2
else
set -l output (
git logsearch |
fzf \
--ansi \
--tiebreak=index \
--preview='git show {2} | delta' \
--preview-window='right,70%,wrap,hidden,<200(bottom,50%,hidden)' \
--bind='?:toggle-preview' \
--header='? to preview'
)
if set -l commit_hash (string split --max 2 " " $output)[2]
commandline -i $commit_hash
end
end
commandline -f repaint
end
function __fzf_history
set -l separator ' | '
set -l output (
history search \
--null \
--show-time="%Y-%m-%d$separator" |
fzf \
--read0 \
--tiebreak=index \
--query=(commandline) \
--preview='echo {3..}' \
--preview-window='down,10,wrap,hidden' \
--bind='?:toggle-preview' \
--header='? to preview'
)
if set -l command (string split --max 1 $separator $output)[2..-1]
commandline -r $command
end
commandline -f repaint
end