1

I'm using the server mode in vim and already have an instance open with a couple of files in. I'd like to add the contents of a session file to this.

However doing vim --servername vim -S mySessionFile ends up opening it in a new server (VIM1 - from doing :echo v:servername).

I also tried vim --servername vim --remote-silent -S .\vimSession which opened in the correct instance but perhaps not surprisingly just added two new buffers for the final two arguments.

This also doesnt work:vim --servername vim -S .\vimSession --remote-silent! It complains of missing arguments (its expecting a file list), and if you add one in then it just ignores the session parameter and gives a blank buffer with the name

Is this possible?

3
  • 1
    A session saved with :mksession is a whole session. If there was a way to load a session into an active session it would replace existing buffers and do all kinds of things that would mess it up. Sessions are only meant to be used with the -S argument at startup.
    – romainl
    Commented May 14, 2015 at 10:00
  • @romainl ah thats a shame. Is there any other method of storing a list of open files? That's the main thing I'm interested in
    – JonnyRaa
    Commented May 14, 2015 at 11:04
  • @JonnyLeeds It probably wouldn't be too hard to write a plugin to import & export the list of open files. All you'd need to do to restore is to do a :e on every file. If you also want to restore paste registers and cursor position, etc, then you might write a wrapper around :mkview which loops over every window to export a view for it.
    – jpaugh
    Commented Jul 17, 2017 at 19:18

1 Answer 1

1

Quick and dirty way of "storing a list of open files", as requested in your comment. (I don't have enough reputation to answer a comment).

:tabs

gives all currently open filenames, arranged by tab number.

So if you do something like

 command! SaveTabs :execute "redir! > tabs_" . v:servername . ".txt 
     \ | echo v:servername strftime(\"%c\") | pwd | tabs | redir END"

you will have a new command

:SaveTabs

which will create a file named something like tabs_GVIM27.txt (yeah that's how many servers I have open...) containing all the filenames, with a "header" at the top with the:

  • servername
  • current working directory
  • date/time that the list was created.

When in a new/different session later on, you can open the tabs file in a window and <c-w>f on any of those filenames to open a new split on the file, effectively "importing" it into your current session.


Variations:

  • If you want you could put the date/time in the filename itself, see :help strftime()
  • Put the list on the clipboard instead of in a file: use redir @+ instead of redir! >

Other explanatory notes:

  • The working directory is because :tabs shows relative paths, so files in the working directory do not have their paths (but others do)
  • :execute is needed because :redir won't allow variables in its argument
  • The command doesn't have to be split over two lines, I just did that so it would be easier to read here

Insane variation for importing all the files at once:

If you want to import all the files at once you can probably do something like

grep "^  " tabs_OLD.txt | xargs | vim --servername NEW --remote-silent

(untested but you get the idea)

(actually if you got that working, you could then just define it as an alias in your shell, so you could in future just type

addfiles foo bar

to add all the files from a previous vim server "foo" to a currently-running vim server called "bar")

2
  • You mentioned 27 Vim servers running. I've never run Vim server. Would you offer some insight to your workflow using Vim servers. Comparatively I use Vim with TMUX, opening files is a matter of TMUX copy/paste and if in a GUI environment (open Vim from gui app) it spawns a MacVIM instance. Would be cool if it went to my running terminal Vim instance instead. Then again most external editor implementations monitor when the process exits before thinking the file is completed. But the server idea might be nice in things like IDE's which have crappy editors built in and need Vim to be effective.
    – Sukima
    Commented Jun 4, 2015 at 12:31
  • @Sukima I've heard that MacVIM runs a server by default, so if you give your tmux'd vim the right command-line options, it might connect to it.
    – jpaugh
    Commented Jul 17, 2017 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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