Skip to content

Instantly share code, notes, and snippets.

@xolox
Created September 25, 2010 19:10
Show Gist options
  • Save xolox/597172 to your computer and use it in GitHub Desktop.
Save xolox/597172 to your computer and use it in GitHub Desktop.
Make graphical Vim use one instance where possible
#!/bin/sh
# This UNIX shell script makes graphical Vim use one instance where possible.
# Save this script as ~/bin/gvim, make sure it's executable and you're off! If
# it doesn't work uncomment the lines with XXX in them and run the script from
# a console.
# Configuration variables.
GVIM=/usr/local/bin/gvim
WMCTRL=/usr/bin/wmctrl # <- try `sudo apt-get install wmctrl'
VIM_WINDOW_CLASS=gvim.Gvim
# Look for command-line arguments that aren't filenames and
# if one is found then fall back to Vim's regular behavior.
for ARG in "$@"; do
if echo "$ARG" | grep -q '^note:'; then
# Don't consider "note:..." as non-file argument (this is a hack to
# make my unreleased note taking plug-in work with this script).
continue
elif [ ! -f "$ARG" ]; then
# Replace the wrapper with Vim itself.
# XXX echo "Process $$: Executing $GVIM $@" >&2
exec $GVIM "$@"
exit # <- shouldn't be reached!
fi
done
# Check if Vim is already running.
if ! pidof $GVIM > /dev/null; then
# It's not: Open file(s) in new instance.
# XXX echo "Process $$: Executing $GVIM -p $@" >&2
exec "$GVIM" -p "$@"
else
# XXX echo "Process $$: Requesting server list" >&2
$GVIM --serverlist | grep '^GVIM\d*$' | while read VIM_SERVER_NAME; do
# XXX echo "Process $$: Raising selected Vim server" >&2
$WMCTRL -a $VIM_SERVER_NAME
# Pass any pathname arguments on to Vim?
if [ $# -ge 1 ]; then
# Open files on command-line in existing Vim window.
# XXX echo "Process $$: Executing $GVIM --servername '$VIM_SERVER_NAME' --remote-tab-silent $@" >&2
exec $GVIM --servername "$VIM_SERVER_NAME" --remote-tab-silent "$@"
exit # <- shouldn't be reached!
fi
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment