简体   繁体   English

vim:进入插入模式时检测底层文件更改

[英]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;vim中打开一个文件,进行一些编辑,保存,并保持正常模式;
  2. The file gets changed by an external process, say git ;该文件被外部进程更改,例如git
  3. Go back to vim , enter insert mode (at this point file has been changed, but vim sees a stale version); Go 回到vim ,进入插入模式(此时文件已更改,但vim看到的是旧版本);
  4. Make some edits to the stale version, go back to normal mode;对陈旧版本进行一些编辑,go 回到正常模式;
  5. Save the file in vim .将文件保存在vim中。

When I do things like above, what typically happens is that vim warns me at step 5:当我执行上述操作时,通常会发生vim在第 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?如何让vim做第3步的检查,即在进入insert模式前,检查buffer是否过期,并主动reload?

Add to your ~/.vimrc:添加到你的 ~/.vimrc:

autocmd InsertEnter * checktime

Ie, on entering Insert mode for any file ( * ) execute checktime .即,在任何文件 ( * ) 进入插入模式时执行checktime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM