From e652756fdb0495e58f3f4f3be3a82aa9f166731d Mon Sep 17 00:00:00 2001 From: Derek Wyatt Date: Tue, 20 Mar 2012 08:16:44 -0400 Subject: [PATCH 1/5] A plugin that makes it easier to interact with the (single) running instance of gvim --- plugins/vim-interaction/README.md | 69 +++++++++++++++++++ .../vim-interaction.plugin.zsh | 67 ++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 plugins/vim-interaction/README.md create mode 100644 plugins/vim-interaction/vim-interaction.plugin.zsh diff --git a/plugins/vim-interaction/README.md b/plugins/vim-interaction/README.md new file mode 100644 index 000000000..65e678b51 --- /dev/null +++ b/plugins/vim-interaction/README.md @@ -0,0 +1,69 @@ +# Vim Interaction # + +The plugin presents a function called `callvim` whose usage is: + + usage: callvim [-b cmd] [-a cmd] [file ... fileN] + + -b cmd Run this command in GVIM before editing the first file + -a cmd Run this command in GVIM after editing the first file + file The file to edit + ... fileN The other files to add to the argslist + +## Rationale ## + +The idea for this script is to give you some decent interaction with a running +GVim session. Normally you'll be running around your filesystem doing any +number of amazing things and you'll need to load some files into GVim for +editing, inspecting, destruction, or other bits of mayhem. This script lets you +do that. + +## Aliases ## + +There are a few aliases presented as well: + +* `v` A shorthand for `callvim` +* `vvsp` Edits the passed in file but first makes a vertical split +* `vhsp` Edits the passed in file but first makes a horizontal split + +## Examples ## + +This will load `/tmp/myfile.scala` into the running GVim session: + + > v /tmp/myfile.scala + +This will load it after first doing a vertical split: + + > vvsp /tmp/myfile.scala + or + > v -b':vsp' /tmp/myfile.scala + +This will load it after doing a horizontal split, then moving to the bottom of +the file: + + > vhsp -aG /tmp/myfile.scala + or + > v -b':sp' -aG /tmp/myfile.scala + +This will load the file and then copy the first line to the end (Why you would +ever want to do this... I dunno): + + > v -a':1t$' /tmp/myfile.scala + +And this will load all of the `*.txt` files into the args list: + + > v *.txt + +If you want to load files into areas that are already split, use one of the +aliases for that: + + # Do a ':wincmd h' first + > vh /tmp/myfile.scala + + # Do a ':wincmd j' first + > vj /tmp/myfile.scala + + # Do a ':wincmd k' first + > vk /tmp/myfile.scala + + # Do a ':wincmd l' first + > vl /tmp/myfile.scala diff --git a/plugins/vim-interaction/vim-interaction.plugin.zsh b/plugins/vim-interaction/vim-interaction.plugin.zsh new file mode 100644 index 000000000..3f346dfc3 --- /dev/null +++ b/plugins/vim-interaction/vim-interaction.plugin.zsh @@ -0,0 +1,67 @@ +# +# See README.md +# +# Derek Wyatt (derek@{myfirstnamemylastname}.org +# + +function resolveFile +{ + if [ -f "$1" ]; then + echo $(readlink -f "$1") + else + echo "$1" + fi +} + +function callvim +{ + if [[ $# == 0 ]]; then + cat <} == $after ]]; then + after="$after" + fi + if [[ ${before#:} != $before && ${before%} == $before ]]; then + before="$before" + fi + local files="" + for f in $@ + do + files="$files $(resolveFile $f)" + done + if [[ -n $files ]]; then + files=':args! '"$files" + fi + cmd="$before$files$after" + gvim --remote-send "$cmd" +} + +alias v=callvim +alias vvsp="callvim -b':vsp'" +alias vhsp="callvim -b':sp'" +alias vk="callvim -b':wincmd k'" +alias vj="callvim -b':wincmd j'" +alias vl="callvim -b':wincmd l'" +alias vh="callvim -b':wincmd h'" From 92227f171ad3777c2671395a9e1d7fbb86a11428 Mon Sep 17 00:00:00 2001 From: Derek Wyatt Date: Tue, 20 Mar 2012 09:55:25 -0400 Subject: [PATCH 2/5] Fixed: If you callvim on a non-existant file with a relative path, the CWD of the running gvim process is used, and that's not right. We use the PWD explicitly instead, in this case --- plugins/vim-interaction/vim-interaction.plugin.zsh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/vim-interaction/vim-interaction.plugin.zsh b/plugins/vim-interaction/vim-interaction.plugin.zsh index 3f346dfc3..309012b34 100644 --- a/plugins/vim-interaction/vim-interaction.plugin.zsh +++ b/plugins/vim-interaction/vim-interaction.plugin.zsh @@ -8,8 +8,10 @@ function resolveFile { if [ -f "$1" ]; then echo $(readlink -f "$1") + elif [[ "${1#/}" == "$1" ]]; then + echo "$(pwd)/$1" else - echo "$1" + echo $1 fi } From 04a7536641621e0188987b5bb1362e6b922a8771 Mon Sep 17 00:00:00 2001 From: Derek Wyatt Date: Fri, 30 Mar 2012 06:33:06 -0400 Subject: [PATCH 3/5] Added an optional callout to the end of the interaction function. I put it in to allow me to put the window focus on MacVim / GVim depending on the different OS I happen to be on --- plugins/vim-interaction/vim-interaction.plugin.zsh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/vim-interaction/vim-interaction.plugin.zsh b/plugins/vim-interaction/vim-interaction.plugin.zsh index 309012b34..af7e60c88 100644 --- a/plugins/vim-interaction/vim-interaction.plugin.zsh +++ b/plugins/vim-interaction/vim-interaction.plugin.zsh @@ -58,6 +58,9 @@ EOH fi cmd="$before$files$after" gvim --remote-send "$cmd" + if typeset -f postCallVim > /dev/null; then + postCallVim + fi } alias v=callvim From c2e459ad43b060c9dc2548e378a2f2d412d6da60 Mon Sep 17 00:00:00 2001 From: Derek Wyatt Date: Fri, 30 Mar 2012 06:39:49 -0400 Subject: [PATCH 4/5] Updated the README to include documentation on the postCallVim callout --- plugins/vim-interaction/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugins/vim-interaction/README.md b/plugins/vim-interaction/README.md index 65e678b51..681648018 100644 --- a/plugins/vim-interaction/README.md +++ b/plugins/vim-interaction/README.md @@ -25,6 +25,19 @@ There are a few aliases presented as well: * `vvsp` Edits the passed in file but first makes a vertical split * `vhsp` Edits the passed in file but first makes a horizontal split +## Post Callout ## + +At the end of the `callvim` function we invoke the `postCallVim` function if it +exists. If you're using MacVim, for example, you could define a function that +brings window focus to it after the file is loaded: + + function postCallVim + { + osascript -e 'tell application "MacVim" to activate' + } + +This'll be different depending on your OS / Window Manager. + ## Examples ## This will load `/tmp/myfile.scala` into the running GVim session: From 94dd6515365e0d2655485c5afc27e75a921fa6ec Mon Sep 17 00:00:00 2001 From: Derek Wyatt Date: Sun, 1 Apr 2012 15:41:39 -0400 Subject: [PATCH 5/5] Added an to the begining of everything. I ran something when I was in insert mode once and all it did was shove !args... into the buffer. first. --- plugins/vim-interaction/vim-interaction.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/vim-interaction/vim-interaction.plugin.zsh b/plugins/vim-interaction/vim-interaction.plugin.zsh index af7e60c88..5142f1f9b 100644 --- a/plugins/vim-interaction/vim-interaction.plugin.zsh +++ b/plugins/vim-interaction/vim-interaction.plugin.zsh @@ -30,7 +30,7 @@ EOH fi local cmd="" - local before="" + local before="" local after="" while getopts ":b:a:" option do