bash 我如何处理 vim 的交换文件系统?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21794235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:34:41  来源:igfitidea点击:

How do I deal with vim's swap file system?

linuxbashunixvimterminal

提问by reesjones

When using vimin ubuntu, I accidentally pressed ctrl-zwhich suspended my session of vim. I was editing a file (I'll call it test) which was not saved.

vim在 ubuntu 中使用时,我不小心按下ctrl-z了暂停我的vim. 我正在编辑一个test没有保存的文件(我称之为)。

When I opened the file again in vim, I got the swap file error:

当我在 中再次打开文件时vim,出现交换文件错误:

E325: ATTENTION
Found a swap file by the name ".test.swp"


Swap file ".test.swp" already exists!

According to Found a swap file by the namequestion, I have two options:

根据根据名称问题找到交换文件,我有两个选择:

  1. Find the session and finish it (preferable).
  2. Delete the .swpfile (if you're sure the other git session has gone away).
  1. 找到会话并完成它(最好)。
  2. 删除.swp文件(如果您确定另一个 git 会话已经消失)。

How would I do either of those things? If I perform rm test.swpit doesn't see the file:

我将如何做这两件事?如果我执行rm test.swp它看不到文件:

rm: cannot remove `test.swp': No such file or directory

What am I doing wrong in the deletion of the swap file and how can I finish the session?

删除交换文件时我做错了什么,如何完成会话?



EDIT: I forgot the period in test.swpSo the correct way to remove the swp file is rm .test.swp.

编辑:我忘记了句号test.swp所以删除 swp 文件的正确方法是rm .test.swp.

My remaining question is how to resume/finish a suspended session of vim.

我剩下的问题是如何恢复/完成vim.

回答by Meino Cramer

This is not related to Ubuntu alone, since what happens is a base mechanism of nearly every Unix OS. By pressin ^Z you ave suspended (not ended) the currently running vim session. The vim session is still there and waiting for a signal to put it to foreground again.

这与 Ubuntu 无关,因为发生的事情几乎是每个 Unix 操作系统的基本机制。通过按 ^Z 您已暂停(未结束)当前正在运行的 vim 会话。vim 会话仍然存在并等待信号再次将其置于前台。

To reactivate the session: If vim was started directly from the commandline -- use the command "fg" (which is for "ForeGroung") and vim will appear again. This works on all ksh/bourne-like shells. For t/csh I dont know. This only works when the command "fg" was given on the console of the same terminal session as from which vim was started (which is the "controlling terminal" related to the vim session).

重新激活会话: 如果 vim 是直接从命令行启动的——使用命令“fg”(用于“ForeGroung”),vim 将再次出现。这适用于所有 ksh/bourne-like shell。对于 t/csh 我不知道。这仅在与启动 vim 的终端会话(即与 vim 会话相关的“控制终端”)的控制台上给出命令“fg”时才有效。

If vim was started (mostly under the name gvim) from a menu of a windowmanager you are a little bit out of luck here, since (g)vim gets detached from its controling terminal.

如果 vim 是从窗口管理器的菜单中启动的(主要以 gvim 的名称启动),那么您在这里有点不走运,因为 (g)vim 与其控制终端分离。

Your options to recover: Use "fg" if the condition is valid described above. This is the cleanest way.

您的恢复选项:如果上述条件有效,则使用“fg”。这是最干净的方法。

If the (g)vim session is detached from the controling terminal, which can be checked by doing a "ps -ef | grep vim". If the column for the TTY (see header of the output) shows a "?" there is no controling terminal anymore, I would recommend to send the process a SIGHUB (see manpage for the commmand "kill"/"killall") and then a SIGKILL if it is still there. Killing vim (or any other task) will probably result in inconsistent data though, cause there was no "save" command to vim before it is killed.

如果 (g)vim 会话与控制终端分离,可以通过执行“ps -ef | grep vim”来检查。如果 TTY 的列(请参阅输出标题)显示“?” 不再有控制终端,我建议向进程发送一个 SIGHUB(请参阅命令“kill”/“killall”的联机帮助页),然后如果它仍然存在,则发送一个 SIGKILL。杀死vim(或任何其他任务)可能会导致数据不一致,因为在vim被杀死之前没有“保存”命令。

After that, start a new vim with the same file, do a "recover" first (as offered by vim, which sees the according swp-file) , save the file, end vim and start it again with that file and do a "delete swap file". This is the savest way possible after killing vim.

之后,使用相同的文件启动一个新的 vim,首先进行“恢复”(由 vim 提供,它会看到相应的 swp 文件),保存文件,结束 vim 并使用该文件再次启动它并执行“删除交换文件”。这是杀死 vim 后最可能的保存方式。

To avoid accidentically putting vim into background if not wanted, map ^Z to another, more "complicated" keysequence, which is hard to press accidentically. You can deactivate the ^Z command by adding the following line to your .vimrc:

为了避免在不需要的情况下意外地将 vim 置于后台,请将 ^Z 映射到另一个更“复杂”的键序列,这很难被意外按下。您可以通过在 .vimrc 中添加以下行来停用 ^Z 命令:

map <C-z> ;

map <C-z> ;

Addition: Your rm-command misses the dot in front of .test.swp causing rm not to find the file...or deleting another file, which is named test.swp instead of ".test.swp". By deleting swp-files via vim, you are sure to delete the correct file. Swp-files always start with a dot (hidden file) on UNIX like systems.

另外:您的 rm 命令错过了 .test.swp 前面的点,导致 rm 找不到文件...或删除另一个名为 test.swp 而不是“.test.swp”的文件。通过 vim 删除 swp 文件,您一定会删除正确的文件。Swp 文件在类 UNIX 系统上总是以点(隐藏文件)开头。

回答by CreationTribe

the file is going to be " .test.swp", not " test.swp" So you'll want to:

该文件将是“ .test.swp”,而不是“ test.swp”,因此您需要:

rm .test.swp