37

When you're editing a file in vim, it generates a swapfile with the same name as your current file, but with a .swp extension.

If .swp is already taken, then it generates one a .swo one. If that's already taken, then you get .swa, etc. etc.

I couldn't find any documentation on what the exact naming-fallback order is for these files, can anyone clarify by what convention the extensions are chosen?

1

5 Answers 5

48

tl;dr swp, swo, ..., swa, svz, svy, ..., sva, ..., saa. Upon reaching the last one it triggers an error.

The particular piece of code that you're looking for (and comment) is in memline.c:

    /* 
     * Change the ".swp" extension to find another file that can be used. 
     * First decrement the last char: ".swo", ".swn", etc. 
     * If that still isn't enough decrement the last but one char: ".svz" 
     * Can happen when editing many "No Name" buffers. 
     */
    if (fname[n - 1] == 'a')        /* ".s?a" */
    {   
        if (fname[n - 2] == 'a')    /* ".saa": tried enough, give up */
        {   
            EMSG(_("E326: Too many swap files found"));
            vim_free(fname);
            fname = NULL;
            break;  
        }
        --fname[n - 2];             /* ".svz", ".suz", etc. */
        fname[n - 1] = 'z' + 1;
    }
    --fname[n - 1];                 /* ".swo", ".swn", etc. */
24

The information from the code snippet is in Vim's help. See :h swap-file:

The name of the swap file is normally the same as the file you are editing,
with the extension ".swp".
- On Unix, a '.' is prepended to swap file names in the same directory as the
  edited file.  This avoids that the swap file shows up in a directory
  listing.
- On MS-DOS machines and when the 'shortname' option is on, any '.' in the
  original file name is replaced with '_'.
- If this file already exists (e.g., when you are recovering from a crash) a
  warning is given and another extension is used, ".swo", ".swn", etc.
- An existing file will never be overwritten.
- The swap file is deleted as soon as Vim stops editing the file.

Technical: The replacement of '.' with '_' is done to avoid problems with
       MS-DOS compatible filesystems (e.g., crossdos, multidos).  If Vim
       is able to detect that the file is on an MS-DOS-like filesystem, a
       flag is set that has the same effect as the 'shortname' option.
       This flag is reset when you start editing another file.

                            *E326*
       If the ".swp" file name already exists, the last character is
       decremented until there is no file with that name or ".saa" is
       reached.  In the last case, no swap file is created.
1
  • Unfortunately the description in the docs is ambiguous.
    – x-yuri
    Aug 10 at 1:13
21

In, slightly easier on the eyes, regex-speak:

[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-v][a-z]
[._]sw[a-p]

The source for this is Github's own gitignore file for Vim.

4
17

good enough .gitignore

While the other answers here are clearly more technically complete here is a good enough entry for most .gitignores which is where I've cared about this most often:

# vim swap files
##################
*.sw[a-p]

As you can see from the other answers vim can create hundreds of other names, but you'd have to stack up 16 swap files before this would fail. By generalizing to something like *.s[a-z][a-z] could appear more correct it will also match lots of valid extensions which in the case of .gitignore means those files won't be tracked by git. I've never managed to create 16 swap files for the same file in 20 years of using vim so I hope you can manage to do the same and this will work for you.

stricter version

As pointed out in the comments Flash developers may have .swf files so you may prefer

*.sw[g-p]

which will still ignore 10 swap files which is plenty for most people. The only sad part is you lose the "swap" mnemonic.

11
  • 3
    Unless I'm missing something, that's only going to catch "swp" since the next one is "swo" not "swq". Personally, I use "*.sw[a-p]" for the reason you gave and also because it reads "swap" :)
    – JoL
    Nov 29, 2016 at 21:39
  • 1
    To avoid ignoring things that aren't vim swap files like .sw2 or .sw$ that should be tracked in the repo.
    – chicks
    Nov 29, 2016 at 23:39
  • 4
    Remember to include your .swf files. Or upgrade your Flash developers to HTML5 :-)
    – Jan Fabry
    Nov 30, 2016 at 11:06
  • 4
    You can avoid most legitimate files being caught by checking for the leading . or _ which is also added.
    – IMSoP
    Nov 30, 2016 at 13:39
  • 3
    I discovered the *.sw[a-p] mnemonic myself. I love it :) Nov 30, 2016 at 14:36
5

This .gitignore alternative should satisfy everyone. The second line negates ignoring '*.swf'.

*.sw[a-p]
!*.swf

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .