编程技巧:VIM中解决swap file “*.swp”already exists!文件已打开

本文详细解析了在使用Vim编辑器时遇到的.swap文件问题,阐述了问题产生的原因,即未正常关闭编辑中的文件导致的.swap文件残留,并提供了具体的解决步骤,包括如何安全地删除.swap文件以恢复正常编辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很常见的一个问题,在使用vim打开某一个文件时,如果没有正常关闭,比如正在运行时,则会产生.swap文件,那么下次再重新打开时,提示如下错误:

E325: ATTENTION
Found a swap file by the name ".11-1.c.swp"
          owned by: fairy   dated: Tue Dec 18 19:44:18 2018
         file name: ~fairy/Unix_Code/Chapter11/queue_2.0/11-1.c
          modified: no
         user name: fairy   host name: ubuntu
        process ID: 2239 (still running)
While opening file "11-1.c"
             dated: Tue Dec 18 16:48:41 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 11-1.c"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file ".11-1.c.swp"
    to avoid this message.

Swap file ".11-1.c.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort: 

一、问题原因

这是因为vim每次再打开文件时,实际是从内存中复制了一份临时文件,我们所做的修改是在临时文件上的,只有使用了w命令后,才会写回到磁盘上。出现.swp文件是临时文件已经存在,使用ll -a命令查看:有一个.11-1.c.swp文件

fairy@ubuntu:~/Unix_Code/Chapter11/queue_2.0$ ll -a
total 60
drwxrwxr-x 2 fairy fairy  4096 12月 18 19:47 ./
drwxrwxr-x 4 fairy fairy  4096 12月 18 14:38 ../
-rwxrwxr-x 1 fairy fairy 21112 12月 18 16:50 11-1*
-rw-rw-r-- 1 fairy fairy   589 12月 18 16:48 11-1.c
-rw-r--r-- 1 fairy fairy 12288 12月 18 19:44 .11-1.c.swp
-rw-rw-r-- 1 fairy fairy  4251 12月 18 16:50 workq.c
-rw-rw-r-- 1 fairy fairy  1067 12月 18 16:48 workq.h

二、解决方法

因为系统中已经存在了.swp文件,则会询问用户如何处理,如果想要正常使用,需删除.swp,即可正常使用。

fairy@ubuntu:~/Unix_Code/Chapter11/queue_2.0$ rm .11-1.c.swp 

参考链接:https://blog.youkuaiyun.com/huangfei711/article/details/51226644

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值