24

The default undo behavior in Evil is too coarse-grained because it considers anything taking place between entering insert mode and leaving it as one edit operation. When you enter a whole paragraph of text during one insert and then execute undo, the whole paragraph is removed. In contrast to that, Vim starts a new undo unit whenever you move the cursor in insert mode by means other than entering text.

Evil has a customization variable that can be used to get a more fine-grained undo history:

(setq evil-want-fine-undo t)

With this setting, Evil starts a new undo unit when the cursor is moved in insert mode, just like Vim does. However, this setting also messes with how the replace operation is handled: if you replace a word using cw, you have to undo twice to restore the original word: once for removing the new word and once for reinserting the original word. This is inconsistent with Vim and doesn't make much sense because replace should be atomic.

The question: How can I configure Evil and undo-tree in order to get Vim's undo behavior?

4
  • 1
    File an issue here: bitbucket.org/lyro/evil/issues?status=new&status=open (Evil generally considers divergences from vim to be bugs).
    – shosti
    Commented Nov 10, 2014 at 19:41
  • @shosti, you are right but the existence of the customization variable suggests that they decided to make an exception to that rule in the present case. I'll file a bug anyway. Let's see what happens.
    – tmalsburg
    Commented Nov 10, 2014 at 19:49
  • I think the customization variable is actually to get more Emacs-like undo behavior (Emacs doesn't do atomic operations in the same way as vim).
    – shosti
    Commented Nov 10, 2014 at 20:13
  • 1
    Here's the new issue: bitbucket.org/lyro/evil/issue/444/…
    – tmalsburg
    Commented Nov 11, 2014 at 20:46

3 Answers 3

14

Since @shosti pointed out that Evil considers deviation from Vim behavior as bugs, I filed a bug and one of the authors of Evil added a new possible value for evil-want-fine-undo:

(setq evil-want-fine-undo 'fine)

With this setting, you get new undo units when moving the cursor in insert mode, but replace operations are undone in one step. As far as I can tell this is consistent with Vim. See here for details.

1
  • 5
    This solution is not anymore recommended because the 'fine setting has been deprecated.
    – tmalsburg
    Commented Jul 31, 2016 at 20:23
7

According to the variable description (see C-h v evil-want-fine-undo) the value fine is not supported any more due to inconsistent behaviour.

As I use undo to undo typos or the mentioned setting helped my allot.

(setq evil-want-fine-undo t) 

I had trouble undoing errors in org-table formula editing, since you don't have to leave the insert mode if you want to change to *Edit Forumlas* with C-'.

2
  • Thanks for the update. I requested that the issue on Bitbucket is reopened.
    – tmalsburg
    Commented Jul 31, 2016 at 20:23
  • Doesn't work for me using Doom Emacs with 27.2.
    – gdonald
    Commented Mar 8, 2022 at 1:35
2

Setting simultaneously

(setq evil-want-fine-undo t) 

and

(advice-add 'undo-auto--last-boundary-amalgamating-number
            :override #'ignore)

as suggested in this answer, gives very fine granularity, that is, undoing each character entered in insert mode one by one.

5
  • Neither of these options work for me using Doom Emacs with 27.2. (setq evil-want-fine-undo t) does break up the large undo cadence, but neither make it single keystroke like I want.
    – gdonald
    Commented Mar 8, 2022 at 1:32
  • Weird. I am using emacs 28.0.91 and they do precisely that.To which files have you added those lines? I suggest adding the (advice ...) one to .doom.d/config.el and the (advice-add ...) one to .doom.d/custom.el through the Custom mechanism [SPC h v, select evil-want-fine-undo, follow the Customize link, set the variable to t (or yes, maybe) and then apply and save]. Report if it works.
    – Tera
    Commented Mar 8, 2022 at 17:37
  • I'd just like to stress that my and in the answer is conjunctive. For the behaviour to take effect I've had to add both settings, none of them alone gave me the wanted behaviour.
    – Tera
    Commented Mar 8, 2022 at 17:41
  • I only just learned about s-z. I was using u like the incorrect docs said. emacs.stackexchange.com/questions/70834/…
    – gdonald
    Commented Mar 8, 2022 at 21:03
  • Nice that you found a solution. (Even if I don't understand what you mean by s-z.) I undo things using u, btw.
    – Tera
    Commented Mar 9, 2022 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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