How can I restrict my other terminals from accessing one of my already opened file (within vim)?

Sometimes I forget which one of my terminals has that particular file already opened and again open it in a new different GNOME terminal. What I would like to have is, vim would warn me or simply refuse to open it, because its already openned in another GNOME terminal. I found an -e switch, but it opens a blank window and says:

Entering Ex mode. Type "visual" to go to Normal mode. which I dont know what purpose.

Any ideas how can I tell vim to open it and do not let any other applications anyway open it until I'm done finish closing it?

  • 1
    Vim does warn you (by default) when you open a file in two different instances. If it does not, it means your config prevents this behaviour. You might want to read this thread which is about a similar question. – Biggybi May 13 '20 at 15:25
  • 1
    By default, if you open a file in two vim instances, you get a message about swapfiles and have to choose how to proceed. Are you opening all files in vim? Do you have swapfiles disabled? – D. Ben Knoble May 13 '20 at 15:25
  • 2
    Then you've disabled vim's only mechanism to detect this situation... Personally, I recommend not disabling them /shrug – D. Ben Knoble May 13 '20 at 15:28
  • 1
    But it cleans them up when you exit vim (unless vim crashes or is exited "abnormally" (e.g., kill...)). You can also set directory to a spot where you want all the swap files to go, if you don't like them in the current directory (e.g., set directory ^=~/.swap//) – D. Ben Knoble May 13 '20 at 15:35
  • 1
    If having swapfiles besides your files is what bothers you, you can have them pop in a specific folder. You could also detect when a file is read and written and have vim to "refresh" it automatically. – Biggybi May 13 '20 at 15:35

In a slight frame challenge, make sure you use swapfiles. This is how vim detects simultaneous edits.

If you think the “clutter” your directories, consider doing

set directory^=~/.swap//

And mkdir ~/.swap in a shell. Then all your swap files are in this one directory.

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.