Matthew Woehlke wrote:
Ming Lei wrote:
I have a basic question about vim internal. Let me use an example to
describe:
Say I have a text file of 20Mbytes. I use VIM to insert 3 characters at
offset 1024 bytes from the beginning of the file and then I save the
file. My question is: how does VIM handle the file write and save? Does
VIM rewrite the whole file back to disk/file system? Can anyone describe
the procedure that VIM employs at the granularity of the system call?
PS. Please reply or CC to my email address as well.
AFAIK the only way to *insert* three bytes in the middle of a file is to
re-write the entire file. I'm not aware of any file system for which
this is not the case.
I suppose it is possible that vim itself doesn't have to do the work,
but someone - either the application or the kernel, and ultimately the
storage device - does. Were you asking *where* that work occurs, or *if*
that work occurs?
Ming Lei:
It may be possible to avoid rewriting the part before the insert, but in this
case (at approx. one twenty-thousandth of the total length) it wouldn't make
much of a difference. Remember that your insert modified everything that
follows it: since you move byte 2000 to offset 2003, the byte at offset 2000
doesn't have the same value as before and must be rewritten.
Anyway I "think" that Vim reads the whole file into memory (possibly virtual
memory, but Vim doesn't know about that, it's the OS's business) when opening
it for editing, and writes the whole file when you implicitly or explicitly
ask for a write. IIUC Vim has the boolean buffer-local 'modified' option, but
if it is on it doesn't know "which parts" of the file are modified.
If you want to be sure, check the source.
Best regards,
Tony.