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?

tmalsburg
  • 2,540
  • 1
  • 14
  • 29
  • 1
    File an issue here: https://bitbucket.org/lyro/evil/issues?status=new&status=open (Evil generally considers divergences from vim to be bugs). – shosti Nov 10 '14 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 Nov 10 '14 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 Nov 10 '14 at 20:13
  • 1
    Here's the new issue: https://bitbucket.org/lyro/evil/issue/444/evils-undo-granularity-is-too-coarse – tmalsburg Nov 11 '14 at 20:46

3 Answers3

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.

tmalsburg
  • 2,540
  • 1
  • 14
  • 29
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-'.

Jörn Reimerdes
  • 171
  • 1
  • 2
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.

Tera
  • 41
  • 6
  • 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 Mar 08 '22 at 01: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 Mar 08 '22 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 Mar 08 '22 at 17:41
  • I only just learned about `s-z`. I was using `u` like the incorrect docs said. https://emacs.stackexchange.com/questions/70834/doom-emacs-single-keystroke-undo/70835#comment114088_70835 – gdonald Mar 08 '22 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 Mar 09 '22 at 02:30