Vim:所有可能的交换文件扩展名是什么?


32

在vim中编辑文件时,它会生成一个交换文件,其名称与当前文件相同,但具有.swp扩展名。

如果.swp已经采取,则将生成一个一个.swo一个。如果已经采取,那么您将获得.swa,等等。

我找不到有关这些文件的确切命名-后备顺序是什么的任何文档,任何人都可以通过选择什么约定来澄清扩展名吗?


另请参阅:Vi和Vim

Answers:


44

您要查找(和注释)的特定代码在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

从代码片段的信息 Vim的帮助。见: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.


9

够好了 .gitignore

尽管这里的其他答案在技术上显然更完整,但是对于大多数s来说,这是一个足够好的输入,.gitignore这是我最常关心的地方:

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

从其他答案中可以看到,vim可以创建数百个其他名称,但是您必须堆叠16个交换文件,否则该文件将失败。通过归纳为*.s[a-z][a-z]可能看起来更正确的东西,它也将与许多有效的扩展名匹配,在这种情况下,.gitignore意味着这些文件不会被跟踪git。在使用20年中,我从未设法为同一文件创建16个交换文件,vim因此我希望您能够做到这一点,并且对您有用。

更严格的版本

正如评论中指出的那样,Flash开发人员可能拥有.swf文件,因此您可能更喜欢

*.sw[g-p]

仍然会忽略10个交换文件,这对大多数人来说已经足够了。唯一可悲的是您丢失了“交换”助记符。


2
除非我缺少任何东西,否则只会捕获“ swp”,因为下一个是“ swo”而不是“ swq”。就您个人而言,我个人使用“ * .sw [ap]”,也因为它显示为“ swap” :)
JoL

1
为了避免忽略不是vim交换文件之类的东西,.sw2或者.sw$应该在存储库中进行跟踪。
小鸡

2
记住要包括您的.swf文件。或将您的Flash开发人员升级到HTML5 :-)
Jan Fabry

3
您可以通过检查开头._添加的开头来避免捕获大多数合法文件。
IMSoP '16

2
*.sw[a-p]自己发现了助记符。我喜欢它:)
韦恩·沃纳

1

这个.gitignore替代方案应该使所有人满意。第二行忽略忽略“ * .swf”。

*.sw[a-p]
!*.swf
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.