tags:

views:

140

answers:

3

If I'm editing a file in Vim, then some external program changes the file, Vim tells me:

W11: Warning: File "test.erl" has changed since editing started
See ":help W11" for more info.
[O]K, (L)oad File: 

If I Load the file, though, all the undo history is lost.

Is there any way to avoid that?

A: 

I don't see how vim could keep track of something it didn't do.

So, as to the question, I would suggest - source control ... but that's probably not the answer you're looking for.

ldigas
Why not, though? If vim believes the file contains an `a`, then some write a `b` to the file, why should Vim be unable to undo that `b`?
David Wolever
And... Unless I setup some elaborate scheme to commit after every change in Vim... I don't see how source control would help.
David Wolever
And if somebody deletes or moves the file ? Why shouldn Vim be unable to undelete it/or move it back ? ... It's not that simple. Vim uses a buffer principle, i.e. it saves the contents of a file to a buffer during the editing phase. I believe that's why it gives you an option to load the file again (and why it doesn't lock the file). @David (2nd comment) - yes.
ldigas
Maybe I should reiterate a little. If vim locked the file, I guess something like that could be possible. Then he would track all changes that happened to that file. But it doesn't. Vim makes a copy of a file for itself, and lets the file go (it is not locked). When you :w, it writes the copy (buffer) contents to a file. For it to track a file in that way, it would need to ... well, I gather you gather what I'm trying to say by now.
ldigas
The file disappearing is one thing... But I'm not asking about that - I'm asking about a file which has been modified (ie, the contents have been changed).
David Wolever
A: 

G'day,

I'm not sure but does setting autoread, i.e. entering :set autoread leave the undo history for the file when it changes?

Hmmmm. I'm thinking probably not because the change history is maintained as line numbers and vim doesn't know if those line numbers are still relevant to the changed file.

BTW WTF are you editing a file that is being changed by external forces anyway? Sounds dangerous to me. (-:

Rob Wells
by "external forces" hehe :-) I can't help myself thinking of Darth Vader editing a file remotely ...
ldigas
nope, autoread actually is just the equivalent of pressing `L` for you automatically.
Peter
I don't know what David is trying to do, but I've run into this when, for example, I write a bash script. I do my first version, try to run it, and realize I forgot to set the script as executable. If I chmod u+x the script while the file is loaded in vim, vim will helpfully tell me the file has changed and offer to load it, even though only the flags have changed. The same thing if you are using a source-control system that fiddles with the write-permission bits, like Perforce does.
Caleb Huitt - cjhuitt
Yup - that's it exactly, @cjhuitt.
David Wolever
+1  A: 

I don't believe this is possible.

There is a very useful patch available for the vim source code available here that keeps the undo history after exiting vim and restarting. However, I've just tried it and it seems to fail when the file is edited externally. It might be worth contacting the author or reporting a bug on the patch website to see if this can be overcome.

Al
Thanks for the vim-debug link... And I will ask about that on the lists - thanks.
David Wolever
Another useful link is http://repo.or.cz/w/vim_extended.git which has a few other interesting patches as well.
Al