Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving a buffer in vim across instances of vim

Tags:

vim

unix

Is this possible to do?

Conceptually, a solution should apply across a lot of possible configurations, ranging from two vim instances running in separate virtual terminals in panes in a tmux window, to being in separate terminals on separate machines in separate geographical regions, one or both connected over network (in other words, the vims are hosted by two separate shell processes, which they would already be under tmux anyhow).

The case that prompted me to ponder this:

I have two tmux panels both with vim open and I want to use the Vim yank/paste to copy across the files.

But it only works if I've got them both running in the same instance of Vim, so I am forced to either:

  1. use tmux's copy/paste feature to get the content over (which is somewhat tedious and finicky), or
  2. use the terminal (PuTTY, iTerm2)'s copy/paste feature to get the content over (which is similarly tedious but not subject to network latency, however this only works up to a certain size of text payload to copy at which point this method will not work at all due to the terminal not knowing the contents of the not-currently-visible parts of the file), or
  3. lose Vim buffer history/context and possibly shell history/context in reopening the file manually in one of the Vim instances in either a split buffer or tab and then closing the other terminal context (much less tedious than 1 for large payloads but more so with small payloads).

This is a bit of a PITA and could all be avoided if I have the foresight of switching to an appropriate terminal already running vim to open my files but the destiny of workflow and habit rarely match up with that which would have been convenient.

So the question is, does there exist a command or the possibility of a straighforwardly-constructed (shell) script that allows me to join buffers across independently running vim instances? Am having a hard time getting Google to answer that adequately.

In the absence of an adequate answer (or if it is determined with reasonable certainty that Vim does not possess the features to accomplish the transfer of buffers across its instances), a good implementation (bindable to keys) for approach 3 above is acceptable.

Meanwhile I'll go back to customizing my vim config further and forcing myself to use as few instances of vim as possible.

like image 585
Steven Lu Avatar asked Mar 16 '13 02:03

Steven Lu


People also ask

How do I navigate buffers in Vim?

Pressing Alt-F12 opens a window listing the buffers, and you can press Enter on a buffer name to go to that buffer. Or, press F12 (next) or Shift-F12 (previous) to cycle through the buffers.

How do I close a single buffer in Vim?

Close buffer named Name (as shown by :ls ). Assuming the default backslash leader key, you can also press \bd to close (delete) the buffer in the current window (same as :Bclose ).

How does the vi editor use buffers?

Whenever you delete something from a file, vi keeps a copy in a temporary file called the general buffer. You can also delete or copy lines into temporary files called named buffers that will let you reuse those lines during your current vi work session.


1 Answers

No, Vim can't share a session between multiple instances. This is how it's designed and it doesn't provide any session-sharing facility. Registers, on-the-fly mappings/settings, command history, etc. are local to a Vim session and you can't realistically do anything about that.


But your title is a bit misleading: you wrote "buffer" but it looks like you are only after copying/pasting (which involves "register", not "buffers") from one Vim instance to another. Is that right? If so, why don't you simply get yourself a proper build with clipboard support?

Copying/yanking across instances is as easy as "+y in one and "+p in another.

Obviously, this won't work if your Vim instances are on different systems. In such a situation, "+y in the source Vim and system-provided paste in the destination Vim (possibly with :set paste) is the most common solution.


If you are on a Mac, install MacVim and move the accompanying mvim shell script somewhere in your path. You can use the MacVim executable in your terminal with mvim -v.

If you are on Linux, install the vim-gnome package from your package manager.

If you are on Windows, install the latest "Vim without Cream".


But the whole thing looks like an XY problem to me. Using Vim's built-in :e[dit] command efficiently is probably the best solution to what appears to be your underlying problem: editing many files from many different shells.

like image 89
romainl Avatar answered Sep 28 '22 14:09

romainl