Skip to content

Instantly share code, notes, and snippets.

@rottencandy
Created December 15, 2020 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rottencandy/a01ed3e65a181ba42162fb917fc54894 to your computer and use it in GitHub Desktop.
Save rottencandy/a01ed3e65a181ba42162fb917fc54894 to your computer and use it in GitHub Desktop.
title author styles
How to time-travel using undo branches in Vim
Saud
style table margin padding
monokai
column_spacing
15
top bottom
3
0
top bottom
2
2

How to time-travel using undo branches in Vim

             ________ ++     ________
            /VVVVVVVV\++++  /VVVVVVVV\
            \VVVVVVVV/++++++\VVVVVVVV/
             |VVVVVV|++++++++/VVVVV/'
             |VVVVVV|++++++/VVVVV/'
            +|VVVVVV|++++/VVVVV/'+
          +++|VVVVVV|++/VVVVV/'+++++
        +++++|VVVVVV|/VVVVV/'+++++++++
          +++|VVVVVVVVVVV/'+++++++++
            +|VVVVVVVVV/'+++++++++
             |VVVVVVV/'+++++++++
             |VVVVV/'+++++++++
             |VVV/'+++++++++
             'V/'   ++++++
                      ++

About me

I'm Saud

OpenShift Developer Console @ Red Hat

Vim user

Undo?

I've been thinking a lot about undo

  • Simple
  • Everywhere
  • Used all the time

Undo as we know it

Undoing is like working on a stack

       .-------.
      _|~~ ~~  |_
    =(_|_______|_)=
      |:::::::::|
      |:::::::[]|
      |o=======.|
 jgs  `"""""""""`

Undo as a stack

      ...
       ^
       |
      ____
     /    \
     | e3 |  ^
     \____/  |
       ^     | Ctrl + Shift + Z
       |     |
      ____
     /    \
     | e2 |
     \____/  |
       ^     | Ctrl + Z
       |     |
      ____   v
     /    \
     | e1 |
     \____/

Undo in vim

Basic stuff

  • Undo with u
  • Redo with <C-r>

Separating edits

What does a single edit mean?

  • Moving insert mode creates a single chunk of edit
  • Can be separated with <C-g>u

Jumping faster

  • Using counts
  • :earlier and :later

Traversing linear history

Using :earlier x{T} and :later x{T}

Where {T}:

  • s - Seconds
  • m - Minutes
  • h - Hours
  • d - Days
  • f - Last saves

How I work everyday

###Step 1:

Open vim

###Step 2:

:later 8h

###Step 3:

Close vim

That's It?

(No)

What about lost edits?

...

Branching off

Nothing is lost in vim

Let me tell you a secret...

It's actually a tree!

      ...
       ^       ...
       |        ^
      ____      |
     /    \     |
     | e3 |    ____
     \____/   /    \
       ^      | e4 |
       |      \____/
      ____    /
     /    \  /
     | e2 |--
     \____/
       ^
       |
      ____
     /    \
     | e1 |
     \____/

Wait, it's a tree?

               __________________
              |                  |
              | ALWAYS HAS BEEN! |
               __________________
       _..._       | /
      .'     '.    |/
     /    .-""-\
   .-|   /:.   |      ,_______^
   |  \  |:.   /.-'-./    _____)
   | .-'-;:__.'    =/  /_/
   .'=  *=|==== _.='__/
  /   _.  |    ;
 ;-.-'|    \   |
/   | \    _\  _\
\__/'._;.  ==' ==\
         \    \   |
         /    /   /
         /-._/-._/
  jgs    \   `\  \
          `-._/._/

Navigating branches

Switch to a older branch with g-.

Switch to an newer branch with g+.

It acts like regular undo until a split is encountered.

Navigating branches

         ^
         |               ...
         | <>             |
         | |              <>
     ^   | <>   <>    <>  |
     |   | |    |     |   <>
<C-R>|   | <>   <>    <>  /
     |   | |    |     /  /
         | <>   <> - '  /
     |   | |    |      /
   u |   | <>   <> - '
     |   | |    /
     v   | <>- '
         | |
         | <>
         *------------------>

              <---      --->
               g-        g+

Jump to a branch

  • List branches with :undol[ist]
  • Jump with :u {num}

It's a bit like Primer

             _____
          _.'_____`._
        .'.-'  12 `-.`.
       /,' 11      1 `.\
      // 10      /   2 \\
     ;;         /       ::
     || 9  ----O      3 ||
     ::                 ;;
      \\ 8           4 //
       \`. 7       5 ,'/
        '.`-.__6__.-'.'
         ((-._____.-))
         _))       ((_
        '--'SSt    '--'

Limits and persistence

Maximum changes: undolevels

Have persistence: undofile and undodir

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment