60

I normally use gVim for editing, but I sometimes use vim when remotely connecting to my machine.

When I have a file opened in gVim and it has changed from outside (e.g. new updates from repository), gVim offers to reload it.

However when such a thing happens with Vim, it does nothing until you try to save the file. It just warns you that file has changed, but does not offer to reload it.

It there a setting to make Vim's behavior match gVim?

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Art
  • 23,747
  • 29
  • 89
  • 101
  • possible duplicate http://stackoverflow.com/questions/2157914/can-vim-monitor-realtime-changes-to-a-file – ivotron Mar 12 '14 at 20:25

6 Answers6

61

If you want to open the updated file use

:e

It should reload the open file.

avadh
  • 659
  • 1
  • 5
  • 2
  • 4
    Yes. But the question if how to detect the file change automatically, not how to reload it. – KFL Mar 22 '19 at 17:55
56

As ashcatch said, the :checktime command will check for changes on disk and prompt you to reload. So set Vim to run checktime automatically after some event. Which event to use is up to you. One possibility is to use CursorHold, which fires after you move the cursor and then let it sit still for updatetime milliseconds. (Default 4 seconds.)

:au CursorHold * checktime

You could also set it on WinEnter or BufWinEnter so it changes every time you switch between buffers/windows. If you're really paranoid you could set it on CursorMoved so it checks the file on disk every time you move the cursor, but that's probably overkill and may lag a bit.

See :h checktime, :h updatetime, :h autocmd-events-abc.

Community
  • 1
  • 1
Brian Carper
  • 71,150
  • 28
  • 166
  • 168
  • 9
    I am so bad reading vim help... I am not sure why it just numbs my mind for some reason. – ojblass May 31 '09 at 08:25
  • Brian, vim says "No such group or event: CurshorHold * checktime " – Art Jun 02 '09 at 01:18
  • Sorry, typo. CurshorHold should have been Cursorhold. – Brian Carper Jun 02 '09 at 19:54
  • 1
    Thanks ^^ I've been wonderning how to do that for quite some time now. This works nice with `set autoread` to, which will re-read the file if its contents did not change. – Romuald Brunet May 07 '10 at 13:05
  • Didn't work for me. I tried CurshorHold and Cursorhold. I am using Windows 7 64 bits. – Akira Yamamoto Apr 30 '13 at 12:49
  • This works badly if you try to enter the "Command Line" (q:). See my answer for a workaround. – DBedrenko Sep 25 '14 at 09:57
  • Use `:au CursorHold,CursorHoldI * checktime %` to only check (and reload if `autoread` is set) the current file. Note that this doesn't work if `buftype` is set, i.e. it is not a normal file (e.g. `buftype=nowrite` or is the `quickfix` or `help` buffers.) – Sameer Jan 15 '15 at 19:57
  • You need to use `getcmdwintype()` (as in @DBedrenko's answer below) otherwise you get errors in the command window. – gib Oct 04 '18 at 15:43
25

This is done using an auto command called FileChangedShell. I'm too new to post links, but your best bet would be to read the autocmd part of the vim documentation (Google for that)

but the gist is to set something like the following line in your vimrc

:au FileChangedShell * echo "Warning: File changed on disk"
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
Matt Simmons
  • 678
  • 1
  • 8
  • 13
  • I don't think that's necessary, vim should warn you about the change automatically. – too much php May 29 '09 at 05:46
  • 14
    Though it does not, unfortunately. – Art May 29 '09 at 09:40
  • 3
    Sorry Matt, it actually did, was looking at wrong file. It does display warning, but not actual prompt to reload though – Art Jun 02 '09 at 01:17
  • Ah, ok. No worries. Hope you can find what you're looking for! – Matt Simmons Jun 02 '09 at 01:35
  • I entered that into my Konsolse vim session and it did not detect that I changed the file with kate behind vim's back. If I refocus the terminal, it does nothing. I guess the event is not fired. Any ideas? – Martin Ueding Dec 08 '11 at 13:18
  • Event "FileChangedShell" is a consequence of file change detection, not a reason. So it absolutely has no sense to have `:au FileChangedShell * somecommand` in config if you want vim to detect a file has been changed from outside. You need to trigger that event, not capture it. Matt, so your answer is misleading. I vote for Brian Carper's answer: http://stackoverflow.com/questions/923737/detect-file-change-offer-to-reload-file/927634#927634 – user2683246 Jul 07 '14 at 14:57
  • Did not work for me, I am using `au CursorHold * checktime` as recommended below – The Vivandiere Sep 22 '16 at 19:51
10

To improve on Carper's answer:

" Run checktime in buffers, but avoiding the "Command Line" (q:) window
au CursorHold * if getcmdwintype() == '' | checktime | endif

Without this check Vim will spout errors if you try to use the "Command Line" (q:) buffer, because this buffer doesn't support the :checktime command. Found this out thanks to kdlv on #vim.

DBedrenko
  • 4,871
  • 4
  • 38
  • 73
  • 1
    Looks like `getcmdwintype()` may require vim 7.4? I seem to have it working on 7.3 using `getcmdtype()` instead. – overthink Oct 24 '14 at 14:17
  • 1
    You can use `au CursorHold,CursorHoldI * silent! checktime` to ignore errors. This will work in earlier Vim versions too. – Sameer Jan 15 '15 at 19:45
  • 1
    It is also nice to use `FocusGained,CursorHold` instead of just `CursorHold` to ensure that the buffer is updated immediately after re-focussing it. – Torben Jan 22 '18 at 14:47
9

You can manually trigger the checking with :checktime. gvim does this everytime it regains focus, so it is not necessary that often to do the manual checking.

I don't know if there is a different approach (like automatically checking in a certain time interval).

ashcatch
  • 2,327
  • 1
  • 18
  • 29
  • 2
    Why can't vim also trigger `:checktime` when it gains focus? Is this a curses program limitation? – KFL Mar 22 '19 at 18:04
3

from this answer (refering to an answer by PhanHaiQuang and a comment by flukus)

One can run this oneliner from ex (whithin vim) when needed (or put each command in vimrc, for when log-files are opened.)

:set autoread | au CursorHold * checktime | call feedkeys("lh")

Explanation:
- autoread: reads the file when changed from the outside (but it doesnt work on its own, there is no internal timer or something like that. It will only read the file when vim does an action, like a command in ex :!
- CursorHold * checktime: when the cursor isn't moved by the user for the time specified in 'updatetime' (which is 4000 miliseconds by default) checktime is executed, which checks for changes from outside the file
- call feedkeys("lh"): the cursor is moved once, right and back left. and then nothing happens (... which means, that CursorHold is triggered, which means we have a loop)

MacMartin
  • 2,366
  • 1
  • 24
  • 27