6

Say I do d4b, then realize I only needed d3b. If I hit u, it'll only undo the d part, and I'll still be 4 words back as if I just did 4b. I'd like to end up exactly where I was before I executed d4b.

Is there a way to make u include the movements, or is there a better way to undo the entire command?

  • 2
    Posting this as a comment not an answer but i think the general solution to this sort of problem is constructing commands based on the granularity you need to repeat/undo. So here, instead of d4b you might just db then use the dot command to repeat. You could then do count dot (ie 3.) to delete 3 words (in addition to the word you deleted with db). Or you might choose to just delete them one at a time with dot, and ctrl-r if you delete too many. Etc. – user859 Jul 18 '17 at 9:34
2

You're slightly misunderstanding what Vim is doing here. When you do d4b, there is only a single action, which deletes from the current position to the position where 4b would jump to.

The behaviour that you see when you undo with u is not that Vim is undoing the d but leaving the 4b motion intact. What's actually happening is that when Vim performs an undo, it places the cursor at the beginning of the change that is undone.

(If you do d4wu, you will see that the cursor stays the same location throughout: it does not end up at the end of the motion, as your interpretation would suggest.)

If you're writing a script/function there are workarounds you can use to move the cursor back to its location prior to the undo, but unfortunately, there's no general command that can always do this: it requires you to know before you make the edit that you're going to want the cursor position restored after an undo.

Unless you are lucky and find that the location happens to already be saved in an automatic mark as the result of another action (see :help mark-motions), I'm afraid it's not possible to restore the location of the cursor in the way you desire.

1

If you do the same thing via visual mode: v3bd, then u, you can jump back to where you started by using the mark `> (that's backtick).

Look up :h marks to learn more; > corresponds to the end of visual selection, backtick means you want specific position, and not the beginning of line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.