20

When working on a feature branch in git, I frequently need to stash my changes to commit a bug fix to the main branch. When I'm done working on the changes, I git stash pop, which updates the timestamps on the files.

Even though the files are identical, the next time I try to save, I get:

WARNING: The file has been changed since reading it!!!

Do you really want to write to it (y/n)?

I don't want to automatically reload the file every time it changes on disk, only when I git stash pop.

Right now, I manually reload each buffer individually (:e). Is there any way I can do this in one command?

26

See :help bufdo for what you want to do. It will execute a command in each buffer in the buffer list. For example:

:bufdo e

You may also want to look at :help noconfirm to disable the confirmation dialog before issueing the bufdo command

:set noconfirm

and reenabling it after the bufdo command.

:set confirm
  • 1
    To force, try: :bufdo! e. – kenorb Apr 18 '15 at 21:22
  • and without printing messages on screen :silent! bufdo e – Werner Aug 12 '15 at 20:26
11

You can do this with the :checktime command. From the docs:

:checkt[ime]        Check if any buffers were changed outside of Vim.
                    This checks and warns you if you would end up with two
                    versions of a file.

The command will ask you what to do for each buffer the file of which has a changed timestamp. To disable this for files that haven't changed you can do :set autoread to force vim to just reload them. vim will ask you if the contents between the buffer and the file on disk have changed.

You can setup a map like the following for ease of use:

nnoremap <F5> :checktime<CR>
  • Here is some bonus information that is not really part of the answer: For me, on OpenSUSE, gvim (the GTK GUI for vim) does this automatically when it gains focus. I don't really know why it does it and I haven't checked on other systems but I thought it might be useful as an extra. Maybe someelse can confirm? – tokoyami Feb 5 '15 at 22:05
  • This is really helpful! What I did on my end was to integrate this command as part of my tab switching shortcuts. i.e. nnoremap tl :tabnext<CR>:checktime<CR> – cr8ivecodesmith Mar 6 at 11:39
  • @tokoyami, the feature you have in gvim may be related to FocusGained event in vim. You can find some related info here - unix.stackexchange.com/a/383044/99801. – Victor Yarema 19 hours ago

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.