As I mentioned in my comments a session is associated with a single invocation of Vim so you can't do exactly what you're hoping to do. But I also mentioned something might be possible using shell scripts and Vim's "clientserver" functionality. I decided to chase that down...primarily for my own edification but since it works I figured I'd share it. (Your version of Vim must be compiled with
+clientserver. Look at the output of
These are rudimentary scripts. My goal is just to demonstrate the concept rather than build production-grade stuff.
# For each instance of Vim save the session in a common directory,
# save the files, and then quit Vim. You can run this from one of
# the Vim instances if you like, perhaps using a key mapping.
if [[ -d $SESSDIR ]]; then
rm "$SESSDIR"/* 2> /dev/null
mkdir -p "$SESSDIR"
chmod 0777 "$SESSDIR"
for server in $(vim --serverlist); do
vim --servername "$server" \
--remote-send ':<c-u>mksession! $TMP/vimsess/%:t<cr>' \
For the most part the
--remote-send parameters are self explanatory with the possible exception of
Ctrl-U which clears the Vim command line in case some text was already present.
% represents the file path/name loaded in the current buffer.
:t is a modifier that strips off the path and leaves just the name. For example, if you're editing
/foo/bar.txt the session will be saved in
/tmp/vimsess/bar.txt. It's a quick and dirty way to choose a relatively unique name for the session file. (Remember this isn't production-grade.)
# Launch a vim session for each file in the common directory.
# The end result is to exactly restore everything closed by
# the other script.
if [[ ! -d $SESSDIR ]]; then
echo "Directory $SESSDIR not found"
for sess in "$SESSDIR"/*; do
vim -S "$sess" &
This assumes that
$TMP in Vim has value
/tmp. Update that and the directory in
SESSDIR as needed to match your own preferences and/or system.
I'm using command
vim here but that could just as well be
gvim...I tested it with GUI windows as opposed to terminal Vim (the question is about GUI vim).