1

I am using system() to change a buffer's file. I apply the same change to the buffer itself. I can't just reload the buffer because there are other unsaved changes.

When I come to write the buffer, Vim gives me:

WARNING: The file has changed since reading it!!!
Do you really want to write to it (y/n)?

I do really want to write to it.

How can I avoid this warning?

I have tried adding a FileChangedShell autocmd which doesn't do anything, but it does not get fired for system() calls. So I added a silent execute ':!', which seems to trigger the autocmd, but the screen gets all messed up – and vim still shows the warning.

Any ideas would be greatly appreciated!

3
  • It's none of my business, but I'd love to know the context of why you're doing this.
    – Rich
    Feb 21, 2018 at 13:17
  • I'm trying to add stashing to vim-gitgutter. Feb 21, 2018 at 15:52
  • Nice. Thanks for satisfying my curiosity. I just read your "improvements" post, BTW, and thought, "I recognise that name..." So then I came back here to say: thanks for making git-gutter: it's one of my favourite plugins!
    – Rich
    Feb 21, 2018 at 17:25

1 Answer 1

1

One solution is to store the contents of the buffer, reload the file (to make vim happy), then restore the contents.

let lastline = line('$')
let bufcontents = getline(1, lastline)
edit!
call setline(1, bufcontents)
if line('$') > lastline
  execute lastline+1.',$:d _'
endif

Your Answer

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

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