Ying Xiong
Ying Xiong

Reputation: 4928

vim: detect underlying file change when entering insert mode

Here's a sequence of events:

  1. Open a file in vim, make some edits, save it, and stay in normal mode;
  2. The file gets changed by an external process, say git;
  3. Go back to vim, enter insert mode (at this point file has been changed, but vim sees a stale version);
  4. Make some edits to the stale version, go back to normal mode;
  5. Save the file in vim.

When I do things like above, what typically happens is that vim warns me at step 5:

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

This is rather inconvenient, because I already made some "valuable" edits. How can I ask vim to do the check at step 3, that is, before entering insert mode, check if the buffer is outdated and offer to reload?

Upvotes: 2

Views: 222

Answers (1)

phd
phd

Reputation: 94483

Add to your ~/.vimrc:

autocmd InsertEnter * checktime

I.e., on entering Insert mode for any file (*) execute checktime.

Upvotes: 4

Related Questions