4

How I can launch gvim with servername for filetype tex?

What I am looking for is something like:

au filetype tex vim --servername "SERVERNAME"

I understand that it directly not possible, as vimrc is not loaded. But is there any other way?

Right now I'm using a shell alias to do this, but I'd like to get it working as a Vim plugin.

  • Have you deleted the answer? – BaRud Jan 1 '17 at 22:42
2

Unfortunately this isn't really possible; not without some hackery anyway.

  1. --servername has to be given as a commandline parameter, it can't be set later (v:servername is read-only).
  2. 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:

fun! RestartVim()
    " 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
        if !has("gui_running")
            return
        endif

        " Close all buffers; prevents swap warnings
        bufdo bdelete

        " Run the second gvim
        execute ':!gvim --servername xxx ' . join(argv()) . ' &'

        " Quit the first Vim instance
        quit
    endif
endfun

augroup tex_restart
    autocmd!
    autocmd Filetype tex call RestartVim()
augroup end

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 vim with & 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 vim processes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.