前言
使用vim的時候,檔案編輯過程中可能會出現bug,導緻非正常關閉。為了儲存剛剛修改的内容,需要對檔案進行恢複。
操作過程
1.檢視目錄檔案
zrj@zrj-ThinkPad-E470:~/work/code/facedetection/src$ ll
total 48
drwxrwxr-x 2 xxx xxx 4096 Sep 11 16:48 ./
drwxrwxr-x 6 xxx xxx 4096 Sep 11 12:23 ../
-rw-rw-r-- 1 xxx xxx 10859 Sep 11 16:48 facedet.cpp
-rw-r--r-- 1 xxx xxx 20480 Sep 11 16:43 .facedet.cpp.swp
-rw-rw-r-- 1 xxx xxx 6346 Sep 11 16:44 facedet_test.cpp
發現多了個swap檔案。
2.問題描述
E325: ATTENTION
Found a swap file by the name ".facedet.cpp.swp"
owned by: xxx dated: Tue Sep 11 16:59:21 2018
file name: ~xxx/work/code/facedetection/src/facedet.cpp
modified: no
user name: xxx host name: xxx-ThinkPad-E470
process ID: 22229 (still running)
While opening file "facedet.cpp"
dated: Tue Sep 11 16:55:31 2018
(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 facedet.cpp"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".facedet.cpp.swp"
to avoid this message.
Swap file ".facedet.cpp.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:
3.原因分析
a.檔案非正常關閉;
b.另一個Terminal也在編輯同一個檔案;
4解決方法
a.非正常關閉;
使用recover覆寫檔案,再删除swap檔案;
1. vim -r facedet.cpp
2. rm .facedet.cpp.swp
b.同時編輯同一個檔案;
先關閉一個檔案再編輯另一個,注意避免同時編輯同一個檔案;
注意,修改檔案時請注意避免同一個檔案産生兩個不同的版本。