How is the undo tree used in Vim?

前端 未结 8 631
面向向阳花
面向向阳花 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条回答
  • 2020-12-22 15:35

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

    0 讨论(0)
  • 2020-12-22 15:36

    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.

    0 讨论(0)
  • 2020-12-22 15:37

    I'm a bit late to the party, but I figured I'd mention that I wrote an undo tree visualization plugin for Vim: http://bitbucket.org/sjl/gundo.vim/

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

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-22 15:48

    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 
    

    0 讨论(0)
  • 2020-12-22 15:49

    This page explains everything you need to know:

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

    0 讨论(0)
提交回复
热议问题