Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Only show “file has been changed” warning if content is different

Tags:

vim

Is it possible to setup Vim so that it will only show:

WARNING: The file has been changed since reading it!!!

If the file is actually different, not just when the timestamp changes?

For example, I'll quite frequently background Vim (^Z), roll back to an older version of a file (eg, to run the test suite against it), revert back to the current version and fg Vim again… But I still get the “file has changed” warning because, even though the content is identical, the timestamp has changed.

like image 1000
David Wolever Avatar asked Nov 29 '10 01:11

David Wolever


People also ask

Why does Vim say the file has been changed since reading?

You changed branches or performed some other Git operation that updated the file you have open, and that means a different modification time. Vim compares it to that of when it was last read and gives you this message if they differ: WARNING: The file has been changed since reading it!!! Do you really want to write to it (y/n)?

What does |filechangedshell| do in Vim?

Vim will run any associated |FileChangedShell| autocommands or display a warning for any files that have changed. In the GUI this happens when Vim regains input focus.

What is the use of the focusgained event in Vim?

Vim will run any associated |FileChangedShell| autocommands or display a warning for any files that have changed. In the GUI this happens when Vim regains input focus. So the FocusGained event should not be needed at all.

How do I force a file to reload in Vim?

If you have made modifications to the file, you can use :edit! to force the reload of the current file (you will lose your modifications). The command :edit can be abbreviated by :e. The force-edit can thus be done by :e! to make Vim automatically refresh any files that haven't been edited by Vim.


1 Answers

If you try on vim 7.3

:help timestamp

It is said that
When Vim notices the timestamp of a file has changed, and the file is being edited in a buffer but has not changed, Vim checks if the contents of the file is equal. This is done by reading the file again (into a hidden buffer, which is immediately deleted again) and comparing the text. If the text is equal, you will get no warning.

So I guess that in your case, something has changed other than the file timestamp ( or there is a bug in Vim).

In my case, I often get that message when I check out files : they change from "read only" to "read write" even if their content has not changed.

So I guess that if the properties of a file are affected, it is considered "changed" even if the content is the same.

like image 146
Xavier T. Avatar answered Oct 21 '22 18:10

Xavier T.