200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > linux vi发现交换文件 vim恢复交换文件中的内容

linux vi发现交换文件 vim恢复交换文件中的内容

时间:2019-05-24 02:18:39

相关推荐

linux vi发现交换文件 vim恢复交换文件中的内容

交换文件的产生可能有几种情况:

1,当你用vim编辑一个文件时,发生突发事件,导致窗口关闭,或者系统崩溃重启;

2,另一个vim正在编辑该文件;

下面具体说下第一种情况:

原文件:#cata.txt

Thisisatestforvim.

line1

line2

line3

在文件里面添加了新的内容:Thisisatestforvim.

line1

line2

line3

###Addnewlines:

Line4

Line5

Line6

还没来得及保存,突然终端就断开了,导致.swp文件产生[root@test~]#ll-a

total316

drwx------5xielfxielf4096Jan713:12.

drwxr-xr-x7rootroot4096Jan14..

-rw-r--r--1rootroot45Jan713:10a.txt

-rw-r--r--1rootroot12288Jan713:11.a.txt.swp

查看原文件内容,发现是最原始的数据,没有我们做过修改的内容[root@test~]#cata.txt

Thisisatestforvim.

line1

line2

line3

查看swp文件:[root@AY1308071436317184c3Z~]#vim-r.a.txt.swp#-r读取交换文件

会看到下面一段提示:

Usingswapfile".a.txt.swp"

Originalfile"/root/a.txt"

E308:Warning:Originalfilemayhavebeenchanged

Recoverycompleted.YoushouldcheckifeverythingisOK.

(Youmightwanttowriteoutthisfileunderanothername

andrundiffwiththeoriginalfiletocheckforchanges)

Deletethe.swpfileafterwards.

PressENTERortypecommandtocontinue

按ENTER继续:

Thisisatestforvim.

line1

line2

line3

###Addnewlines:

Line4

Line5

Line6

swp文件里面的内容有最后编辑的最新内容,那我们要把它恢复到a.txt,先退出。

打开a.txt源文件:[root@test~]#vima.txt

E325:ATTENTION

Foundaswapfilebythename".a.txt.swp"

ownedby:rootdated:WedJan713:11:26

filename:~root/a.txt

modified:YES

username:roothostname:test

processID:29908

Whileopeningfile"a.txt"

dated:WedJan713:10:00

(1)Anotherprogrammaybeeditingthesamefile.

Ifthisisthecase,becarefulnottoendupwithtwo

differentinstancesofthesamefilewhenmakingchanges.

Quit,orcontinuewithcaution.

(2)Aneditsessionforthisfilecrashed.

Ifthisisthecase,use":recover"or"vim-ra.txt"

torecoverthechanges(see":helprecovery").

Ifyoudidthisalready,deletetheswapfile".a.txt.swp"

toavoidthismessage.

Swapfile".a.txt.swp"alreadyexists!

[O]penRead-Only,(E)ditanyway,(R)ecover,(D)eleteit,(Q)uit,(A)bort:

##

O以只读方式打开

E继续编辑,如果该文件正在被另一个vim编辑,你将可能得到两个版本

R从swp文件中恢复

D删除swp文件,如果发现swp文件和原文件内容一致,没有任何改动,可以用该操作。

Q退出

A类似于退出,但它同时会撤销后续命令的执行,在载入一个脚本编辑多个文件时比较有用。

上面提示显示:modified: YES,说明有改动,我们从swp文件中恢复,按shift+r,接着按回车就恢复了swp文件中的内容。

然后保存下,这样才能将swp的内容保存到原文件里面,保存退出后,记得将swp文件删除,不然下次还会提示。

确认下恢复后的文件:[root@test~]#cata.txt

Thisisatestforvim.

line1

line2

line3

###Addnewlines:

Line4

Line5

Line6

OK,没问题,已经恢复了修改后的文件。

试想一下,如果我们正在配置某个配置文件,突然导致vim编辑异常关闭,导致了交换文件的产生,那我们之前做的大量的修改就白做了,所以知道如何恢复swp文件是很有必要的哦,避免过多的重复工作。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。