How to get rid of the warnings when opening a file that has a .swp file?

program

E325: ATTENTION
Found a swap file by the name “.notes.swp”
owned by: james dated: Fri Dec 3 17:38:07 2010
file name: ~james/school/se/project-dir/rottencucumber/doc/notes
modified: no
user name: james host name: james-laptop
process ID: 2251 (still running)
While opening file “notes”
dated: Fri Dec 3 18:46:10 2010
NEWER than swap file!

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

Swap file “.notes.swp” already exists!
[O]pen Read-Only, (E)dit anyway, ®ecover, (Q)uit, (A)bort:

solve

This message is actually pretty important if you care about not losing text you’ve potentially not saved. It should not be considered annoying, and should not cause you to hastily delete the swap file or configure vim to run without it.

Any file you edit with vim will have a corresponding swap file while you edit, which vim uses to keep track of changes. When you quit editing a file, vim will automatically discard the corresponding swap file. Therefore, the existence of a swap file, and your attempt to write overtop the original file, should be cause for consideration and appropriate action.

The two scenarios presented in the message (E325: ATTENTION Found a swap file) are actually quite common: (1) either another vim program is editing the very same file you’re trying to edit (it could actually be another person - in which case it really wouldn’t make sense to just blindly delete the swap file - or it could be you in another terminal window or tab), or (2) a previous vim session crashed (most often this happens when you’re editing remotely, and the network session is severed - in which case the vim session was not exited normally, and the .swp file remains behind; another example of this second scenario is that you’ve accidentally closed the terminal window or tab that had an active or backgrounded vim session).

When I encounter this message I first think about whether I am editing this file in another terminal window or tab, as I normally operate with several terminal windows with several tabs each:

If I realize that I am editing in another place, and can return to it, I then press the q key to (Q)uit this additional session, and return to editing via the original vim session.
Sometimes if I am not entirely sure, I (Q)uit and then run jobs to verify whether I am running vim in the exact same terminal; if nothing comes up, I run ps -ef | grep vim to check whether I am running vim elsewhere (i.e. in another terminal window or tab). The point is, I always try to resume editing via the original vim session.


If I am certain that I cannot return to the original editing session, and I am still presented with the following options, then I press r to (R)ecover
Swap file ".notes.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:

Pressing r, you will see a message like this:

Swap file ".notes.swp" already exists!
"notes" 18L, 46C
Using swap file ".notes.swp"
Original file "/private/tmp/notes"
Recovery completed. Buffer contents equals file contents.
You may want to delete the .swp file now.

Press ENTER or type command to continue

If, on the other hand, I am no longer faced with those options, because I am at the shell prompt, then I run vim with the -r option, as follows:

vim -r notes

The resultant message will be similar:

Using swap file ".notes.swp"
Original file "/private/tmp/notes"
Recovery completed. Buffer contents equals file contents.
You may want to delete the .swp file now.

Press ENTER or type command to continue

Either way, press ENTER to continue, and you will see your file.

Note: If Vim has doubt about what it found, it will give an error message and insert lines with “???” in the text. If you see an error message while recovering, search in the file for “???” to see what is wrong. You may want to cut and paste to get the text you need.

The most common remark is “???LINES MISSING”. This means that Vim cannot read the text from the original file. This can happen if the system crashed and parts of the original file were not saved.

That said, I have never seen those ??? marks, so this must be a truly rare occurrence.


Next, save (i.e. write) the content to another file (usually I just append "2" to the end of the original file name):
:w notes2

Next, force-quit this vim session:

:q!

Next, compare the two files;

diff notes notes2

If the diff returns nothing, that means there is no difference, and it is safe to remove both the swap file and the second file:

rm .notes.swp notes2

At this point, open the original file and proceed as if there had never been a problem:

vim notes

If the diff returns something, that means the original file (via the swap file) had changes that, thanks to the recover, have been saved to the second file.

Since those changes are captured in the second file you are safe to delete the swap file and overwrite the original file with the second one:

rm .notes.swp
remove .notes.swp? y

mv notes2 notes
overwrite notes? (y/n [n]) y

At this point, open the original file and proceed as if there had never been a problem:

vim notes

This seems like a lot of work, but once you get used to the workflow it takes like 20 seconds max.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值