News for hackers. Stuff that matters.
Re:Why is it so hard to see code from 5 minutes ago? by jolmg on Thursday February 18, 2021 @08:38PM (#26188419)
> Why is it so hard to see code from 5 minutes ago while in the middle of a change?

In vim you can do:

  :earlier 5m
to see the code you had 5 minutes ago.

> (1) If you go to a prior state and then make a new change, you can no longer redo and all those changes are lost.

Vim makes a tree, so that doesn't happen with it. When you undo and make a new change, you're just making a new branch. u/Ctrl-r goes from leaf to trunk, but with g-/g+ or :earlier/:later you can walk the whole tree.

Emacs similarly doesn't lose changes in this scenario, but instead of making a tree, it has a ring and undo is an action that can be undone.

> (2) You can not see a side-by-side comparison of the previous version and the latest version.

It's probably not complicated to make a macro or function where you go to a previous version, copy the buffer, paste it in a new one on a new window, return the original buffer to the state you were in, and diff the windows for that side-by-side comparison.

A command like so suffices:

  :earlier 5m | %y | later 5m | diffthis | vnew | put | 1d | diffthis
> (3) There is no visual indicator of where you are are in your undo/redo history.

Not by default, no. :undolist provides some info, though, and the vimscript undotree() function probably provides all the state of the undotree. There might be plugins that somehow present this info in the interface.

> (5) I have found many actions in code editors that do not get added to the undo stack (e.g., changing a debugger option), which caused me problems in the midst of an annoying bug.

In vim, only stuff that changes the buffer is added to the undo tree. Navigation or changes of the state of the editor (e.g. editor options or vimscript variables) aren't added. Are there really editors where adding actions that aren't changes make sense?

> (6) There is no indication of what steps were "big" or how long ago they happened.

That's also true in vim it seems, at least how "big" they where. Each step does have a timestamp, though.

> (6) It is tedious to backtrack one small step at a time.

You can backtrack by however many steps you want. :earlier also supports specifying by seconds, minutes, hours, days, or file writes.

Most cool of all is that vim can persist the undo history. I don't know how common that is among editors, but in vim you can go to a file you haven't opened in years and undo it all the way to its beginning.

Re:Why is it so hard to see code from 5 minutes ago? by justinnhli on Thursday February 18, 2021 @08:50PM (#26188527)
> > (3) There is no visual indicator of where you are are in your undo/redo history.

> There might be plugins that somehow present this info in the interface.

The undotree vim plugin [1] does this, and gives both the file at the time as well as a diff of what changed.

[1]: https://github.com/mbbill/undotree

Re:Why is it so hard to see code from 5 minutes ago? by dorian-graph on Friday February 19, 2021 @07:19AM (#26192341)
> undotree

I was going to mention this plugin. I haven't fully mastered quickly jumping around in it, but it's one of those things that when it's useful, it's VERY useful.

Re:Why is it so hard to see code from 5 minutes ago? by justinnhli on Friday February 19, 2021 @06:52PM (#26199643)
Ditto. Unless I have to go back a lot of edits, I don't actually use the plugin that often. I've mapped my undo/redo keys to traverse the undo tree instead of the undo stack:

nnoremap u g-

nnoremap <C-r> g+

It takes a little bit of getting use to, but it takes care of most instance where I need a previous edit from a minute ago.

Re:Why is it so hard to see code from 5 minutes ago? by frosted-flakes on Thursday February 18, 2021 @10:33PM (#26189319)
> Are there really editors where adding actions that aren't changes make sense?

Adobe Photoshop is one. Selection changes don't modify the document at all, but they are part of the undo stack. That's because selecting a part of an image isn't as trivial as selecting text.

Sometimes I wish the current selection could be automatically saved in the file too so you could start where you left off if you were halfway through making a complicated selection (I know you can manually save selections, but that's an added step).

Re:Why is it so hard to see code from 5 minutes ago? by chrismorgan on Friday February 19, 2021 @01:58AM (#26190474)
I don’t know about Photoshop, but the selection is part of the document in GIMP.
Re:Why is it so hard to see code from 5 minutes ago? by doix on Friday February 19, 2021 @04:06AM (#26191063)
> Most cool of all is that vim can persist the undo history. I don't know how common that is among editors, but in vim you can go to a file you haven't opened in years and undo it all the way to its beginning.

I absolutely love this feature. I regularly open a file and undo/redo to figure out where I was editing last.

Re:Why is it so hard to see code from 5 minutes ago? by Moru on Friday February 19, 2021 @05:20AM (#26191468)
NetBeans also have this but it's set to 7 days as default. Never looked into this before, just thought it was broken but you can save it for eternity if you want :-)
Re:Why is it so hard to see code from 5 minutes ago? by iaabtpbtpnn on Thursday February 18, 2021 @11:25PM (#26189717)
Wow, I have a lot of vim learning to do. Your pipeline is far beyond my level.
Re:Why is it so hard to see code from 5 minutes ago? by jolmg on Thursday February 18, 2021 @11:31PM (#26189765)
| in vimscript is like ; in other languages. I'm not sure pipeline is a good name to call it. It's just a list of commands run in sequence.
Re:Why is it so hard to see code from 5 minutes ago? by apocolyps6 on Friday February 19, 2021 @09:50AM (#26193731)
> In vim, only stuff that changes the buffer is added to the undo tree.

There is also a command history, and jumplist (movement cursor position history) for things that don't go in the diff tree.

Re:Why is it so hard to see code from 5 minutes ago? by im3w1l on Friday February 19, 2021 @03:35AM (#26190898)
> Emacs similarly doesn't lose changes in this scenario, but instead of making a tree, it has a ring and undo is an action that can be undone.

Emacs default undo system is really, really bad. Like worse than the "standard" one. But after installing the emacs undotree it becomes sane.

Re:Why is it so hard to see code from 5 minutes ago? by b3n on Friday February 19, 2021 @05:39AM (#26191601)
What is bad about it? It always goes back to the previous state, and if you want to skip previous undo states (so you don't undo an undo) you can use `M-x undo-only`.
Re:Why is it so hard to see code from 5 minutes ago? by im3w1l on Friday February 19, 2021 @07:00AM (#26192188)
It's been over a decade since my bad experience so take my words with a grain of salt. Iirc, the issues I had was that I made a huge history of undos-followed-by-redos-followed by-undos-etc etc and the more I navigated my history the more tangled and out of control it became. I think I also had a problem with often accidentally switching from undo to redo mode. Emacs documentation recommends using C-f (forward-character) to switch to redo mode, but in my opinion attaching side effect to movement is awful.
Re:Why is it so hard to see code from 5 minutes ago? by jolmg on Friday February 19, 2021 @11:28AM (#26194784)
I agree. It's cool that it's lossless, but doing a lot of undos and redos makes the history quickly unwieldy to navigate. I much prefer vim's tree implementation.

I wouldn't say that it's worse than the standard, though. Unwieldy as it may be, I like that I can just keep on pressing C-/ and eventually get to the state I want without risk that I may have lost it because I accidentally made a change after a series of undos.

> Emacs documentation recommends using C-f (forward-character) to switch to redo mode, but in my opinion attaching side effect to movement is awful.

I use C-g (keyboard-quit) for that, which is the general let's-not-do-this-anymore keybinding. I think the documentation mentions C-f as an example keybinding that does the needful without much thought on being the "best" keybinding for it.

Re:Why is it so hard to see code from 5 minutes ago? by b3n on Friday February 19, 2021 @05:56PM (#26199135)
> doing a lot of undos and redos makes the history quickly unwieldy to navigate

Are you aware of the `undo-only` (and `undo-redo`) functions? They make it a breeze to navigate even the most complex undo history.

Re:Why is it so hard to see code from 5 minutes ago? by jolmg on Friday February 19, 2021 @07:37PM (#26200067)
Actually, I wasn't. Thanks for that.

(undo-redo) doesn't seem to be defined in stock Emacs, but undo-tree defines (undo-tree-redo), which does the needful.

My comment was more about lamenting the defaults in comparison with vim's, but it's true that it doesn't take much configuration to make it much better.

Re:Why is it so hard to see code from 5 minutes ago? by im3w1l on Saturday February 20, 2021 @03:56AM (#26202762)
undo-only doesn't have a keybind which makes it inconvient to use many times in a row. You could define one yourself I suppose, but at that point why not just get undo-tree?
Re:Why is it so hard to see code from 5 minutes ago? by im3w1l on Saturday February 20, 2021 @03:58AM (#26202774)
The issue I have is that I want to be able to use C-f and C-g and many others without it changing anything about my history.
Re:Why is it so hard to see code from 5 minutes ago? by john4532452 on Friday February 19, 2021 @05:58AM (#26191743)
This seems cool. Is there an equivalent feature in emacs ?
Re:Why is it so hard to see code from 5 minutes ago? by jolmg on Friday February 19, 2021 @11:59AM (#26195114)
What particular feature? :earlier 5m? I'm afraid not. Emacs' C-/ acts like :earlier/:later/g-/g+ in that you can navigate the history across undos, but there's no support for specifying by number of minutes. I don't know if there's a package for it though. Evil-mode (vim emulation in emacs) doesn't seem to have support for that either.

I can't imagine it'd be that complicated to add it though, if you really want it. I think it'd just be a matter of hooking to the undo function and maintaining a list of timestamps for each item added to the undo list, then adding a command that lets you specify the number of minutes and figures out the correct undo item it should go back to by using that list of timestamps.

Re:Why is it so hard to see code from 5 minutes ago? by nexthash on Thursday February 18, 2021 @08:42PM (#26188452)
This is excellent... for Vim users. A lot of developers either are not willing or haven't gotten around to learning the complexities of Vim, and use other IDEs like JetBrains or Eclipse. This workflow won't work for them.
Re:Why is it so hard to see code from 5 minutes ago? by enobrev on Thursday February 18, 2021 @08:53PM (#26188544)
JetBrains' various apps can do this as well with a feature called "Show Local History". I'm not sure where it is in the menus but you can hit shift twice and type it in.

It automatically commits after every change since the IDE was opened, and can easily be diffed and reverted as needed. Reverting saves another entry, so you can revert back to the future as well.

Re:Why is it so hard to see code from 5 minutes ago? by voodootrucker on Thursday February 18, 2021 @09:37PM (#26188891)
This. I've seen developers _think_ they lost code in Android Studio and spend hours trying to recreate it when it was in local history all along...
Re:Why is it so hard to see code from 5 minutes ago? by Quekid5 on Thursday February 18, 2021 @09:01PM (#26188603)
It's in the context menu for files/classes at least.
Re:Why is it so hard to see code from 5 minutes ago? by cmehdy on Thursday February 18, 2021 @09:48PM (#26188991)
IIRC you can right-click on the column with line numbers to the left of the main editor and you'll have that option too
Re:Why is it so hard to see code from 5 minutes ago? by watwut on Friday February 19, 2021 @03:24AM (#26190834)
Eclipse has the same thing.
Re:Why is it so hard to see code from 5 minutes ago? by alpaca128 on Friday February 19, 2021 @02:49AM (#26190658)
If you learn Vim like anything else - slowly and starting with the basics - it's not more complicated than figuring out any IDE. And once you know the basics you are fully in control how deep you want to go.

Personally I have found Vim more approachable than IDEs actually. It doesn't throw all those overloaded toolbars at you according to some one-size-fits-all principle. For me it went so far that last year I actually did a Java project in Vim instead of an IDE for the first time, because the latter had grown to be so frustrating to use.

Re:Why is it so hard to see code from 5 minutes ago? by 112 on Friday February 19, 2021 @04:50AM (#26191298)
If writing text more than 4 hours / day is what you do for a living, and you have at least 10 more years left of doing that, not learning Vim is just lazy. Vim gives you back enough time to think about what you're writing.

Also, if you go cold turkey, you'll be productive in less than a month, and arrive at a good workflow that fits you in less than six months, flow that you will improve over the years in amazing ways.

Re:Why is it so hard to see code from 5 minutes ago? by hnlmorg on Friday February 19, 2021 @05:33AM (#26191558)
And there are a lot of people, like myself, who do use vim regularly and somehow completely missed this feature. For those people the OPs post is excellent.

Likewise, if you have any recommendations for your preferred workflow I'm sure some will find it useful. Hopefully those who don't use your preferred $EDITOR don't then rush to complain that not everyone uses $EDITOR. ;)

Re:Why is it so hard to see code from 5 minutes ago? by ramraj07 on Thursday February 18, 2021 @09:05PM (#26188633)
I am one of those "not willing" people. I encounter the same issue discussed here without even thinking about it, and solve it as such:

1. Realize I need to check how my new code looked like 5 min back. 2. Copy full file into clipboard, undo by a few steps 3. Do a temp commit on GitHub desktop 4. Paste the new code back and look at the diff on GitHub desktop, and if needed undo the temp commit from above.

Is this more clicks than the vin shortcut? Sure. But I don't have to go learn the internal tree structure representation of my text editor and spend my time in that universe. I have found a way to do what I need to with the tools I have in hand.

This is the same way how the majority of finance runs on excel when they could do better with better programing. In the end whether you get the job done in fields where you're not billed by the second is not dependent on how well you use your tools necessarily.

The other analogy is cars - for most people it's something that gets them places, for others it's a way of life if not at least a more involved proposition.

Re:Why is it so hard to see code from 5 minutes ago? by buzzerbetrayed on Thursday February 18, 2021 @09:53PM (#26189031)
> The other analogy is cars - for most people it's something that gets them places, for others it's a way of life if not at least a more involved proposition.

But if code is your way of life, which it is for many people here, it is beneficial to learn the tools that allow you to do it easier. If someone can see how their code was 5 minutes ago in a few keystrokes, they’re going to take advantage of that significantly more frequently than someone using your cumbersome method.

Re:Why is it so hard to see code from 5 minutes ago? by xyzzy_plugh on Friday February 19, 2021 @03:41AM (#26190934)
As some other comments suggest, vim is the gift that keeps on giving. I'm not going to try and convince you to use it -- do whatever you want.

But for me, every little thing I learn in vim pays off... a thousand times? Macros alone have probably saved me a cumulative few hundred hours. And with every other little thing I learn, compounds with this effect. I write macros much better than I did five years ago, but I still surely have tons of stuff to learn. No IDE will ever compete because vim is just four keystrokes away at any time, no matter where my terminal is.

Re:Why is it so hard to see code from 5 minutes ago? by pharke on Friday February 19, 2021 @05:02AM (#26191370)
Not so sure about this, I think it depends on how frequently you have to do that task. If you're doing it every day or even every week you could potentially be saving a huge amount of time and effort by using a tool that does the job well. It's sort of like using a random heavy object as a hammer, sure it's fine in a pinch if you need to occasionally hang a picture but if you're working as a carpenter it's counter productive. Comparing it to the notorious abuse of excel spreadsheets in finance is not doing the argument any favours.
Re:Why is it so hard to see code from 5 minutes ago? by flipcoder on Thursday February 18, 2021 @11:18PM (#26189658)
It's not as hard to learn as people say it is. It's just weird at first because it's modal and different than what they're used to. You can get away with using it pretty naively and still get a productivity gain.