有没有一种方法可以禁用vim的交换文件警告?

3
每当我运行
git commit --amend

我认为其他一些命令也是如此

在编辑提交消息之前,我从vim中收到一个警告,大致意思是:

E325: ATTENTION
Found a swap file by the name "~/Desktop/code/web/.git/.COMMIT_EDITMSG.swp"

我总是忽略警告,只是写下我的提交信息并保存文件。有没有一种方法可以永久禁用这个警告?


3
只需删除交换文件即可... - Eugene Sh.
3
删除交换文件可以阻止警告弹出。 - carol Alex
啊,谢谢!我没意识到这个问题,问题解决了 :) - maxfowler
4个回答

4
删除交换文件可以解决该警告:
rm ~/Desktop/code/web/.git/.COMMIT_EDITMSG.swp

2
将以下内容添加到您的.vimrc文件中。
" Don't let Vim's "Found a swap file" message block input
set shortmess=A

永远不再担心烦人的交换文件消息!

如果我这样做,如果有“(R) recover”选项,我仍然可以通过swp恢复未保存的进度吗? - vhoyer
无法在 MacVim 8.2.2164 (169) 上运行。仍会弹出“交换文件已存在”的提示。 - user674669

1
如果您正在使用vim/gvim编辑器编辑文件,则需要在您的.vimrc/.gvimrc文件中设置以下命令,以避免生成交换文件。
set noswapfile

1

当发生这种情况时,您还可以在 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接