Vim的撤销系统不仅能够撤销和重做简单的错误,而且还能够访问不同的文本状态,使您能够控制所输入的所有文本。
- u 撤销
- Ctrl-R 或者 :redo 重做
- U 撤消所有最新的更改。
注:U和u的区别 - U删除最新更改行的所有更改,而U每次只删除一个更改。U绕过了所有中间更改,并在开始时回到原始状。
- u不算一次改变,但U算一次改变
Breaking The Blocks
对插入模式下的文本分块,这样撤销的就不会是一整个插入内容
在插入模式下,Ctrl-G u创建一个撤销断点
撤销树
- g+ 可以使撤销的文本返回
- 要遍历每个撤消树节点,可以使用g+转到新状态,使用g-转到旧状态。
- u、Ctrl-R、g+和g-的区别在于,u和Ctrl-R都只遍历undo tree中的主节点,而g+和g-则遍历undo tree中的所有节点。
- vim-mundo plugin to be very useful to help visualize Vim's undo tree. Give it some time to play around with it.
Persistent Undo
- 要翻转上次编辑会话的撤消历史,Vim可以使用撤消文件:wundo保存撤消历史。
- 使用wundo {my-undo file}创建撤销文件。
- 如果您需要覆盖现有的撤销文件,您可以添加!在wundo之后。
:wundo! mynumbers.undo - By now you should have mynumbers.txt and mynumbers.undo files in your directory.
- 要再次打开执行撤销,需要首先加载撤销文件
:rundo mynumbers.undo - 如果您希望具有自动撤消持久性,那么一种方法是在vimrc中添加这些内容
set undodir=~/.vim/undo_dir
set undofile
- 上面的设置将把所有的undofile放到一个集中的目录~/中。vim目录
- 名称undo_dir是任意的。
- set undofile告诉Vim打开undofile特性,因为它在默认情况下是关闭的。现在,无论何时保存,Vim都会自动在undo_dir目录中创建并更新相关文件(确保在~/中创建了实际的undo_dir目录。Vim目录)。
Time Travel
- Vim can travel to a text state in the past with :earlier command-line command.
- You can use :undolist to see when the last change was made.
- :earlier also accepts different arguments:
:earlier 10s Go to the state 10 seconds before
:earlier 10m Go to the state 10 minutes before
:earlier 10h Go to the state 10 hours before
:earlier 10d Go to the state 10 days before
:earlier 2 go back to an older text state two changes ago.
:earlier 2 = g-
网友评论