Vim Buffer has been modified [duplicate]
Asked Answered
C

1

4

I am trying to implement an AutoCmd in Vim that needs to be executed when the current buffer has been modified.

I have read through all the events available for AutoCmd in the docs but could not find something that would help determine when a buffer was modified or not.

This doesn't necessarily mean "when a buffer was written" because the action I need to trigger needs to be called when the buffer has been modified including being written.

Any ideas on how to implement a BufIsModified that could achieve this objective?

Note: not a duplicate of What is a vimrc function to determine if a buffer has been modified?, since this question is about an autocmd and not only the buffer modified check

Coccidiosis answered 9/4, 2011 at 18:16 Comment(5)
Possible duplicate of: https://mcmap.net/q/1423360/-what-is-a-vimrc-function-to-determine-if-a-buffer-has-been-modifiedImmemorial
this is odd, how come this question is a duplicate of another question if this was asked first?Coccidiosis
What duplicate? How do I get https://mcmap.net/q/1423360/-what-is-a-vimrc-function-to-determine-if-a-buffer-has-been-modified to work as an autocmd? (Reason I came here for..)Contrary
Actual Solution: let modified = getbufvar(bufname('%'), "&mod")Nominative
@Robert Harvey This is not a duplicate of the linked question.Ramah
T
2

The only thing popped into my mind is to use the CursorMoved and CursorMovedI events.

HTH

Titoism answered 9/4, 2011 at 19:17 Comment(4)
Sounds to me like that would work, and within the event function test the &modified vim variable, which will be 0 if unchanged or 1 if there has been change since last write to disk.Lutes
You could run a bunch of Ex commands to totally transform the buffer without ever moving the cursor or entering insert mode.Gev
I'm willing to make a compromise with Ex commands. The answer combined with Herbert's comments does what I wanted. Thanks!Coccidiosis
@Gev Your described situation could be considered rare, the EX command that modifies the buffer will very likely cause the cursor moving as well.Graciagracie

© 2022 - 2024 — McMap. All rights reserved.