How is the undo tree used in Vim?

前端 未结 8 634
面向向阳花
面向向阳花 2020-12-22 14:53

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

8条回答
  •  -上瘾入骨i
    2020-12-22 15:40

    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
    

提交回复
热议问题