Linux 我可以恢复在 vim 上次更改期间丢失的代码吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5662513/
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-08-05 03:39:55  来源:igfitidea点击:

Can I recover my code lost during my last change in vim?

linuxvim

提问by CCC

I am very sad I deleted wrong function without commit to SVN server by using vim After I compiled it I found I made the mistake. I 'make' the file also via vim.

我很伤心我删除了错误的函数,而没有使用 vim 提交到 SVN 服务器 编译后我发现我犯了错误。我也通过vim“制作”了这个文件。

Now I haven't closed the file and it has .swp file. I tried to use 'u' command to restore my deletion but failed. vim said it's the latest changes. sigh.... Anyway I can restore my function?

现在我还没有关闭文件,它有 .swp 文件。我尝试使用“u”命令来恢复我的删除,但失败了。vim 说这是最新的变化。叹....反正我能恢复我的功能吗?

Million thanks.

万感谢。

采纳答案by sehe

To make Drasils pointer a lot more explicit:

为了使 Drasils 指针更加明确:

 :undolist

g-to 'go back in time'

g-'回到过去'

g+to 'go forward in time'

g+'及时前进'

Vim 7.3+ has undo 'branches': meaning that it will save state snapshots, even if linear history was overwritten (so it isn't reachable by simple uand )

Vim 7.3+ 具有撤消“分支”:这意味着它会保存状态快照,即使线性历史被覆盖(因此它无法通过简单的u

回答by Andrew Aylett

Vim usually saves the previous version of any file edited as a backup with a ~appended -- you could check to see whether that file is there and if so, whether it's got the right contents.

Vim 通常将任何编辑过的文件的先前版本保存为带有~附加的备份——您可以检查该文件是否存在,如果存在,则它是否包含正确的内容。

回答by Drasill

I don't know if you can recover something here, but for the future, if you user vim 7.3, you should active these options I explain in my previous comment.

我不知道你是否可以在这里恢复一些东西,但是对于未来,如果你使用 vim 7.3,你应该激活我在我之前的评论中解释的这些选项。

I must say that the saveversplugin has saved me a lot of hours ;-)

我必须说,savevers插件为我节省了很多时间;-)

回答by alpha_989

There are a couple of ways to recover text that you may have unwittingly lost due to a crash or because you closed your program unintentionally.

有几种方法可以恢复由于崩溃或无意关闭程序而无意中丢失的文本。

  1. Use persistent-undo. Persistent undo provides almost all the features provided by swap/backup file option in points #2 and #3, along with some other options such as granular history traversal.

    a. Set persistent-undoon:

    Put this in your .vimrc:

    set undofileset undodir=~/.vim/undodir

    b. Create the undodir

    mkdir ~/.vim/undodir

    c. To undo a change, use either of the following options

    1) g+in normal mode to go forward in history

    2) g-in normal mode to go backward in history

    3). :earlier 20sto go back in time by 20s or earlier 10mto go back in time by 10min etc

    4) :later 20sto go forward in time by 20s or later 10mto go forward in time by 10min etc

    5). Use :undolistto get a list of undo changes

    d. To get a visualization of your undo-branches, you can use plugins like gundo.vim: http://bitbucket.org/sjl/gundo.vim/to visualize the branches

  2. Use backup files

    a. Use this in your .vimrcset backup

    b. Set up the backup directory by putting this in your .vimrcset backupdir=~/tmp/

    c. Set up the backup file name to be added to the backup file by setting this in your .vimrcset backupext=string

  3. Use swap files

    a. Use this in your .vimrcset swapfile

    b. Set up the swap directory by putting this in your .vimrc. This may not be a good idea, because it will prevent you from having two files with the same names, or cause conflicts in swap file names.

    set directory=~/tmp/

    A better option is to provide multiple paths, so if vim encounters a conflict it can then save it to the directory that it can write to using

    set directory=,~/tmp/

    In this case, it will try to write the swap file to the current directory. If it can't, then it will attempt to write it to the ~/tmpdirectory

    c. Set up the backup file name to be added to the backup file by setting this in your .vimrc

    set backupext=string

  1. 使用持久撤消。持久性撤消提供了点 #2 和 #3 中交换/备份文件选项提供的几乎所有功能,以及一些其他选项,例如粒度历史遍历。

    一种。设置persistent-undo

    把它放在你的.vimrc

    set undofileset undodir=~/.vim/undodir

    湾 创建撤销目录

    mkdir ~/.vim/undodir

    C。要撤消更改,请使用以下任一选项

    1) g+在普通模式下在历史上前进

    2) g-在普通模式下历史倒退

    3)。 :earlier 20s回到过去 20 秒或earlier 10m回到过去 10 分钟等

    4) :later 20s及时前进 20s 或later 10m前进 10min 等

    5)。使用:undolist获得的撤消更改的列表

    d. 要可视化您的撤消分支,您可以使用以下插件gundo.vimhttp: //bitbucket.org/sjl/gundo.vim/来可视化分支

  2. 使用备份文件

    一种。在你的.vimrcset backup

    湾 通过将其放在您的备份目录中来设置备份目录.vimrcset backupdir=~/tmp/

    C。通过在您的.vimrcset backupext=string

  3. 使用交换文件

    一种。在你的.vimrcset swapfile

    湾 把它放在你的.vimrc. 这可能不是一个好主意,因为它会阻止您拥有两个同名的文件,或者导致交换文件名冲突。

    set directory=~/tmp/

    更好的选择是提供多个路径,因此如果 vim 遇到冲突,它可以将其保存到它可以写入的目录中使用

    set directory=,~/tmp/

    在这种情况下,它会尝试将交换文件写入当前目录。如果不能,那么它将尝试将其写入~/tmp目录

    C。通过在您的.vimrc

    set backupext=string

TL;DRUse persistent-undo. It provides almost all features of swapand backup, and provides additional features such as granular undo which is not provided by backupand swapfile options.

TL;DR使用persistent-undo。它提供了swap和 的几乎所有功能backup,并提供了其他功能,例如backupswap文件选项未提供的粒度撤消。

References1. ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf

参考资料1. ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf