Difference between revisions of "How to open files in specific Vim instance"

From Vifm Wiki
Jump to navigation Jump to search
(Initial version of the page)
m (Xaizek moved page How to send all files to specific Vim instance to How to open files in specific Vim instance without leaving a redirect: Better wording in the title)
(No difference)

Revision as of 10:49, 24 February 2015

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.