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
Can I recover my code lost during my last change in vim?
提问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.
有几种方法可以恢复由于崩溃或无意关闭程序而无意中丢失的文本。
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/undodirb. Create the undodir
mkdir ~/.vim/undodirc. To undo a change, use either of the following options
1)
g+in normal mode to go forward in history2)
g-in normal mode to go backward in history3).
:earlier 20sto go back in time by 20s orearlier 10mto go back in time by 10min etc4)
:later 20sto go forward in time by 20s orlater 10mto go forward in time by 10min etc5). Use
:undolistto get a list of undo changesd. To get a visualization of your undo-branches, you can use plugins like
gundo.vim: http://bitbucket.org/sjl/gundo.vim/to visualize the branchesUse backup files
a. Use this in your
.vimrcset backupb. 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=stringUse swap files
a. Use this in your
.vimrcset swapfileb. 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
~/tmpdirectoryc. Set up the backup file name to be added to the backup file by setting this in your
.vimrcset backupext=string
使用持久撤消。持久性撤消提供了点 #2 和 #3 中交换/备份文件选项提供的几乎所有功能,以及一些其他选项,例如粒度历史遍历。
一种。设置
persistent-undo:把它放在你的
.vimrc:set undofileset undodir=~/.vim/undodir湾 创建撤销目录
mkdir ~/.vim/undodirC。要撤消更改,请使用以下任一选项
1)
g+在普通模式下在历史上前进2)
g-在普通模式下历史倒退3)。
:earlier 20s回到过去 20 秒或earlier 10m回到过去 10 分钟等4)
:later 20s及时前进 20s 或later 10m前进 10min 等5)。使用
:undolist获得的撤消更改的列表d. 要可视化您的撤消分支,您可以使用以下插件
gundo.vim:http: //bitbucket.org/sjl/gundo.vim/来可视化分支使用备份文件
一种。在你的
.vimrcset backup湾 通过将其放在您的备份目录中来设置备份目录
.vimrcset backupdir=~/tmp/C。通过在您的
.vimrcset backupext=string使用交换文件
一种。在你的
.vimrcset swapfile湾 把它放在你的
.vimrc. 这可能不是一个好主意,因为它会阻止您拥有两个同名的文件,或者导致交换文件名冲突。set directory=~/tmp/更好的选择是提供多个路径,因此如果 vim 遇到冲突,它可以将其保存到它可以写入的目录中使用
set directory=,~/tmp/在这种情况下,它会尝试将交换文件写入当前目录。如果不能,那么它将尝试将其写入
~/tmp目录C。通过在您的
.vimrcset 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,并提供了其他功能,例如backup和swap文件选项未提供的粒度撤消。
References1. ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf

