Skip to content

Instantly share code, notes, and snippets.

@mikiyaf
Created November 13, 2017 01:31
Show Gist options
  • Save mikiyaf/4c19091dffa47ef24e730de5ed66d8f7 to your computer and use it in GitHub Desktop.
Save mikiyaf/4c19091dffa47ef24e730de5ed66d8f7 to your computer and use it in GitHub Desktop.
mru(mru+fzf) : mru incremental search by fzf, and execute vim, less, ...
mru() {
local -a f
f=(
~/.vim_mru_files(N)
~/.unite/file_mru(N)
~/.cache/ctrlp/mru/cache.txt(N)
~/.frill(N)
)
if [[ $#f -eq 0 ]]; then
echo "There is no available MRU Vim plugins" >&2
return 1
fi
local cmd q k res
local line ok make_dir i arr
local get_styles styles style
while : ${make_dir:=0}; ok=("${ok[@]:-dummy_$RANDOM}"); cmd="$(
cat <$f \
| while read line; do [ -e "$line" ] && echo "$line"; done \
| while read line; do [ "$make_dir" -eq 1 ] && echo "${line:h}/" || echo "$line"; done \
| awk '!a[$0]++' \
| perl -pe 's/^(\/.*\/)(.*)$/\033[34m$1\033[m$2/' \
| fzf --ansi --multi --query="$q" \
--no-sort --exit-0 --prompt="MRU> " \
--print-query --expect=ctrl-v,ctrl-x,ctrl-l,ctrl-q,ctrl-r,"?"
)"; do
q="$(head -1 <<< "$cmd")"
k="$(head -2 <<< "$cmd" | tail -1)"
res="$(sed '1,2d;/^$/d' <<< "$cmd")"
[ -z "$res" ] && continue
case "$k" in
"?")
cat <<HELP > /dev/tty
usage: vim_mru_files
list up most recently files
keybind:
ctrl-q output files and quit
ctrl-l less files under the cursor
ctrl-v vim files under the cursor
ctrl-r change view type
ctrl-x remove files (two-step)
HELP
return 1
;;
ctrl-r)
if [ $make_dir -eq 1 ]; then
make_dir=0
else
make_dir=1
fi
continue
;;
ctrl-l)
export LESS='-R -f -i -P ?f%f:(stdin). ?lb%lb?L/%L.. [?eEOF:?pb%pb\%..]'
arr=("${(@f)res}")
if [[ -d ${arr[1]} ]]; then
ls -l "${(@f)res}" < /dev/tty | less > /dev/tty
else
if has "pygmentize"; then
get_styles="from pygments.styles import get_all_styles
styles = list(get_all_styles())
print('\n'.join(styles))"
styles=( $(sed -e 's/^ *//g' <<<"$get_styles" | python) )
style=${${(M)styles:#solarized}:-default}
export LESSOPEN="| pygmentize -O style=$style -f console256 -g %s"
fi
less "${(@f)res}" < /dev/tty > /dev/tty
fi
;;
ctrl-x)
if [[ ${(j: :)ok} == ${(j: :)${(@f)res}} ]]; then
eval '${${${(M)${+commands[gomi]}#1}:+gomi}:-rm} "${(@f)res}" 2>/dev/null'
ok=()
else
ok=("${(@f)res}")
fi
;;
ctrl-v)
nvim -p "${(@f)res}" < /dev/tty > /dev/tty
;;
ctrl-q)
echo "$res" < /dev/tty > /dev/tty
return $status
;;
*)
echo "${(@f)res}"
break
;;
esac
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment