VSCodeVim / Vim

:star: Vim for Visual Studio Code
http://aka.ms/vscodevim
MIT License
13.88k stars 1.31k forks source link

VSCodeVim Undo stack should be synced with VSCode undo stack #1490

Open casey-speer opened 7 years ago

casey-speer commented 7 years ago

VSCodeVim undo is not synced with VSCode undo. I've provided a couple different issues/test cases below to illustrate what I mean:

Simple undo VSCode and VSCodeVim seem to maintain different undo stacks, such that pressing u in VIM adds to the VSCode undo stack so that doing a VSCode undo (CMD-Z) undoes the VIM undo. u and CMD-Z should have the same function and share a common undo stack.

Search Replace When doing a vim-based search and replace, one change with all the replacements is pushed to the VSCodeVim undo stack, whereas one change for each replacement is pushed to the VSCode undo stack. It's easy to get confused about the state of the document if you make a replacement, then press CMD-Z thinking you've reversed all the changes, make some other changes, then have to backtrack to revert the rest of the replacements.

I'm not familiar with the extension architecture for VSCode, so I'm not sure what is technically possible here.

I can break these off into separate issues if you'd prefer.

thumbs-up 👍 this issue if it personally affects you! You can do this by clicking on the emoji-face on the top right of this post. Issues with more thumbs-up will be prioritized.


Simple undo

What did you do? Press i. Type some text: PIZZA. Press escape. Press i. Type some more text: IS GREAT. Press the u key to undo. Press CMD-Z.

What did you expect to happen? " IS GREAT" should disappear upon pressing u. "PIZZA" should disappear upon pressing CMD-Z so we're left with an empty string.

What happened instead? " IS GREAT" disappeared and then reappeared upon pressing CMD-Z so we're left with "PIZZA IS GREAT".

Search and replace

What did you do? Given a 50-line document with the string "Pizza" on each line, type %s/Pizza/Hot Dogs/ from the VSCodeVim command line. Press CMD-Z.

What did you expect to happen? "Pizza" should appear on every line.

What happened instead? "Hot Dogs" appears on every line except the last one which shows "Pizza". Similarly, if you first undo with u then press CMD-Z, "Pizza" appears on every line except the first one which shows "Hot Dogs".

Technical details:

v-au commented 3 years ago

There's a newer VS Code extension called Neo Vim

With this you get vim's actual undo/redo tree (not just a stack). I haven't tried it yet, but I suspect that vim's (neovim's) persistent history tree will also work (VS Code will have no idea it exists, neovim will simply be updating the text buffers in VS Code, so the history state effectively lives outside of VS Code and you never touch VS Code's history yourself).

FWIW, it doesn't appear to run quite well yet, see this issue.

praksekar commented 3 years ago

Maybe I am missing something here, but instead of trying to have Vim use VS Code's undo stack, why not have VS Code use Vim's undo stack and undo/redo commands? i.e. Ctrl-Z is mapped to "vim-undo". It seems to me that Vim's undo implementation can perform a super set of the tasks that VS Code's does, so I don't see why this could go wrong.

skplunkerin commented 3 years ago

Maybe I am missing something here, but instead of trying to have Vim use VS Code's undo stack, why not have VS Code use Vim's undo stack and undo/redo commands? i.e. Ctrl-Z is mapped to "vim-undo". It seems to me that Vim's undo implementation can perform a super set of the tasks that VS Code's does, so I don't see why this could go wrong.

I believe the issue is that the extension can't just tap into vim-undo/redo, but instead is trying to emulate how vim-undo/redo works and that the emulation of this isn't identical enough to vim-undo/redo... so since it doesn't work as expected (how we all know/love how it works in vim), the suggestions have been to not use it until it's fixed, and to use VS Code's undo stack since that one works more predictably/consistently.

wakywayne commented 2 years ago

I can personally say that vims undo has left me screwed twice already.

Thankfully I had git committed recently both times. If I hadn't my code would've been left looking like some kind of heiroglifics. Yes, it was that bad, letters became scattered all over the place. And redoing actually made it worse.

All this when the only changes I made were very minor. Has anyone else experienced this?

skplunkerin commented 2 years ago

@wakywayne yes, that's pretty much the whole point of this issue about the VSCode-Vim undo being unreliable. And I'm glad Git exists, I have also been saved from Git due to this.

You will want to map your vim undo to use VSCode's undo (and redo) until this issue is fixed.

trusktr commented 2 years ago

@J-Fields would you kindly mind putting vscodevim's undo/redo behind an option, disabled by default, and marked "experimental", with VS Code's undo/redo being the default?

max-sixty commented 2 years ago

@J-Fields would you kindly mind putting vscodevim's undo/redo behind an option, disabled by default, and marked "experimental", with VS Code's undo/redo being the default?

Could I politely suggest that people can a) discuss ideas, and b) submit pull requests. But asking specific people to complete tasks is not in the spirit of open-source.

wilbur4321 commented 2 years ago

@wakywayne yes, that's pretty much the whole point of this issue about the VSCode-Vim undo being unreliable. And I'm glad Git exists, I have also been saved from Git due to this.

You will want to map your vim undo to use VSCode's undo (and redo) until this issue is fixed.

To save people time, the workaround/solution here seems to work for me (very basic testing ONLY). Handily, vscode also no longer shows a file as modified after undoing my changes!

https://github.com/VSCodeVim/Vim/issues/2007#issuecomment-529924611

zvalcav commented 1 year ago

@atulpatildbz As has been mentioned several times in the thread you are replying to, this doesn't work with macros. There's a few other commands and things this doesn't work with also, tabdo, many more.

Maybe it doesn't allow something, maybe it breaks something. Yet @atulpatildbz gave the freedom of choice to every user of your extension. Since last update of VSCode there are some issues with u(ndo) in vim. Remaping to VSCode undo / redo commands helped me get back to work for now.

martinemde commented 1 year ago

Just within the last few days my undo has switched from being perfect to being completely broken. As an example, I will now make a motion like dd and a line will delete. Hitting u immediately after will say "Already at oldest change". Clearly something is broken beyond just this being vscode undo stack vs VIm. Has anyone found a solution to this?

EDIT: I'm nearly certain this sudden change is related to enabling "ruby.format": "rufo". This broke around the same time I was trying to get ruby formatting working. If I comment ruby.format it seemingly stops wrecking my vim undo stack. I'm also trying running without format on save. In the case above with dd u I did not save or format in between, but it's still a possible culprit since I got formatting working correctly right when the undo bug surfaced.

ianchanning commented 1 year ago

@martinemde see #8157

ivnsch commented 1 year ago

Relatedly, is there a way to reduce the granularity of vim's undo? in my experience it's quite high compared to the default one, which I find much better.

rsslldnphy commented 7 months ago

I'm also seeing weirdness with u causing my code to end up in completely garbled states with characters missing etc, and I think it's likely caused by a combination of vs code vim and prettier format on write. The nasty thing about it is that once it happens, neither u nor Cmd+z can get me back to a good state. I just have to try to reconstruct things myself.

<EDIT: I'm nearly certain this sudden change is related to enabling "ruby.format": "rufo"

@martinemde what i'm seeing sounds similar to what you describe, did you ever get to the bottom of this, or was it fixed in #8157?

martinemde commented 7 months ago

I gave up on format on save. I haven't tried again since this disruptive problem. Maybe someone else figured it out?

rsslldnphy commented 7 months ago

@martinemde sad to hear that! ok in the meantime i'm going to try using the below normal mode remapping to remap u and ctrl-r to the native undo/redo commands and see if that avoids the issue.

"vim.normalModeKeyBindingsNonRecursive": [

    {
      "before": ["u"],
      "after": [],
      "commands": [
        {
          "command": "undo",
          "args": []
        }
      ]
    },
    {
      "before": ["<C-r>"],
      "after": [],
      "commands": [
        {
          "command": "redo",
          "args": []
        }
      ]
    }
  ]
ivnsch commented 2 weeks ago

Any progress here?