3

from some time now I'm using vim very intensively. I'm connecting to my server with different screen sessions, one for my main PC, another for netbooks or mobile phone, tablets, ... All this time working on the same files.

My main screen session has vim spanning my dual screens in four vsplits. Opening that with a small screen device messes everything up :\

So I open vim in another screen session, but then i get the .swp file message. For now I just edit these anyway and later clear up the .swp and .swo files but this is a bit hacky and messy.

So how to open the same file from different vim instances properly? Covim is a bit too much for me, something more basic would suffice. The basic of the basic I guess would be to have an option to automatically clear the swp files when I edit the file in another vim session and then have vim automatically refresh the file whenever it has the focus in a terminal.

8
  • Why not use vim's session files, and save and exit when you log out?
    – Herb
    Feb 26, 2017 at 19:20
  • Actually this is a good option, as I have already everything configurated in this regard with auto session saving and restoring. Perhaps I should have some quick in the butt and get the habit to close vim when I'm finishing the job. That said does it keep the vsplits dimensions ?
    – Joel.O
    Feb 27, 2017 at 8:27
  • Yes it does ! So actually this is quite a good option. Thanks !
    – Joel.O
    Feb 27, 2017 at 8:30
  • @Joel.O It sounds like vim's session files address your question. Have you considered writing up session files as your own answer to your question and marking this question as Answered? That would take your question off the Unanswered queue. Thanks! Mar 28, 2017 at 22:28
  • have you tried :set noswf?
    – rbernabe
    Mar 29, 2017 at 17:32

2 Answers 2

1

Vim developers and users have been debating on methods to deal with the swap file and multiple Vim processes for a long time. Some plugins did come out of it.

The plugin that I use for that is vim-autoswap, but I do make changes to its code. When you do :w Vim first updates the swap file and then the file, e.g.

$ echo yay > dummy
$ vim dummy
:w
:!ls -a --fulltime *dummy
-rw-r--r-- 1 grochmal users     4 2017-02-26 21:19:52.995699920 +0000 dummy
-rw-r--r-- 1 grochmal users 12288 2017-02-26 21:19:47.819084930 +0000 .dummy.swp

Therefore the swap file is older than the file you're editing when you perform the write (:w). And the vim-autoswap plugin will delete a swap file older than the file itself.

On the other hand, the plugin would not be able to figure out that the swap file is older in some cases. For example, in a second test where I perform the :w quickly after opening the file I get the following:

-rw-r--r-- 1 grochmal users     4 2017-02-26 21:22:14.144290852 +0000 dummy
-rw-r--r-- 1 grochmal users 12288 2017-02-26 21:22:14.104291252 +0000 .dummy.swp

The swap file is older alright, by 0.4 seconds. But Vim's getftime() can only deal with seconds, so vim-autoswap will fail to figure out that the swap is older.

What I do to deal with that case is that I change line 61 in the plugin from:

elseif getftime(v:swapname) < getftime(a:filename)

to:

elseif getftime(v:swapname) <= getftime(a:filename)

And then I do:

$ echo yay > dummy
$ vim dummy
:w
<Ctrl+Z>
$ vim dummy

Vim (with the plugin) gives me a warning message saying:

Old swapfile detected... and deleted

But if it takes me more than one second making changes to the file before giving the SIGSTOP (Ctrl+Z) then the plugin discovers the that the swap was updates and tells me:

Swapfile detected, opening read-only

So I know that it is very likely that I have unwritten changes in the other process.

1

One convenient way is to use vim's built-in session files. Before exiting vim, use the command :mksession project.vis, or :mks! to save to the default session.vim file.

Then, to resume editing, start vim with the command vim -S project.vis, or start vim and load the session file with the :source command.

From the online documentation:

A Session keeps the Views for all windows, plus the global settings.  You can
save a Session and when you restore it later the window layout looks the same.
You can use a Session to quickly switch between different projects,
automatically loading the files you were last working on in that project.

Views and Sessions are a nice addition to viminfo-files, which are used to
remember information for all Views and Sessions together.

Additionally, there are several plugins available for working with sessions.

1
  • The OP could keep one screen/tmux/vim session running 24/7. He could then customize his views/workarea once for each device and then use :mksession thisdevice.vis to save it. Thereafter, whenever he changes devices just do :source thisdevice.vis
    – Jim U
    May 3, 2017 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.