Parent directory

vim-interaction.plugin.zsh

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