1.什么是.swp文件
.swp = swap file(交换文件),是VIM的临时自动保存文件。
当你用VIM打开一个文件时,如practice.txt,VIM会在同一目录下自动创建一个一个隐藏的交换文件.practice.txt.swp
这个交换文件的作用:
| 作用 | 说明 |
|---|---|
| 崩溃恢复 | 如果 VIM 崩溃、系统宕机、SSH 断开,你未保存的修改会存在.swp里,可以用:recover找回 |
| 防止多重编辑 | 当你再次打开同一个文件时,VIM 检测到.swp已存在,就会警告你(就是你看到的那个 E325),避免两个人同时编辑导致冲突 |
| 自动保存草稿 | 默认每 4 秒或每输入 200 个字符,VIM 会自动保存到.swp,所以你即使忘了按:w,也能找回大部分内容 |
正常情况下,当你正常退出VIM(比如用:wq或ZZ),VIM 会自动删除.swp文件。
只有在你异常退出(直接关终端、kill 进程、SSH 断开、系统崩溃)时,.swp才会残留下来。
交换文件是隐藏文件的原因:
因为以点(.)开头的文件在 Linux/Unix 中就是隐藏文件。
之所以设计成隐藏,是因为:
不需要用户手动管理:它只是 VIM 内部使用的临时文件,不应该干扰日常的
ls查看避免误操作:如果你
rm *.txt不会误删它,但rm -rf *依然会删掉它(因为*不匹配隐藏文件,但.*会)保持目录整洁:对于普通用户来说,你只应该看到自己创建的文件(如
practice.txt),而不是 VIM 的“内部工作文件”
总结:
| 问题 | 答案 |
|---|---|
.swp是什么? | VIM 的临时自动保存文件 |
| 有什么作用? | 崩溃恢复 + 防止多重编辑 + 自动草稿 |
| 为什么隐藏? | 避免干扰,它是 VIM 的内部文件,不是你的文档 |
| 什么时候自动删除? | 正常退出 VIM 时 |
| 为什么现在还残留? | 上次 VIM 异常退出了(崩溃、关终端等) |
| 能直接删吗? | 能。确认不需要恢复后,rm .*.swp |
| 删了会丢数据吗? | 不会。原文件practice.txt还在,只是丢了上次未保存的修改 |
2.我遇到的错误
E325: ATTENTION Found a swap file by the name ".practice.txt.swp" owned by: rain dated: Thu May 07 09:36:09 2026 file name: ~rain/Tests/practice.txt modified: YES user name: rain host name: LAPTOP-IIB6PKR5 process ID: 2170 (STILL RUNNING) While opening file "practice.txt" dated: Thu May 07 09:40:22 2026 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 practice.txt" to recover the changes (see ":help recovery"). If you did this already, delete the swap file ".practice.txt.swp" to avoid this message. Swap file ".practice.txt.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:这个错误提示很常见,不用担心。原因:你上次编辑practice.txt时没有正常退出(比如直接关闭终端、SSH 断开、或者 VIM 意外崩溃),导致 VIM 留下了一个临时交换文件(.practice.txt.swp)。
你现在有 5 个选项,请根据情况选择:
| 选项 | 含义 | 什么时候选 |
|---|---|---|
O | Open Read-Only | 只想查看文件,不改内容 |
E | Edit anyway | 确定上次的修改不重要,直接编辑 |
R | Recover | 想恢复上次未保存的修改 |
Q | Quit | 退出 VIM,先处理问题 |
A | Abort | 放弃本次编辑,和 Q 类似 |
推荐操作步骤
想直接编辑(放弃之前的未保存内容):
按
E(Edit anyway)正常编辑文件
编辑完成后,手动删除交换文件:
退出 VIM 后,在终端执行:
rm .practice.txt.swp
想恢复上次丢失的内容:
按
R(Recover)VIM 会尝试恢复上次崩溃时的内容
确认无误后保存(
:w)退出 VIM(
:q)删除交换文件:
rm .practice.txt.swp
为什么不直接按E?
按E继续编辑时,每次打开这个文件都会看到这个警告,直到你删除.practice.txt.swp为止。所以建议编辑完后执行:
3.交换文件的练习
主动创造.swp文件来练习各种恢复和清理操作
步骤:
# 1. 创建一个测试文件 cd ~/Tests echo "这是第一行内容" > test_swp.txt echo "这是第二行内容" >> test_swp.txt # 2. 用 VIM 打开它 vim test_swp.txt # 3. 在 VIM 中进行一些修改(不保存!) # 按 i 进入插入模式 # 输入一些新内容,比如 "这是崩溃前未保存的内容" # 注意:不要按 :w 保存 # 4. 模拟崩溃 —— 直接关闭终端窗口(点右上角 X) # 或者在另一个终端里 kill 掉 VIM 进程点右上角X关闭终端窗口来模拟崩溃的情况:
下面重新打开终端窗口:
rain@LAPTOP-IIB6PKR5:~$ ls Projects Tests fnm_setup hy2 miniforge3 miniforge_setup rain@LAPTOP-IIB6PKR5:~$ cd Tests rain@LAPTOP-IIB6PKR5:~/Tests$ ls -a . .. .test_swp.txt.swp D a.txt b.txt practice.txt test_swp.txt rain@LAPTOP-IIB6PKR5:~/Tests$ ls -a | grep swp .test_swp.txt.swp test_swp.txt验证了交换文件已经被创建了
练习1:尝试恢复
rain@LAPTOP-IIB6PKR5:~/Tests$ vim test_swp.txt出现E325警告
E325: ATTENTION Found a swap file by the name ".test_swp.txt.swp" owned by: rain dated: Thu May 07 12:51:22 2026 file name: ~rain/Tests/test_swp.txt modified: YES user name: rain host name: LAPTOP-IIB6PKR5 process ID: 545 While opening file "test_swp.txt" dated: Thu May 07 12:49:24 2026 (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_swp.txt" to recover the changes (see ":help recovery"). If you did this already, delete the swap file ".test_swp.txt.swp" to avoid this message. Swap file ".test_swp.txt.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:接着按R恢复,出现如下界面:
E325: ATTENTION Found a swap file by the name ".test_swp.txt.swp" owned by: rain dated: Thu May 07 12:51:22 2026 file name: ~rain/Tests/test_swp.txt modified: YES user name: rain host name: LAPTOP-IIB6PKR5 process ID: 545 While opening file "test_swp.txt" dated: Thu May 07 12:55:58 2026 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 test_swp.txt" to recover the changes (see ":help recovery"). If you did this already, delete the swap file ".test_swp.txt.swp" to avoid this message. Swap file ".test_swp.txt.swp" already exists! "test_swp.txt" 3L, 72B Using swap file ".test_swp.txt.swp" Original file "~/Tests/test_swp.txt" E308: Warning: Original file may have been changed Recovery completed. Buffer contents equals file contents. You may want to delete the .swp file now. Press ENTER or type command to continue它说明:
1.Recovery completed. Buffer contents equals file contents.
即恢复操作已经完成了。缓存中的内容等于文件的内容。
这就是表明VIM已经将交换文件中的内容(即未保存的内容)恢复到了原始文件上(即test_swp,txt)
2.You may want to delete the .swp file now.
VIM很贴心地提醒你:恢复操作以及完成,现在可以安全地删除这个.swp文件了
接着你需要按一下回车键,会回到VIM正常的编辑界面
这是第一行内容 这是第二行内容 这是未崩溃前的内容 "test_swp.txt" 3L, 72B你需要检查文件的内容,确认它确实是崩溃前的那个文件
接着输入:wq保存并退出
检查.swp文件是否存在:
rain@LAPTOP-IIB6PKR5:~/Tests$ ls -a . .. .test_swp.txt.swp D a.txt b.txt practice.txt test_swp.txt看来.swp文件还是存在的
关键点:手动恢复(按R)并不会自动删除交换文件,VIM会建议你手动删除它
于是你需要手动输入并执行下面的命令:
rain@LAPTOP-IIB6PKR5:~/Tests$ rm .test_swp.txt.swp再次检查,发现.swp交换文件以及被删除
你再次用vim命令打开test_swp.txt文件就不会报错了!
rain@LAPTOP-IIB6PKR5:~/Tests$ vim test_swp.txt这是第一行内容 这是第二行内容 这是未崩溃前的内容 "test_swp.txt" 3L, 72B练习2:按E忽略警告
# 重新制造一个 .swp(重复前面的步骤) vim test_swp.txt # 修改后非正常退出 # 再次打开 vim test_swp.txt # 按 E 忽略警告,直接编辑 # 修改后 :wq 正常保存退出 ls -a | grep swp # 注意:.swp 还在!(因为没处理)1.用vim创建新文件模拟崩溃
我自己的操作:
rm test_swp.txt→ 删除了原文件vim test_swp.txt→ 用 VIM 打开一个新文件(同名)输入内容 → VIM 创建了
.test_swp.txt.swp(交换文件)点击右上角
X→异常退出(相当于崩溃)重新打开终端 → 发现只有
.swp文件,没有test_swp.txt
rain@LAPTOP-IIB6PKR5:~$ ls Projects Tests fnm_setup hy2 miniforge3 miniforge_setup rain@LAPTOP-IIB6PKR5:~$ cd Tests rain@LAPTOP-IIB6PKR5:~/Tests$ ls D a.txt b.txt practice.txt rain@LAPTOP-IIB6PKR5:~/Tests$ ls D a.txt b.txt practice.txt rain@LAPTOP-IIB6PKR5:~/Tests$ ls -a . .. .test_swp.txt.swp D a.txt b.txt practice.txt原因:
关键原因:你从未保存过这个文件。
VIM 的工作流程:
打开新文件 → 只在内存中编辑,没有保存到磁盘
自动生成
.swp→ 这是 VIM 的临时草稿,用于崩溃恢复点击
X关闭 →异常退出,VIM 没有机会执行保存操作结果 → 只有
.swp残留,原文件从未被创建
rain@LAPTOP-IIB6PKR5:~/Tests$ ls -l .test_swp.txt.swp # 查看交换文件的大小 -rw------- 1 rain rain 12288 May 7 13:17 .test_swp.txt.swp发现.swp文件只有7字节(7KB),说明输入的内容确实保存在这里面了
rain@LAPTOP-IIB6PKR5:~/Tests$ cat .test_swp.txt.swp U3210#"! Utpad�����这是未崩溃前的内容这是第二行的内容这是第一行的内容rain@LAPTOP-IIB6PKR5:~/Tests$为什么看到的是乱码?
.swp文件不是文本文件,它是 VIM 直接保存的内存映像:
| 内容 | 说明 |
|---|---|
U3210#"! | VIM 的文件头标记 |
Utpad | 可能包含了你的用户名rain的部分信息 |
这是未崩溃前的内容 | 你实际输入的文本(被二进制数据包围) |
| 其他乱码 | VIM 保存的光标位置、模式状态、撤销历史等信息 |
输入下面的命令:
rain@LAPTOP-IIB6PKR5:~/Tests$ vim -r test_swp.txt-r选项(Recover)的作用:恢复交换文件中保存的未保存的内容
| 用法 | 作用 |
|---|---|
vim -r | 列出所有可恢复的交换文件 |
vim -r filename | 恢复指定文件的交换文件内容 |
vim -r swapfile | 直接用指定的交换文件恢复(不常用) |
接着出现如下界面:
Using swap file ".test_swp.txt.swp" "~/Tests/test_swp.txt" [New] 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) You may want to delete the .swp file now. Press ENTER or type command to continue按回车进入到VIM正常的编辑界面(默认模式下)
检查确实是未保存的内容后,输入:wq保存并退出
rain@LAPTOP-IIB6PKR5:~/Tests$ cat test_swp.txt # test_swp.txt文件不存在,因为之前你在Vim中没有做任何保存操作,这个新文件自然就不会被保存下来 cat: test_swp.txt: No such file or directory rain@LAPTOP-IIB6PKR5:~/Tests$ vim -r test_swp.txt # 将.test_swp.txt.swp文件中的内容恢复到test_swp.txt中 rain@LAPTOP-IIB6PKR5:~/Tests$ cat test_swp.txt # 发现test_swp.txt文件存在了 这是第一行的内容 这是第二行的内容 这是未崩溃前的内容 rain@LAPTOP-IIB6PKR5:~/Tests$ ls -al total 40 drwxr-xr-x 3 rain rain 4096 May 7 13:32 . drwxr-x--- 18 rain rain 4096 May 7 13:32 .. -rw------- 1 rain rain 12288 May 7 13:31 .test_swp.txt.swp drwxr-xr-x 2 rain rain 4096 May 7 00:29 D -rw-r--r-- 1 rain rain 6 May 7 00:28 a.txt -rw-r--r-- 1 rain rain 12 May 7 00:29 b.txt -rw-r--r-- 1 rain rain 82 May 7 12:46 practice.txt -rw-r--r-- 1 rain rain 78 May 7 13:32 test_swp.txt rain@LAPTOP-IIB6PKR5:~/Tests$ vim test_swp.txt # 再次用vim命令打开,发现还是出现错误。这是因为没有删除交换文件 rain@LAPTOP-IIB6PKR5:~/Tests$ rm .test_swp.txt.swp # 删除交换文件 rain@LAPTOP-IIB6PKR5:~/Tests$ ls -a . .. D a.txt b.txt practice.txt test_swp.txt rain@LAPTOP-IIB6PKR5:~/Tests$ vim test_swp.txt # 再用vim命令打开时发现没有报错只有删除.swp文件后,用vim打开test_swp.txt才不会报错!
VIM 检测到.swp文件存在,它不确定这是:
情况A:另一个 VIM 进程正在编辑同一个文件(需要警告,避免冲突)
情况B:上次编辑崩溃留下的残留(可以安全恢复或删除)
所以它每次打开都会提示,直到你明确告诉它“这个.swp已经没用了”
2.在文件存在的情况下对它做修改模拟崩溃
rm test_swp.txt touch test_swp.txt echo "这是第一行内容" >> test_swp.txt echo "这是第二行内容" >> test_swp.txt vim test_swp.txt # 按j键一次到第二行,按o键往下一行(第三行)进行添加操作,添加“这是未崩溃前的内容”,然后点击右上角的X直接关系Linux终端用vim打开test_swp.txt时出现:
E325: ATTENTION Found a swap file by the name ".test_swp.txt.swp" owned by: rain dated: Thu May 07 13:47:35 2026 file name: ~rain/Tests/test_swp.txt modified: YES user name: rain host name: LAPTOP-IIB6PKR5 process ID: 766 While opening file "test_swp.txt" dated: Thu May 07 13:47:12 2026 (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_swp.txt" to recover the changes (see ":help recovery"). If you did this already, delete the swap file ".test_swp.txt.swp" to avoid this message. Swap file ".test_swp.txt.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:按下E,表示忽略这个警告,于是会回到VIM正常的编辑界面:
是第一行内容 这是第二行内容 "test_swp.txt" 2L, 44B输入:wq
执行下面的命令:
rain@LAPTOP-IIB6PKR5:~/Tests$ rm .test_swp.txt.swp # 删除交换文件 rain@LAPTOP-IIB6PKR5:~/Tests$ ls -a # 查看是否有交换文件 . .. D a.txt b.txt practice.txt test_swp.txt再用vim去打开test_swp.txt就不会出现这个警告了,此时文件中的内容只有:
是第一行内容 这是第二行内容 "test_swp.txt" 2L, 44B这是因为执行的是E,没有执行R或vim -r test_swp.txt操作,没有将未保存的内容恢复到原始文件上。
练习3:按D删除交换文件
rm test_swp.txt touch test_swp.txt echo "这是文件的一行内容" >> test_swp.txt echo "这是文件的第二行内容" >> test_swp.txt vim test_swp.txt # 在第3行输入:这是文件未崩溃前的内容关掉Linux终端
再次打开Linux终端
rain@LAPTOP-IIB6PKR5:~$ cd Tests rain@LAPTOP-IIB6PKR5:~/Tests$ ls -a . .. .test_swp.txt.swp D a.txt b.txt practice.txt test_swp.txt rain@LAPTOP-IIB6PKR5:~/Tests$ cat test_swp.txt 这是文件的第一行内容 这是文件的第二行内容ain@LAPTOP-IIB6PKR5:~/Tests$ cat .test_swp.txt.swp U3210#"! Utpad|����这是文件未崩溃前的内容这是文件的第二行内容这是文件的第一行内容rain@LAPTOP-IIB6PKR5:~/Tests$rain@LAPTOP-IIB6PKR5:~/Tests$ vim test_swp.txt出现如下界面:
E325: ATTENTION Found a swap file by the name ".test_swp.txt.swp" owned by: rain dated: Thu May 07 14:03:41 2026 file name: ~rain/Tests/test_swp.txt modified: YES user name: rain host name: LAPTOP-IIB6PKR5 process ID: 813 While opening file "test_swp.txt" dated: Thu May 07 14:02:55 2026 (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_swp.txt" to recover the changes (see ":help recovery"). If you did this already, delete the swap file ".test_swp.txt.swp" to avoid this message. Swap file ".test_swp.txt.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:按下大写的D,回到正常的VIM编辑界面,这时无论保存还是不保存结果都一样,因为前两行的内容在文件出现崩溃前已经保存好了,只有第三行的“这是未崩溃前的内容”没有被保存,而你没有执行恢复操作,所以现在test_swp.txt文件中只有两行内容
退出VIM界面后
rain@LAPTOP-IIB6PKR5:~/Tests$ ls -a . .. D a.txt b.txt practice.txt test_swp.txt发现.test_swp.txt.swp交换文件已经被删除了
练习4:手动删除交换文件
rm test_swp.txt touch test_swp.txt echo "这是一行的内容" >> test_swp.txt echo "这是第二行的内容" >> test_swp.txt vim test_swp.txt # 在第3行输入:这是未崩溃前的内容同样先关闭再打开Linux终端
rain@LAPTOP-IIB6PKR5:~$ cd Tests rain@LAPTOP-IIB6PKR5:~/Tests$ ls -a . .. .test_swp.txt.swp D a.txt b.txt practice.txt test_swp.txt rain@LAPTOP-IIB6PKR5:~/Tests$ vim test_swp.txt # 出现错误,按Q退出 rain@LAPTOP-IIB6PKR5:~/Tests$ rm .test_swp.txt.swp # 直接删除交换文件 rain@LAPTOP-IIB6PKR5:~/Tests$ ls -a . .. D a.txt b.txt practice.txt test_swp.txt再次vim打开test_swp.txt
是第一行的内容 这是第二行的内容 ~ ~ "test_swp.txt" 2L, 50B至此,
这是一些非常成功的练习!你已经掌握了处理 VIM 意外崩溃后,挽救未保存工作的核心技能。