v 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2011 rupa deadwyler. Licensed under the WTFPL license, Version 2
  3. [ "$vim" ] || vim=vim
  4. [ $viminfo ] || viminfo=~/.viminfo
  5. usage="$(basename $0) [-a] [-c] [-l] [-[0-9]] [--debug] [--help] [regexes]"
  6. [ $1 ] || list=1
  7. _pwd="$(command pwd)"
  8. fnd=()
  9. for x; do case $x in
  10. -a) deleted=1;;
  11. -c) subdir=1; shift;;
  12. -l) list=1;;
  13. -[0-9]) edit=${x:1}; shift;;
  14. --help) echo $usage; exit;;
  15. --debug) vim=echo;;
  16. --) shift; fnd+=("$@"); break;;
  17. *) fnd+=("$x");;
  18. esac; shift; done
  19. set -- "${fnd[@]}"
  20. [ -f "$1" ] && {
  21. $vim "$1"
  22. exit
  23. }
  24. while IFS=" " read line; do
  25. [ "${line:0:1}" = ">" ] || continue
  26. fl=${line:2}
  27. _fl="${fl/~\//$HOME/}"
  28. [ -f "$_fl" -o "$deleted" ] || continue
  29. match=1
  30. for x; do
  31. [[ "$fl" =~ $x ]] || match=
  32. done
  33. [ "$subdir" ] && {
  34. case "$_fl" in
  35. $_pwd*);;
  36. *) match=;;
  37. esac
  38. }
  39. [ "$match" ] || continue
  40. i=$((i+1))
  41. files[$i]="$fl"
  42. done < "$viminfo"
  43. if [ "$edit" ]; then
  44. resp=${files[$((edit+1))]}
  45. elif [ "$i" = 1 -o "$list" = "" ]; then
  46. resp=${files[1]}
  47. elif [ "$i" ]; then
  48. while [ $i -gt 0 ]; do
  49. echo -e "$((i-1))\t${files[$i]}"
  50. i=$((i-1))
  51. done
  52. read -p '> ' CHOICE
  53. [ "$CHOICE" ] && resp=${files[$((CHOICE+1))]}
  54. fi
  55. [ "$resp" ] || exit
  56. $vim "${resp/\~/$HOME}"