Skip to content

Instantly share code, notes, and snippets.

@raggi
Created May 29, 2012 17:04
Show Gist options
  • Save raggi/2829547 to your computer and use it in GitHub Desktop.
Save raggi/2829547 to your computer and use it in GitHub Desktop.
Vim server per-directory
#!/usr/bin/env zsh
which -s vim > /dev/null && VIM=vim
which -s gvim > /dev/null && VIM=gvim
which -s mvim > /dev/null && VIM=mvim
[[ -e /Applications/MacVim.app/Contents/MacOS/Vim ]] && VIM="/Applications/MacVim.app/Contents/MacOS/Vim"
[[ -z "${VIM}" ]] && echo "No vim found" && exit 1
# Console vim just gets a straight pass through, as there's no quick way to
# determine additional capabilities.
[[ "vim" = "${VIM}" ]] && exec $VIM $*
for arg in $*; do
# XXX - vim actually only accepts '-' as the first and last file in the file
# list for reading from stdin. This will set nofork at other times, but
# without doing a full optparse, it's not work fixing.
if [[ "-" = "${arg}" ]]; then
nofork="-f"
files=($files $arg)
fi
# If there are any args that are not files, then --remote... must be passed by
# hand, as it's not possible to know what other effects the args may have.
[[ -f $arg ]] && files=($files $arg)
done
# If launched as "vwait" then fork, but don't release the parent until the file
# is closed (good for $EDITOR, commit messages, etc.)
[[ "vwait" = $(basename $0) ]] && wait="-wait"
# Only pass files to remote-silent if all args are files, and there are files to
# edit. Special case for "-" as --remote-silent assumes it's a file (vim bug?).
if [[ "${files}" = "${*}" ]] && [[ -n $* ]] && [[ "-" != $* ]]; then
exec $VIM -g $nofork --servername $PWD --remote${wait}-tab-silent $*
else
exec $VIM -g $nofork --servername $PWD $*
fi
# Current known bugs:
# * Multiple servers can be started if no args provided.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment