当发生这种情况时,您还可以在 Vim 中的提示符下删除有问题的交换文件。您将会看到类似于以下内容:
E325: ATTENTION
Found a swap file by the name "~/.vim/tmp/test.txt.swp"
owned by: jim dated: Mon Nov 21 00:54:03 2016
[cannot be read]
While opening file "test.txt"
dated: Mon Nov 21 00:53:38 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 test.txt"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file "/Users/jim/.vim/tmp/test.txt.swp"
to avoid this message.
Swap file "~/.vim/tmp/test.txt.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
如果你在这里按下
D
,它将删除导致错误消息的交换文件,并且你不会再次看到该错误消息。然后你可以重新编写文件,就不会出现错误了。
与你的问题不直接相关,但你可能需要考虑更改
directory
选项,以便不要在编辑的文件所在目录中创建交换文件。我使用:
set directory=~/.vim/tmp,/var/tmp,/tmp
这将按顺序尝试使用这些目录。这有助于将交换文件保存在一个位置,因此如果在打开一堆文件时 Vim 崩溃,可以更轻松地一次性删除它们:
rm ~/.vim/tmp/*
它还可以防止交换文件进入您的Git树,而无需担心.gitignore
。