一千萬個為什麽

搜索

強制提示是否刪除交換文件

如何配置vim以提示刪除交換文件,即使它認為另一個vim實例可能仍在使用該文件?我仍然希望看到警告消息...也可以選擇刪除文件並取消vim。

例如,下面的選項沒有(D)elete 選項。 (其中 program.cc 可能被另一個vim實例修改)

(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 program.cc"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "program.swp"
    to avoid this message.

Swap file "program.cc.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:

最佳答案

如果Vim進程仍在運行,則不顯示“刪除它”選項;我無法在任何地方找到這個文檔,但我在源代碼中查找了它;來自 memline.c ,第4512行(略微簡化):

do_dialog(
    [..]
    process_still_running
        ? (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Quit\n&Abort") :
          (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Delete it\n&Quit\n&Abort"), [..]);

交換文件嵌入創建它的進程ID,如果具有該PID的進程仍然存在,則認為該進程“正在運行”。

交換消息應顯示以下信息:

E325: ATTENTION
Found a swap file by the name "~/.vim/tmp/swap/swapy.swp"
          owned by: martin   dated: Fri Sep  8 22:13:35 2017
         file name: ~martin/swapy
          modified: no
         user name: martin   host name: arch.arp242.net
        process ID: 17355 (still running)
While opening file "swapy"
             dated: Fri Sep  8 22:13:35 2017

請註意進程ID:17355(仍在運行)行。


最可能的情況是你有另一個Vim實例在某處運行:-)如果你找不到它,你可以 kill

有一個機會,PID被另一個進程重用,在這種情況下你唯一的選擇是退出Vim,手動刪除交換文件,然後重新啟動它:

$ rm ~/.vim/tmp/swap/swapy.swp

轉載註明原文: 強制提示是否刪除交換文件