I usually keep *.in files open in split and edit them. I would like vim to autosave the *.in files whenever I switch between splits.

Searching other answers I found BufLeave that suits my case. I have below in my .vimrc:

autocmd BufLeave *.in :w!<cr>   

But this gives below error when I switch out of the split that has a *.in file open:

Error detected while processing BufLeave Auto commands for "*.in":
E13: File exists (add ! to override)

I am able to normally execute :w and :w! in the vim window successfully, but this auto save short cut is not working.

My vim details

$ vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Dec  3 2019 04:38:39)
Included patches: 1-503, 505-680, 682-1283, 1365
Compiled by [email protected]

What could be the issue? Any help?

  • check your existing BufLeave autocommand. Perhaps an old autocommand without the !? Apr 24, 2020 at 20:37

1 Answer 1


Took me a while before I saw it. The problem is the<cr>. Try:

augroup autosave_buffer
  au BufLeave *.in :w
augroup END

If you check your current directory, you will find a file named '<cr>'.

  • Wow, great find! I completely missed that too. Wondering why this was causing it to execute the write on the wrong window for some reason...
    – filbranden
    Apr 25, 2020 at 15:56
  • 1
    @filbranden I guess (speculate) there are two things here: 1) Might be that there was another autocmd for BufLeave without the ! (augroup not mentioned). And 2) The autocmd wrote to the file <cr>. So the E13 was not about the*.in or the other file, but for the file <cr>. Finally I learned that :wralf results in an error, but :w(ralf would create the file (ralf. Any [^A-z] starts a file name. And btw: Great to see you & Ben on the mod candidate list. Just my first thought.
    – Ralf
    Apr 25, 2020 at 19:20
  • Ah yeah good call! Now I recall seeing a file named '<cr>' in a directory and wondering where it was coming from. It was definitely from trying to reproduce this one! So that's why the original mapping was failing.
    – filbranden
    Apr 25, 2020 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.