I’m sure it happened to you too.
Just remember. That horrible moment when you time-travelled using undo
- to copy and paste that line you deleted twenty minutes ago but for some reason you really, really, need it now - and then you hit a key and insert a letter.
And just like that, your way back is gone. The only appropriate reaction to that is either eating your computer or turning into a hulk and going on a neighbourhood-destroying rampage.
Luckily Vim, my editor of choice, subscribes to the multiverse theory and it sees history as a tree. And you see, it’s actually right. Frankly, the whole idea of undo history being linear is, well, just ridiculous and has caused a lot of misery (yea, I’m looking at you, Sublime!).
There’s a number of plugins one can use to display the history tree in Vim. My personal favourite is gundo. But hey, I’m sure undotree is just as good, I just haven't tried it yet.
Time Travelling Without Worries
But here's the best part - knowing the true nature of history, we can combo it with another cool feature of Vim - persistent undo - to be able to travel in time there and back without fear of losing anything!
In other words, if you do:
mkdir -p ~/.vim/undodir
and then add:
set undofile
set undodir=~/.vim/undodir
to your ~/.vimrc
, you get a file-backed infinite undo. And even if you undo like a madman and then edit something, you will not lose your way back to where you’ve been. Which is pretty much a developer’s (or anyone’s, really) text-editing nirvana.
Enhance you calm and enjoy a bit saner coding.
Do you think time travel works differently? I’m @tomas_brambora!