Advanced C Programming

Spring 2023 ECE 264 :: Purdue University

Alchemy details (also sent last week)

Vim swap file warnings, and how to get rid of them

Vim might occasionally give you a warning about a swap file when you start up. Two examples are at the bottom.

About swap files. These swap files help you recover in case you lose your connection (e.g., dropped wifi). They also protect you from trying to edit the same file from two places at the same time. When you open Vim, it creates the swap file (e.g., .after.c.swp). As you type, it records all of your edits, even before you save. When you close Vim properly, it deletes the swap file. If you lose your connection, it will find the file and let you know that you can use it to recover your unsaved work. If you try to open the file from two places at once, it finds the file and gives you the same warning.

What to do. To deal with the warning:

  1. Close the file elsewhere (if open).  Make sure you don't have the file open in another window, or elsewhere. In particular, if there is no (D)elete option—see the examples below—then it means Vim thinks it is still open in another window or from another computer, though that is occasionally wrong.
  2. Recover.  Press r for the (R)ecover option.
  3. Save (probably) and reopen. First, check the contents. If the contents looks right, then save (:w<enter>) and reopen (:e<enter>). However, it looks like an old version, then reopen the file without saving (:r!<enter>). Notice the exclamation point.
  4. Delete the swap file.  From step 3, you should now be seeing the warning screen again. Press d to choose the (D)elete option, assuming that option is available.
    1. If there is no (D)elete option…  This means that Vim thinks you have the file open elsewhere. First, triple-check that it is not open anywhere else. If you're sure, then just delete the swap file manually from bash. Make a note of the swap file name (e.g., .after.c.swp), exit vim, type rm .after.c.swp (for example), and re-open Vim.

At this point, the swap file should be gone and you can continue editing as normal. You shouldn't get it again.

What causes it. The following cause this warning to appear:

Avoiding those conditions should prevent the error from occurring in the first place.


Example #1: Usual case with (D)elete option

E325: ATTENTION
Found a swap file by the name ".after.c.swp"
          owned by: aq   dated: Wed Aug 24 17:03:38 2016
         file name: ~aq/264/hw01/after.c
          modified: no
         user name: aq   host name: ecegrid-thin8.ecn.purdue.edu
        process ID: 15080
While opening file "after.c"
             dated: Wed Aug 24 14:34:37 2016

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r after.c"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file ".after.c.swp"
    to avoid this message.

Swap file ".after.c.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:

Example #2: No (D)elete option

This happens if Vim detects that another instance of Vim is running with that file open. However, that detection is imperfect.

E325: ATTENTION
Found a swap file by the name ".after.c.swp"
          owned by: aq   dated: Wed Aug 24 16:31:22 2016
         file name: ~aq/264/hw01/after.c
          modified: no
         user name: aq   host name: ecegrid-thin8.ecn.purdue.edu
        process ID: 12637 (still running)
While opening file "after.c"
             dated: Wed Aug 24 14:34:37 2016

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r after.c"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file ".after.c.swp"
    to avoid this message.

Swap file ".after.c.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort: