How can I keep undo history after saving on Visual Studio Code
Asked Answered
H

8

31

I'm using latest version of VS code (1.9.0).

After I undo (ctrl+z) something, and save the file (ctrl+s), then I cannot redo anymore (ctrl+y).

I would like to go back and forth during the editor is running same as Sublime or other editor. Can anyone solve this problem? Thank you.

Headsman answered 3/2, 2017 at 9:6 Comment(7)
Did you ever figure out what this was?Peru
This was the issue with extensions. Find a extention which is not updated following latest version of VS code and remove it.Headsman
I'm having the same problem although I disabled all extensions. Once I save, I cannot redo anything. I'm using 1.19.1. Anyone else knows what to do?Tyner
On mac you can do ⇧⌘ZLillie
Same problem. Disabling extensions doesn't fix it. Problem seems to be an issue if I close the file I'm working on, then go back to it. All undo/redo history is lost.Scatterbrain
Wonder if there's a way to keep an auxiliary set of undo/redo history so that we don't get hoodwinked by a stray save-hook extension (format-on-save) when we temporarily make changes.Oviform
@Scatterbrain Undo/redo is not working on a reopened file. See my updated answer.Gillispie
B
18

For people getting to this question that want to disable the persistence of the undo stack after a file is closed, this too appears to be coming to v1.45.

See https://github.com/microsoft/vscode/issues/94778 and https://github.com/microsoft/vscode/pull/96317 and v1.45 Release Notes:

"files.restoreUndoStack": false

Set to false to disable undo stack items after a file is closed. the default is true.

Boric answered 30/4, 2020 at 16:11 Comment(3)
thank you, you save my life. mhuah <3Athletic
This was very helpful! Is there any such setting that will also drop the undo stack upon SAVING a file?Gibran
@Gibran I don't think there is a setting that will clear an undo stack for a file upon saving it.Boric
M
12

Hope this will work.

shift + command + z 

It will redo what you expected in Sublime Text (command + y )

Markswoman answered 17/1, 2020 at 3:23 Comment(3)
That is great which helpedMarkswoman
this is the anwer I was looking for.Lunnete
You can also remap the keys to command + y in the keyboard shortcutsGilmagilman
M
9

At the bottom left corner of the vscode you have a "timeline", you can undo, and see all saved logs.

Mockheroic answered 20/5, 2022 at 22:0 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Navada
G
4

Undo/redo on

Gillispie answered 3/3, 2017 at 14:8 Comment(2)
Update: It's not working on a reopened file, yet. Unfortunately.Gillispie
Broken on a saved cpp file. Edit: Well now it is. Weird.Bangtail
S
3

In my case it was the Format On Save in settings that caused the undo/redo buffer to be flushed when saving files in VSC.

To get around this you can set Format On Save Mode to

modifications (requires source control like git)

or

modificationsIfAvailable (this also requires source control to help with this issue else VSC will format the entire file and flush the undo/redo buffer)

Voila!!! I had both Format On Save enabled and a redo buffer that was working after saving the file. Truly wonderful!

If this doesn´t help make sure to try to disable all extensions to see if one of them might be causing the buffer flushing.

enter image description here

Sabbath answered 8/3, 2022 at 16:35 Comment(0)
E
1

The problem is confirmed in microsoft/vscode issue 10932 "Undo stack lost" (and linked to the aforementioned issue 7169, as part of the undo/redo issues)

We loose the undo stack very easily when leaving a dirty file in a tab (not visible) and then saving the dirty file:

  • open some tabs and make them dirty
  • wait for a few seconds
  • press "Save All"
  • go to those tabs that where not visible and try to undo
    => you cannot undo

For an opened file, it was solved in VSCode 1.5.0 (Sept. 2016) with commit b872e3d

But for a reopened file, VSCode 1.44 (March 2020) does address the issue, which is listed in the Iteration Plan for March 2020, and assigned to Alexandru Dima: see release notes

Keep Undo stack when reopening files

VS Code will now keep the Undo/Redo stack of a file when the file is closed. When the file is reopened, if its contents hasn't changed since it was closed, the Undo/Redo stack will be restored.

This is very useful if you limit the number of open editors with the workbench.editor.limit.value setting or you accidentally close a file you are working on.

Excitable answered 11/3, 2020 at 17:12 Comment(1)
@Henry I just tested it with the same 1.53.2 (on Windows), and it does work. The release notes mentions "When the file is reopened, if its contents hasn't changed since it was closed, the Undo/Redo stack will be restored." Is it possible your file has changed since it was closed?Excitable
I
1

It's now a feature in the latest version :)

https://code.visualstudio.com/updates/v1_44#_keep-undo-stack-when-reopening-files

Intoxicating answered 20/4, 2020 at 7:10 Comment(1)
That doesn't seem to be persisted outside of workspace memory, unlike say Vim's persistent undo.Ruffina
N
0

I think the answer you're looking for is Save without Formatting.

This command (workbench.action.files.saveWithoutFormatting) will directly save the file as-is.

The issue with regular save, even if you don't have a formatter configured, is that there are still other routines that can run, like removing trailing spaces or ensuring there's a final newline at the end of the file. These changes push onto the undo stack replacing your redo history.

I disagree with the other answers that suggest disabling format-on-save; cleanly formatted code should be the default. When you want to test an older version of the code briefly, "Save without Formatting" lets you do so without interfering with the undo stack.

I have bound "Save without Formatting" to ctrl+shift+s (which I believe was "Save All" by default) and I use it frequently when traversing my undo stack.

I also find this command useful when tools like eslint or scss langauge services start mis-behaving and interrupt the save process with excessive "on save" action delays, allowing me to save quickly now, allow the recompilation to trigger so I can test immediately, then I run the regular save command so that the "on save" actions can run in the background while I'm testing the saved but unformatted code.

Nicknack answered 31/5 at 3:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.