0

I am using this code in my vimrc
if a file is changed outside vim, it shows File changed on disk. Buffer reloaded.

set autoread
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif
autocmd FileChangedShellPost *
  \ echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None

but i am facing a problem.
When i open multiple files via :tabnew filename in vim and let's say among 4 files, one file has been changed
vim doesn't specify me which one (is changed)

Now can anyone give me a fix?
any string or something through which vim will say, A.cpp file changed on disk. Buffer reloaded.

0

The file that is associated with the currently executing autocommand can be retrieved with this function call:

expand('<afile>')

Somewhat contrived but easy to understand test...

augroup AfileTest
  autocmd!
  autocmd BufWritePost * echom "I saved file " . expand('<afile>')
augroup END

After loading this and running :w the message with the affected file will be displayed.

Here's one possibility for your use case:

autocmd FileChangedShellPost *
  \ echohl WarningMsg | echo "File " . expand('<afile>') . " changed on disk. Buffer reloaded." | echohl None

See :h <afile>

11
  • Awesome it works.. but how about if multiple files change? if i had three tabs and among those, two are changed, how can i make vim to show "file A.cpp B.cpp changed" ?
    – Mega Bang
    Jul 15 at 12:08
  • You can't. At least not without writing a bunch of extra code. Each file related autocommand event is associated with a single file. You'd have to store the filenames somewhere and then print them all at once after the fact. Non-trivial. If you really want to do it then go ahead and ask a new question since that's a totally separate thing.
    – B Layer
    Jul 15 at 12:13
  • One big issue is determining/specifying that multiple events should be folded into a single message or if they happened far apart enough that they should be printed individually. Maybe a timer that periodically checks the "cache" of filenames and prints whatever's there then clears it for the next "round".
    – B Layer
    Jul 15 at 12:19
  • no, just for the opened tabs inside vim.. i don't need the others
    – Mega Bang
    Jul 15 at 12:21
  • 1
    much appreciated
    – Mega Bang
    Jul 15 at 12:29

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.