Skip to content

Instantly share code, notes, and snippets.

@FractalWire
Last active September 5, 2020 03:32
Show Gist options
  • Save FractalWire/ef1721799d7b043bc62d9fe70205ec3c to your computer and use it in GitHub Desktop.
Save FractalWire/ef1721799d7b043bc62d9fe70205ec3c to your computer and use it in GitHub Desktop.
preview file with fzf and nvr
#!/bin/bash
# simple script to preview files with the help of fzf
cwd=$(pwd)
# set up
nvr --nostart -cc 'let nvrp_buffer=""'
set_variables='mime_list=( "application/json" "text/" ); mime=$(file --mime-type {} | cut -d " " -f 2)'
f_is_text='for m in ${mime_list[@]}; do test "${mime#$m}" != "$mime" && break || false; done'
vim_expr_before='let old_nvrp_buffer=nvrp_buffer'
vim_expr='buflisted("{}") > 0 && execute(["buffer {}", "let nvrp_buffer=\"\"", "echo nvrp_buffer"])+1 || execute(["edit {}", "let nvrp_buffer=\"{}\""])'
vim_expr_after='if old_nvrp_buffer != "" | execute("bdelete " . old_nvrp_buffer) | unlet old_nvrp_buffer | endif'
cmd=$(echo "$set_variables; $f_is_text && nvr --nostart -cc '$vim_expr_before' --remote-expr '$vim_expr' -c '$vim_expr_after'" | sed "s#{}#$cwd/{}#g")
f=$(fzf --multi --reverse --bind 'ctrl-space:toggle' --no-height --preview-window up:0 --preview "$cmd" $@)
success=$?
# cleaning up or opening file(s)
if test $success -eq 0
then
nvr --nostart -cc 'unlet nvrp_buffer' $f
echo "opening $(echo $f | tr '\n' ' ')"
else
nvr --nostart --remote-expr \
'buflisted(nvrp_buffer) > 0 && execute(["bdelete " . nvrp_buffer])' \
-c 'unlet nvrp_buffer' > /dev/null
echo "No file to opened"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment