Vim 中如何使用撤消树?

发布于 2024-07-25 19:52:19 字数 360 浏览 4 评论 0 原文

这个答案 说:

Vim 的撤消/重做系统是无与伦比的。 输入一些内容,撤消,输入其他内容,您仍然可以返回您输入的第一个内容,因为 Vim 使用撤消树而不是堆栈。 在几乎所有其他程序中,您输入的第一条内容的历史记录都会在这种情况下丢失。

这是我第一次听说这个。 如何沿着树原路返回?

This answer says:

Vim's undo/redo system is unbeatable. Type something, undo, type something else, and you can still get back the first thing you typed because Vim uses an undo tree rather than a stack. In almost every other program, the history of the first thing you typed is lost in this circumstance.

This is the first I hear of this. How can I backtrack along the tree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

橘和柠 2024-08-01 19:52:19

另请参见 :h undo-redo,其中列出了所有命令及其用法。

有两种方法可以遍历撤销树。 一是“回到过去”。 g+g- 将以时间顺序或逆时间顺序遍历树中的所有节点(这可能有点令人困惑,因为它可以在撤消分支之间任意跳转,但如果你做 g- 足够长的时间,你最终总会到达你需要去的地方)。 :earlier:later 采用时间描述符,例如 7m1h; 同样,这可以让你在撤消分支之间任意跳转。

另一种方法是使用 :undo n 跳转到树中的特定节点,其中 n 是操作的编号。 (所有操作,即文本添加、删除、替换,都会在您执行这些操作时按顺序编号。)您可以通过 :undolist 查找撤消树的叶子上的操作编号。 这将使您轻松地在分支之间跳转。 然后,您可以使用 uCtrl-R 在该分支上上下移动。

Vim 帮助中有一些很好的例子。 弄清楚它是如何工作的最好方法就是尝试一下。

See also :h undo-redo, which lists all the commands and their usage.

There are two ways to traverse the undo tree. One is to go "back in time". g+ and g- will traverse all of the nodes in the tree in chronological or reverse-chronological order (which can be a bit confusing, because it can jump arbitrarily between undo branches, but if you do g- long enough you'll always get where you need to go eventually). :earlier and :later take a time descriptor like 7m or 1h; again this can jump you arbitrarily between undo branches.

The other way is to jump to specific nodes in the tree using :undo n where n is a number of an action. (All actions, i.e. text additions, deletions, replacements, are numbered sequentially as you do them.) You can look up the number of the actions on the leaves of the undo tree via :undolist. This will let you jump between branches easily. You can then use u and Ctrl-R to move up and down that branch.

There are some good examples in the Vim help. The best way to figure out how this works is to play with it a bit.

疑心病 2024-08-01 19:52:19

我为 Vim 编写了一个撤消树可视化插件:
https://github.com/sjl/gundo.vim

我个人发现绘制树图就像这是我能理解它的唯一方法。

I wrote an undo tree visualization plugin for Vim:
https://github.com/sjl/gundo.vim

Personally I found that graphing the tree like this was the only way I could make sense of it.

梦幻的味道 2024-08-01 19:52:19

本页解释了您需要了解的所有内容:

http://vimdoc.sourceforge.net/htmldoc/usr_32。 html

This page explains everything you need to know:

http://vimdoc.sourceforge.net/htmldoc/usr_32.html

沉鱼一梦 2024-08-01 19:52:19

如果您使用的是 vim,则可以使用以下方式浏览撤消树:

  • u:(撤消)在撤消树中向后移动
  • Ctrl+R:(重做)向前移动在撤消树中

将文档时间向后或向前移动的其他方法:

  • :earlier 15m: 向后移动 15 分钟
  • :later 15m: 向前移动 15 分钟

If you're using vim, you can navigate through the undo tree using:

  • u: (undo) move back in the undo tree
  • Ctrl+R: (redo) move forward in the undo tree

Other ways of bringing document back or forward in time:

  • :earlier 15m: move back in time 15 minutes
  • :later 15m: move front in time 15 minutes
作妖 2024-08-01 19:52:19

我知道这个问题已经得到解答,但我想我应该添加一个例子。

创建一个新文件并输入:

this is a line

undol 将显示撤消树。 此时您还没有撤消任何操作,

:undol

number changes  when               saved
     1       1  14:50:36

请按 ESC 并将该行修改为:

this is a old line

切换到正常模式并按 u(撤消),这应该删除“旧”。 如果您选中undol,此时您仍然只有一个分支。

现在修改该行,使其显示:

this is a new line

现在 :undol 显示:

number changes  when               saved
     2       2  87 seconds ago
     3       2  3 seconds ago

您可以通过键入此命令切换到第一个分支,

:u 2

这会将您移动到与数字 2 关联的分支的末尾。您可以沿着该分支移动与g+g-。 此时,g+ 将不会执行任何操作(您位于叶子位置)。 如果您按g-,“old”将被删除(您正在遍历第一个撤消树)。也就是说,如果您使用g-删除“old”并按g+,“旧”将被重做。

如果您输入

:u 3

You 将跳转到第二个撤消分支的叶子,它将显示:

this is a new line

I'm aware this question has been answered, but I thought I'd add an example.

Create a new file and type:

this is a line

undol will display the undo tree. At this point you haven't undone anything

:undol

number changes  when               saved
     1       1  14:50:36

now press ESC and modify the line to:

this is a old line

switch to normal mode and press u (undo), this should remove "old". If you check undol, at this point you still have only one branch.

now modify the line so it says:

this is a new line

Now :undol shows:

number changes  when               saved
     2       2  87 seconds ago
     3       2  3 seconds ago

You can switch to the first branch by typing

:u 2

this will move you to the end of the branch associated with number 2. You can move along this branch with g+ and g-. At this point g+ will do nothing (you are at the leaf). If you press g- “old" will be removed (you are traversing the first undo tree). That is if you remove “old” with g- and press g+ again, “old" will be redone.

If you type

:u 3

You will jump to the leaf of the second undo branch and it will read:

this is a new line
娇纵 2024-08-01 19:52:19

这里总结了很多内容:

http://vim.wikia.com/wiki/Using_undo_branches

A lot of this is summed up here:

http://vim.wikia.com/wiki/Using_undo_branches

不知所踪 2024-08-01 19:52:19

undotree 是用纯 vim 脚本编写的,因此没有要求。

并在为时已晚之前将其添加到您的 vimrc 中:

  set nobackup
  set noswapfile
  set nowritebackup
  set undolevels=10000         " use many levels of undo
  set history=10000    " After nocompatible

  if has('persistent_undo')
    set undodir=$HOME/.vim/undo
    set undofile 
  endif 

在此处输入图像描述

The package undotree is written in pure vimscript so no requirement.

And add this to your vimrc before it is too late:

  set nobackup
  set noswapfile
  set nowritebackup
  set undolevels=10000         " use many levels of undo
  set history=10000    " After nocompatible

  if has('persistent_undo')
    set undodir=$HOME/.vim/undo
    set undofile 
  endif 

enter image description here

如歌彻婉言 2024-08-01 19:52:19

除了使用gundo.vim之外,我还喜欢提到g+g-

Besides using gundo.vim I like to mention g+ and g-

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文