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

很常见的一个问题,在使用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.csdn.net/huangfei711/article/details/51226644

你可能感兴趣的:(Linux使用技巧)