预计阅读时间:6分钟
《》编写过程中,曾碰见一个问题,算是个小知识点,模拟操作如下。
一个session用vi打开文件,但不退出,或者直接关闭session,造成异常退出的场景,
vi test.txt
另一个session打开相同的文件,会有如下提示,直接回车,可以打开,但是是以只读方式打开的,
vi test.txt
E325: ATTENTION
Found a swap file by the name ".test.txt.swp"
owned by: oracle dated: Tue Jul 3 14:36:01 2018
file name: ~oracle/test.txt
modified: no
user name: oracle host name: vm-kvm12907-app
process ID: 30834 (still running)
While opening file "test.txt"
dated: Tue Jul 3 14:35:53 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 test.txt"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".test.txt.swp"
to avoid this message.
Swap file ".test.txt.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:
查看当前路径,会有个叫.test.txt.swp的隐藏文件,
ls -al
total 192
-rw-r--r-- 1 oracle oinstall 2 Jul 3 14:35 test.txt
-rw-r--r-- 1 oracle oinstall 12288 Jul 3 14:36 .test.txt.swp
其实这是因为,使用vi/vim编辑文件实际是先copy一份临时文件并映射到内存给你编辑,编辑的是临时文件,当执行:w后才保存临时文件到原文件,执行:q后才删除临时文件。每次启动编辑时都会检索这个文件是否已经存在临时文件, 有则询问如何处理,即上面截图信息。
解决方法1
删除临时文件,就可以直接用了。
rm *.swp
解决方法2
使用vi -r选项,回车,可以打开和编辑文件,:w保存即可,
vi -r test.txt
Using swap file ".test.txt.swp"
Original file "~/test.txt"
Recovery completed. You should check if everything is OK.
(You might want to write out this file under another name
and run diff with the original file to check for changes)
Delete the .swp file afterwards.
Press ENTER or type command to continue
关于-r选项,可以接文件名,或者不接文件名,
-r List swap files, with information about using them for recovery.
-r {file} Recovery mode.
The swap file is used to recover a crashed editing session.
The swap file is a file with the same filename as the text file with ".swp" appended. See ":help recovery".
不接文件,直接用-r选项,能列出所有.swp文件,
vi -r
Swap files found:
In current directory:
1. .test.txt.swp
owned by: oracle dated: Tue Jul 3 16:06:28 2018
file name: ~oracle/test.txt
modified: no
user name: oracle host name: vm-kvm12907-app
process ID: 31641 (still running)
In directory ~/tmp:
-- none --
In directory /var/tmp:
-- none --
In directory /tmp:
-- none --
如果您觉得本文有帮助,欢迎关注转发:bisal的个人杂货铺,