vim-interaction.plugin.zsh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #
  2. # See README.md
  3. #
  4. # Derek Wyatt (derek@{myfirstnamemylastname}.org
  5. #
  6. function callvim {
  7. if [[ $# == 0 ]]; then
  8. cat <<EOH
  9. usage: callvim [-b cmd] [-a cmd] [-n name] [file ... fileN]
  10. -b cmd Run this command in GVIM before editing the first file
  11. -a cmd Run this command in GVIM after editing the first file
  12. -n name Name of the GVIM server to connect to
  13. file The file to edit
  14. ... fileN The other files to add to the argslist
  15. EOH
  16. return 0
  17. fi
  18. # Look up the newest instance or start one
  19. local name="$(gvim --serverlist | tail -n 1)"
  20. [[ -n "$name" ]] || {
  21. # run gvim or exit if it fails
  22. gvim || return $?
  23. # wait for gvim instance to fully load
  24. while name=$(gvim --serverlist) && [[ -z "$name" ]]; do
  25. sleep 0.1
  26. done
  27. }
  28. local before="<esc>" files after cmd
  29. while getopts ":b:a:n:" option
  30. do
  31. case $option in
  32. a) after="$OPTARG"
  33. ;;
  34. b) before="$OPTARG"
  35. ;;
  36. n) name="$OPTARG"
  37. ;;
  38. esac
  39. done
  40. shift $((OPTIND-1))
  41. # If before or after commands begin with : and don't end with <cr>, append it
  42. [[ ${after} = :* && ${after} != *\<cr\> ]] && after+="<cr>"
  43. [[ ${before} = :* && ${before} != *\<cr\> ]] && before+="<cr>"
  44. # Open files passed (:A means abs path resolving symlinks, :q means quoting special chars)
  45. [[ $# -gt 0 ]] && files=':args! '"${@:A:q}<cr>"
  46. # Pass the built vim command to gvim
  47. cmd="$before$files$after"
  48. # Run the gvim command
  49. gvim --servername "$name" --remote-send "$cmd" || return $?
  50. # Run postCallVim if defined (maybe to bring focus to gvim, see README)
  51. (( ! $+functions[postCallVim] )) || postCallVim
  52. }
  53. alias v=callvim
  54. alias vvsp="callvim -b':vsp'"
  55. alias vhsp="callvim -b':sp'"
  56. alias vk="callvim -b':wincmd k'"
  57. alias vj="callvim -b':wincmd j'"
  58. alias vl="callvim -b':wincmd l'"
  59. alias vh="callvim -b':wincmd h'"