Imagine I have a Vim buffer open and the file is changed externally (example: I switched git branches or the file was just edited normally).

Any time that happens, I want to have 2 commands run. The first command, Foo runs right before Vim updates the buffer with the new text and then the second command, Bar runs after the buffer is updated. How do I do that?

I tried to use autocmd like this:

autocmd! FileChangedShell * Foo
autocmd! FileChangedShellPost * Bar

The problem is that it doesn't work. When the buffer updates, only FileChangedShellPost gets called. I tested this using these commands.

autocmd! FileChangedShell * echoerr "About to read"
autocmd! FileChangedShellPost * echoerr "read!"

When the file is updated, only "read!" is printed.

Right now I'm using CursorHold,CursorHoldI to try to simular a "before buffer is read" event but it's hacky. How do I run a command before the Vim buffer gets changed from an external edit?


Your Answer

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

Browse other questions tagged or ask your own question.