vim 使用 swp 文件恢复文件时无法打开文件进行写入

Can't open file for writing when vim recover file with swp file

我想恢复 /home/lufei/market/resources/views/user.blade.php,这里是 swp 文件的位置。

> lufei@localhost:~/market$ ls resources/views/.user.blade.php.swp 
> resources/views/.user.blade.php.swp

我用vim -r resources/views/.user.blade.php.swp恢复文件,我明白了,

E306: Cannot open resources/views/.user.blade.php.swp Press ENTER or type command to continue

当我使用 sudo vim -r resources/views/.user.blade.php.swp 时,我可以打开文件,但是当我在 vim 中使用 :wq 时,我得到

"/root/market/resources/views/user.blade.php"
"/root/market/resources/views/user.blade.php" E212: Can't open file for writing Press ENTER or type command to continue

从上面的信息我认为vim想要将文件保存到根目录。从 vi - getting an error E212: Can't open file for writing,我想如果我创建路径 /root/market/resources/views/ 可能会起作用。但是我想直接把文件保存到正确的地方/home/lufei/market/resources/views/user.blade.php。所以我使用 su && cd /home/lufei/market 然后 vim -r resources/views/.user.blade.php.swp。但是当我 :wq 在 vim 时,我也得到

"~/market/resources/views/user.blade.php"                                                        
"~/market/resources/views/user.blade.php" E212: Can't open file for writing
Press ENTER or type command to continue

从上面的尝试,无论我使用普通用户还是root用户,当我恢复文件时,vim都会尝试将文件保存到错误的地方,路径不存在。

我可以直接将文件恢复到正确的位置吗,而不创建临时路径,然后将恢复的文件复制回正确的路径。

确保您有足够的访问权限以 lufei 的身份打开文件,然后开始恢复:

$ chmod u=rw resources/views/.user.blade.php.swp
$ vim -r resources/views/.user.blade.php.swp

这可能是像命令sudo vim market/resources/views/user.blade.php中的第2项这样的编辑会话挤压造成的。它使 swp 文件成为 wong 原始文件路径。注意我真的没有 /root/market/resources/views/user.blade.php 文件甚至 /root/market/ 路径。这是我第一次创建文件 user.blade.php 并且没有保存它,然后关闭计算机。

E325: ATTENTION
Found a swap file by the name "resources/views/.user.blade.php.swp"
          owned by: root   dated: Thu Nov  9 17:33:23 2017
         file name: ~root/market/resources/views/user.blade.php
          modified: YES
         user name: root   host name: localhost
        process ID: 128985
While opening file "resources/views/user.blade.php"

(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 resources/views/user.blade.php"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "resources/views/.user.blade.php.swp"
    to avoid this message.

Swap file "resources/views/.user.blade.php.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort: 

解决方案:

    1. sudo vim market/resources/views/user.blade.php,如果vim显示以上信息,只需按R,然后保存文件。如果没有显示以上信息,使用:rec(over),然后保存文件。
    1. 使用vim打开任何文件env创建一个新文件,使用sudo vim aaa.txt,然后使用:rec(over) user.blade.php然后保存它。这会将文件保存到 user.blade.php 而不是 aaa.txt。确保 aaa.txt 和 .user.blade.php 在同一目录中。