2

My work flow involves editing many files in vim, and occasionally I will do a git pull --rebase, which may or may not actually change the files I am editing. However, after such a rebase, vim will always think that every file has been modified, and will prompt me that "file has changed from original, are you sure you want to edit it?" when I make a modification and try to save a file that's open. This becomes extremely annoying because I will actually be unsure whether the file has changed or not, and whether it's okay or not to save my change (let alone being prompted this for every single file that I try to save).

Is there a way to make vim smarter about these modified files? I'd prefer to do this WITHOUT some large vim/git package.

  • use git status prior to vim'ing the files ONLY those with red /green (or plus / minus if no color in play) have been missed with. – linuxdev2013 Jun 12 '15 at 16:27
  • save all the files before a git pull --rebase and you will know where you are – Vorsprung Jun 12 '15 at 20:49
  • No one seemed to understand. I have the same problem. git appears to touch every file when you pull/rebase, for no good reason. Whatever git is doing, it causes vim to think the file has changed, even though it hasn't. It asks if you really want to overwrite the changes, because the file has been updated (but it hasn't). – Jason McCarrell Dec 3 '19 at 19:37
  • I suspect this is a git problem with how it touches files. – Jason McCarrell Dec 3 '19 at 19:38
0

If you have not yet modified the file, but it is open, then you update it outside of Vim, then Vim should prompt you to reload the file, or even (if only the timestamp changed) update automatically for you. But Vim only checks for this when it runs an external command, or when it gains input focus after having lost input focus (which only happens reliably in gVim and in a few terminal versions). If you are using terminal Vim, then you will probably need to manually add calls to :checktime in an appropriate autocmd, for example, CursorHold, CursorMoved, BufEnter, or another event that fires frequently. See this example of autocmds to run :checktime and also some of the other answers on that question for details.

Once you have Vim reliably checking for modified files, you can also customize what Vim does when it detects a modified timestamp, using the FileChangedShell autocmd event. This autocmd lets you set the variable v:fcs_choice to "reload", "ask", or "" (empty string) to tell Vim what to do, and you could probably do something crazy like start a diff with the on-disk file if you wanted. Vim can detect and separately act on the following scenarios (stored in v:fcs_reason):

deleted     file no longer exists
conflict    file contents, mode or timestamp was
            changed and buffer is modified
changed     file contents has changed
mode        mode of file changed
time        only file timestamp changed

See http://vim.wikia.com/wiki/File_no_longer_available_-_mark_buffer_modified for a fully developed example of using the FileChangedShell event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.