32

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?

44

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. */
22

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.
17

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.

9

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.

  • 2
    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 '16 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 '16 at 23:39
  • 2
    Remember to include your .swf files. Or upgrade your Flash developers to HTML5 :-) – Jan Fabry Nov 30 '16 at 11:06
  • 3
    You can avoid most legitimate files being caught by checking for the leading . or _ which is also added. – IMSoP Nov 30 '16 at 13:39
  • 2
    I discovered the *.sw[a-p] mnemonic myself. I love it :) – Wayne Werner Nov 30 '16 at 14:36
1

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

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

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.