I am using the following Vim autocommand in my .gvimrc file:

augroup MyAuGroup
  autocmd MyAuGroup FileChangedShell * call FileChanedEvent_BuffUpdate()
augroup END

function FileChanedEvent_BuffUpdate()
  let MyBn  = bufname("%")
  let MyStr = "Warning: File \"".MyBn."\" has changed since editing started\nSee \":help W11\" for more info."
  let MyTest = confirm(MyStr, "&OK\n&Load File", 2, "W")
  if MyTest == 2

with the intention to replace the default gVim behaviour when a file is changed externally (see this question). However, if multiple windows are opened, showing multiple buffers, the edit command works on the last active window, and not on the window containing the buffer that was changed.

How can I determine which buffer caused the FileChangedShell event, and apply the edit commant to that buffer?

| |

From :help FileChangedShell:

NOTE: When this autocommand is executed, the
current buffer "%" may be different from the
buffer that was changed "<afile>".

You need to locate the window where the corresponding file is being edited. For that, the buffer number (in <abuf>) is even easier:

let winNr = bufwinnr(0 + expand('<abuf>'))
execute winNr . 'wincmd w'

The same applies to the buffer name; replace

let MyBn  = bufname("%")


let MyBn  = expand('<afile>')
| |
  • I used edit MyBn instead of the edit command. But, when selecting the load File button, I get error E811 and the buffer won't update. I think my alternative answer is more direct. – ysap Mar 21 '14 at 19:52
  • I wonder why there is a difference between the two edit forms. The edit (no args) version works when the last active buffer was the one with the changed file. – ysap Mar 21 '14 at 19:56
  • tried also edit <afile> but got the same response – ysap Mar 21 '14 at 20:03
  • You would need execute 'edit' fnameescape(MyBn), but I see you've found a more straightforward alternative. – Ingo Karkat Mar 21 '14 at 20:15
  • Unfortunately, this variation fails either. – ysap Mar 21 '14 at 20:31

Thanks to @IngoKarkat's answer. I found an alternative solution. Replace the if block in the function with:

  if MyTest == 2
    let v:fcs_choice = "reload"
    let v:fcs_choice = ""

This seems to do the trick.

| |

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.