brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: :earlier 10m takes a Vim file back to how it looked ten minutes ago

Vim
~/tips/tip-vim-earlier-later-undo-by-time

:earlier 10m

Jumps the text back or forward in time instead of stepping through undo one change at a time.

Pressing u twenty times and overshooting is a very Vim way to lose an afternoon. There is a better undo, and it works with a clock.

Vim timestamps every change, so you can go back by time instead of by step:

:earlier 10m
:later 5m

:earlier 10m returns the text to roughly how it was ten minutes ago. :later 5m moves forward again. The units are s, m, h and d, and :later takes the same arguments. If you jump too far, :later is your way back, so nothing is lost by trying.

Not sure what time to pick? :undolist shows the leaves of the undo tree with a “when” column, and those moments work with :earlier and :later too.

I would reach for this after a messy refactor where I know the file was fine a while ago but cannot say how many edits back that was. Counting steps is guessing, minutes are something I can estimate.

Two caveats from the Vim help. The jump is approximate, not exact to the second. And states you can no longer reach are gone once the undo information has been cleared, which happens for example when you go past 'undolevels'. Quitting Vim also throws the history away unless you turned on persistent undo with :set undofile.

Further reading: Vim help: undo.txt

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Vim tips :%s/<Ctrl-r><Ctrl-w>/newname/g /oldname find . -name "*.conf" | vim – vim --clean file.txt :set textwidth=72 All Vim tips (45) All topics

Link