1

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 root@apple.com

What could be the issue? Any help?

mtk
  • 243
  • 3
  • 12

1 Answers1

3

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

augroup autosave_buffer
  au!
  au BufLeave *.in :w
augroup END

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

Ralf
  • 9,197
  • 1
  • 11
  • 30
  • 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 '20 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 '20 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 '20 at 19:22