使用VIM保存文件时会发生什么?

时间:2017-01-25 03:20:31

标签: linux file vim filesystems

我使用一些文件系统更改通知库来查看目录/asdf,并在asdf内{I} vim tmp,并进行了一些更改,然后使用:wq来保存文件

然后我得到了这个输出:

/asdf/4913 at watch.pl line 9.
/asdf/4913 at watch.pl line 9.
/asdf/tmp at watch.pl line 9.
/asdf/tmp at watch.pl line 9.
/asdf/tmp at watch.pl line 9.
/asdf/tmp~ at watch.pl line 9.
/asdf/tmp~ at watch.pl line 9.

4913文件是什么?什么是tmp~文件? :wq之后究竟发生了什么?

1 个答案:

答案 0 :(得分:0)

来自https://github.com/neovim/neovim/issues/3460

  

提到了一个有趣的案例:Neo / Vim创建一个临时文件来检查目录是否可写并查看生成的ACL。

     

因此,如果您正在编写用于监视文件更改的软件,您会发现Vim几乎在每次编辑时都会创建并删除文件4913。参考

其他细节 https://groups.google.com/forum/#!topic/vim_dev/sppdpElxY44
https://vi.stackexchange.com/questions/4038/why-does-set-nocompatible-result-in-vim-saving-extra-all-numeric-temporary-fi

这是导致此

的代码
/*
 * Check if we can create a file and set the owner/group to
 * the ones from the original file.
 * First find a file name that doesn't exist yet (use some
 * arbitrary numbers).
 */
STRCPY(IObuff, fname);
for (i = 4913; ; i += 123)
{
    sprintf((char *)gettail(IObuff), "%d", i);
    if (mch_lstat((char *)IObuff, &st) < 0)
        break;
}
fd = mch_open((char *)IObuff,
                O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
if (fd < 0)     /* can't write in directory */
    backup_copy = TRUE;
else
{

    ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
    if (mch_stat((char *)IObuff, &st) < 0
        || st.st_uid != st_old.st_uid
        || st.st_gid != st_old.st_gid
        || (long)st.st_mode != perm)
    backup_copy = TRUE;
    /* Close the file before removing it, on MS-Windows we
     * can't delete an open file. */
    close(fd);
    mch_remove(IObuff);