Is there a way to let gVim only run a single instance, so that when a new file is opened with it it\'s automatically opened in a new tab in the currently running instance?
I'm using TotalCommander for file browsing. It lets you press "F4" to edit the file. So in the "Editor for F4" option window, you just need to do the following:
C:\Program Files (x86)\Vim\vim73\gvim.exe --remote-tab-silent
Works on my win7 64bit machine.
If you are using the bash shell (on Linux/OS X/using Cygwin) is to add you ~/.bashrc
file:
gvim () { command gvim --remote-silent "$@" || command gvim "$@"; }
On Windows I think you could have a gvim.bat
batch-script to achieve the same..
gvim.exe -p --remote-tab-silent %1 %*
If gvim.exe isn't in your path
Run > Search "Environment"
Edit PATH var for current user or system.
The solutions above don't launch the gvim server on first execution, so I use:
ANS=`pgrep -fx "$VIM"`
# Launch server if needed
if [[ ! $ANS ]]; then
$VIM
fi
# Now open the file
if [[ $1 ]]; then
$VIM --remote-tab "$@"
fi
modified from https://stackoverflow.com/a/15335004/214686
I got a little tired of working around this in cygwin + windows so I finally did something about it. I started with the wrapper script defined above but I wound up making it a lot more robust and multi-env capable for *nix and Win.
#!/bin/bash
#bash wrapper for windows/cygwin gvim
#####################################################################
## Cygwin/*nix and Windows gvim wrapper script, alias friendly, path friendly
## Author: Matt Gregory (skyleach (AT) geemale (dot) com)
## Version: 1.5
## Date: Thu Jun 12 10:02:54 2014
## Known Bugs:
## Changes:
## Thu Jun 12 10:04:08 2014 : Initital posting to StackOverflow
#####################################################################
[[ -z ${WINVIM} ]] && WINVIM=true
[[ -z ${VIMRUN} ]] && export VIMRUN='' #scoping
if [[ ${WINVIM} = false ]]; then
[[ ! ${VIMRUN} ]] && VIMRUN='/bin/gvim'
ANS=$("${VIMRUN}" --serverlist | grep GVIM)
else
[[ ! "${VIMRUN}" ]] && VIMRUN='/cygdrive/c/Program Files/vim/vim74/gvim'
ANS=$(ps -Wsl | grep "${VIMRUN}" | sed -e "s/\s\+\([0-9]\+\).*/\1/g")
fi
[[ ! -z ${VIM} && ${WINVIM} = true ]] && export VIM=$(cygpath -wal "${VIM}")
RT="--remote-tab"
[[ $ANS ]] || unset RT
if [ ! -z ${DEBUG} ]; then
echo "WINVIM: ${WINVIM}"
echo "VIMRUN: ${VIMRUN}"
echo "ANS: ${ANS}"
echo "VIM: ${VIM}"
fi
#process arguments or stdin
if [ ${#} -ne 0 ]; then
[[ ! -z ${DEBUG} ]] && echo "Got arguments [${#}]:'${@}'"
for OFILE in "${@}"; do # if [ -f "${OFILE}" ] || [ -d "${OFILE}" ]; then
[[ -h ${OFILE} ]] && OFILE="$(readlink -f "${OFILE}")"
[[ ${WINVIM} == true ]] && OFILE=$(cygpath -wal "${OFILE}")
echo "\"${VIMRUN}\" --servername GVIM $RT \"${OFILE}\""
"${VIMRUN}" --servername GVIM $RT "${OFILE}" &
if [[ -z ${RT} ]] || [[ ! "${RT}" ]]; then
RT="--remote-tab"
sleep 5 #give gvim time to start up, running too fast messes things up
else
sleep .3 #shorter sleep for tabs
fi
#fi
done
else
while read OFILE; do
[[ -h ${OFILE} ]] && OFILE="$(readlink -f "${OFILE}")"
[[ ${WINVIM} == true ]] && OFILE=$(cygpath -wal "${OFILE}")
echo "\"${VIMRUN}\" --servername GVIM $RT \"${OFILE}\""
"${VIMRUN}" --servername GVIM $RT "${OFILE}" &
if [[ -z ${RT} ]] || [[ ! "${RT}" ]]; then
RT="--remote-tab"
sleep 3 #give gvim time to start up, running too fast messes things up
else
sleep .3 #shorter sleep for tabs
fi
done
fi
# vim: set filetype=sh:
How to use it effectively:
alias some command to cygwin and/or windows gvim like so:
echo "alias gwrap='WINVIM=false ~/src/localscripts/wgwrap'" >> ~/.bashrc echo "alias wgvim='wgwrap'" >> ~/.bashrc
NOTE: If the hard-coded paths to gvim are incorrect for your system you can edit the script directly, the alias(s) and/or add the environment variables WINVIM and/or VIMRUN. You can set them in the alias as I do above for gwrap or you can add them to your .bashrc or Windows system environment.
gvimt () { [ $# -ne 0 ] && command gvim --remote-silent-tab $@ || command gvim $@; }
Using MacVim, I discovered the default servername was simply VIM. Sticking with that theme, I threw the following function in my bashrc and it works like a charm:
mvim() { ~/bin/mvim --servername VIM --remote-tab-wait-silent $* & }