Unfortunately this isn't really possible; not without some hackery anyway.
--servername has to be given as a commandline parameter, it can't be set later (
v:servername is read-only).
- Vim has no facility to restart itself with different parameters (e.g. like
exec() function family in C).
The best you can do it to start Gvim from within Gvim with:
:execute ':!gvim --servername xxx ' . join(argv())
This has the problem of having two Gvim processes though (and thus Gvim windows); you'll will have to
:q twice and the second Gvim will complain about swap files if the files are open in the first Gvim.
You can somewhat work around with with something like:
" Only run this if servername is empty or the default
if v:servername == '' || v:servername =~ '^gvim'
" Only run for gvim, since this version doesn't work well with Vim
" Close all buffers; prevents swap warnings
" Run the second gvim
execute ':!gvim --servername xxx ' . join(argv()) . ' &'
" Quit the first Vim instance
autocmd Filetype tex call RestartVim()
I didn't extensively test this, and will probably have a caveat or two.
This version also won't work very well with regular Vim inside a terminal. Running
& won't work well since it won't be attached to the terminal, and I can't really think of a solution to prevent two