1

When a file is being opened neovim warns that swap file already exists, at which point you can't safely open a file without first closing it in the other instance.

I frequently have many projects open in different instances, which sometimes use shared files. It is difficult to find which instance is using the file I want to open (especially if refactoring tools open them in the background).

Is it possible to close the buffer in the other instance from the active one?

  • 1
    Maybe you'll want to just disable swapfiles: :h noswapfile – Biggybi May 6 at 12:46
  • What is the downside of disabling them? – Rizhiy May 6 at 12:47
  • Well, you won't have a backup if vim crashes and you have unsaved changes. You could lose changes made in another instance, too. That's when autowrite comes to help. There're others, more reliable ways to autosave files (check this post). Then, you can autoread so you'll load changes made to your files. – Biggybi May 6 at 12:52
  • 1
    The swapfile is your protection from parallel editing the same file in multiple instances. Also it is useful to recover after a crash. In short: Think twice before disabling! Read `:h 'swapfile' and ':h swap-file'. – Ralf May 6 at 12:52
  • 3
    One might be able to reuse the editexisting plugin, but instead of opening the existing instance, close the buffer there. Note, this relies on remote_expr() functions, thus does not work for Neovim (who patched that out I think). – Christian Brabandt May 6 at 14:05
2

Since your question is about saving files, here's how I do it.

Vim files out of my way

if exists('$SUDO_USER')  " no files for root user
    set noswapfile
    set nobackup
    set nowritebackup
    set noundofile
    set viminfo=
else                     " I created a tmp folder
    set backupdir=$HOME/.vim/tmp/backup//
    set directory=$HOME/.vim/tmp/swap//
    set undodir=$HOME/.vim/tmp/undo//
    set viewdir=$HOME/.vim/tmp/view//
    set viminfo+=n$HOME/.vim/tmp/viminfo
endif

The // is to avoid conflicts: it will create a file based on the path, so 2 files named the same can't conflict here.

Auto write AND read

set autoread
set autowriteall

Save when focus is lost, load when focus is gained

A complement for autowriteall and autoread.

augroup FocusAutoReadWrite
    au!
    au FocusGained,BufEnter * :silent! !
    au FocusLost,WinLeave * :silent! w
augroup end

GTK vim and Neovim handle it by default (:h :FocusGained).

In regular terminal vim, a compatible terminal is needed to get focus events. I use it with tmux and this extension.

Sorry for telling you to disable your swap files. I thought that's what I do, but I don't. And I don't think you should do it when you can set them up properly...

| improve this answer | |
0

I decided to use:

set noswapfile
set autowriteall

All of my important work is under git, so recovery is not a problem and while multiple-edits might be a problem, I guess I will just have to be careful.

| improve this answer | |

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.