Daan Bakker
Daan Bakker

Reputation: 6332

How to find out if InsertLeave was the result of a change

In Vim there are basically two types of commands that can make it go into insert mode:

Commands that just add something, such as: i, I, a, A (apart from using backspace). Or, that also remove a piece of text, such as: c[motion], C, s, v[motions]s.

I would like to hook the InsertLeave event, but in my code I need to know which type of change it was (an insert like i, or a change like cw). Is there any way to find that out?

Upvotes: 4

Views: 114

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172590

Would i<BS><BS><BS>bar count as insertion or change? If the latter, you could :undo the change on InsertLeave, store the lines affected by it (i.e. '[,']), :redo, then compare both sets. If there's "just more text", it's an insertion, else a change.

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172590

There is one difference that you may be able to exploit: The change commands all modify a register (unless the black-hole register was explicitly specified by prepending "_), whereas insertions do not (well, except for ".).

If you take a "snapshot" of the default register before (e.g. with a CursorMoved,CursorHold combo) and compare the contents on InsertLeave, you can find out.

Upvotes: 0

Related Questions