How to open files in specific Vim instance

From Vifm Wiki
Revision as of 10:47, 24 February 2015 by Xaizek (talk | contribs) (Initial version of the page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

There are some usage scenarios in which one might want to open all/some files in specific Vim instance. This can be done via Vim client-server facilities which include means to send file for opening in specific instance. Vim must be compiled with +clientserver feature.

Here only single example is described, one can think of something better/different (binding to process id, terminal multiplexer session, etc.).

How files are opened

Say you have a mapping to open file in Vim which looks like this:

nnoremap o :!gvim --remote-tab-silent %f &<cr>

Same should be applicable to 'vicmd'/'vixcmd' options. Here you see mapping, because it is handy to have different ways to open files (in the same terminal, separate terminal multiplexer window/split, gVim instance).

Amending Vim run command

To bind the command we need to pass --servername option to Vim. Let's use environment variable $VIMARGS for this and put it into Vim invocation command:

nnoremap o :!gvim $VIMARGS --remote-tab-silent %f &<cr>

Here $VIMARGS can include anything and we want to put --servername argument there.

Picking target instance

By default $VIMARGS is empty, so first found server is used by remote commands (just as usual). Setting $VIMARGS to --servername name makes all consecutive file opens to use Vim with that server name (if there is none yet, it will be created).

Setting environment variable looks like this:

let $VIMARG='--servername work'

Command to pick target

Having to type something like let $VIMARG='--servername project' each time one wants to change target instance is not very good idea. It's better to add a command which would set target. Here it is:

command! target :let $VIMARGS = '--servername "%a"'

That's it, now simply running :target project1 will cause all files to be sent to project1 instance and after :target project2 all files will be sent to project2.