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 :version
.)
These are rudimentary scripts. My goal is just to demonstrate the concept rather than build production-grade stuff.
save_vim_sessions.sh
#!/bin/bash
# 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.
SESSDIR=/tmp/vimsess
if [[ -d $SESSDIR ]]; then
rm "$SESSDIR"/* 2> /dev/null
else
mkdir -p "$SESSDIR"
chmod 0777 "$SESSDIR"
fi
for server in $(vim --serverlist); do
vim --servername "$server" \
--remote-send ':<c-u>mksession! $TMP/vimsess/%:t<cr>' \
--remote-send ':wqa<cr>'
done
For the most part the --remote-send
parameters are self explanatory with the possible exception of <c-u>
and %:t
. <c-u>
sends 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_vim_sessions.sh
#!/bin/bash
# Launch a vim session for each file in the common directory.
# The end result is to exactly restore everything closed by
# the other script.
SESSDIR=/tmp/vimsess
if [[ ! -d $SESSDIR ]]; then
echo "Directory $SESSDIR not found"
exit 1
fi
for sess in "$SESSDIR"/*; do
vim -S "$sess" &
done
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).
That's it.
:split
)?:mksession
only works on one session at a time.