I am interested in having a single file open with vim in two shells at once. The use case I have in mind is two windows of gnu screen running on a tty.

The catch is that I would like the instances of vim to be coordinated in the sense that edits in one window manifest instantly in each window. I would also like different cursor positions in each window, so something like just having two screen windows displaying the same shell is not what I am looking for.

For example, I would like to be able to be in edit mode on line 1 in my first window, and also in edit mode on line 200 in my second window. I would like edits from one window to immediately manifest in the other window, and I would like a save action in one window to save all changes that have been made in each window.

Is anything like this possible? If not with vim, with another editor?

  • 1
    You can do it in one emacs session. Oct 26 '19 at 23:49
  • 1
    I do this using vile (vi like emacs) Oct 27 '19 at 1:09
  • 1
    You can do this with just plain vim, with the same buffer open in two windows. I do this all the time. Would that work or is it important that they be two distinct programs running under different shells, sharing data underneath?
    – trentcl
    Oct 27 '19 at 1:30
  • 2
    Use :split in Vim to get two separate views of the same file, and learn about Vim's window management (:help windows).
    – AlexP
    Oct 27 '19 at 8:43
  • 1
    rastafile mentioned it in their answer. I also found a few webpages that offer holistic explanations of using buffers with windows: 1 and 2. I usually edit a dozen or more files in a single gvim instance, divided among 1-3 windows according to whatever I currently need to see, and I use :b file-name-or-fragment to switch between buffers. :set hidden is really nice for this. In gvim, besides using ^W, you can also click on windows to switch between them.
    – trentcl
    Oct 27 '19 at 11:45

You should really differentiate between opening the file twice or just displaying it's contents twice.

Opened separately (in two shells / xterms) the coordination is via :e! etc. in vim, but it is not practical and not what you want.

If you just :sp in vim, you get a instant duplication; but this is not "two shells".

And now, if you "split" (or multiplex) your shell, you will get something in between. It is quite impossible to implement what you want, without a additional layer. That would be a database system.

  • I wouldn't say that opening the same file in two Vim instances is impractical, with :set autoread and some swapfile management this is possible (though not totally concurrent and instantaneous, as OP has asked for). I do this all the time, but with Vim + IDE. Oct 28 '19 at 9:15
  • @IngoKarkat good remark, but this is a special situation (vim + IDE). Q wants to "run two vims at once on a single file". I guess you have one vim for editing and another for compiling? How do you prevent making changes in both? I don't even understand autoread: seems like I still get warnings if a modified file has been modified outside, and I have to make up my mind if I want :w! or :e! (so I loose either this or that).
    – user373503
    Oct 28 '19 at 11:50
  • This will work if each edit is saved when focus is left. In Vim, you can :autocmd FocusLost * write (but saving often is second nature for me, so I don't have this), and many IDEs (like IntelliJ IDEA) do this out of the box. Yes, if you do concurrent modifications somehow, there's a conflict, but even then it's not a matter of :w! vs. :e!, but rather I use a custom :DiffToSaved command to merge both inside Vim. Oct 28 '19 at 12:10

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.