4

I'd like to automatically save buffer after moving into another split.

If you're interested why I'm interested in that read section below.

I've started using vim and then neovim not so long ago, so I'm still figuring stuff out. I've noticed a particular pattern in my workflow. I'm splitting terminal (for now I'm using tmux for splits, but I've used iterm2 and terminator for that previously) and one part of the screen is vim and other part is shell where I'm doing something, like running tests, REPL or debugging.

I don't want save files manually before running tests or debugging every time, so I've used

augroup focus
    au!
    au TabLeave * silent! wall
    au FocusLost * silent! wall
augroup END

set autowriteall

And a plugin so that focus related events would work with tmux. So If I use tmux everything works smoothly here for me, but I don't like that I need to memorize another set of keybindings that basically doing the same thing. Like splitting windows, creating windows etc. So I thought, why not just use neovim terminal emulator. It works well for me, but one thing is missing, when I'm invoking terminal in split my buffer isn't saved.

Maybe the right way to do it is just configure tmux somehow to replicate all vim keybindings (It has nice features like persistent sessions and stuff), but for now I'd like to use neovim emulator if possible.

  • Wouldn't the autocommand event BufLeave do the trick? It is triggered before leaving to another buffer, so I guess it would be triggered before switching to the terminal, right? I don't use neovim so I'm not really sure. – statox May 4 '16 at 9:07
5

What you are looking for is a way to trigger the autocommand each time you leave a buffer to another location.

Conveniently the autocommand event BufLeave was made precisely for that, so you just need to add a line like this in your autogroup:

au BufLeave * silent! wall

As a bonus: the command :h autocommand-events might be pretty useful to you when you are looking for a new autocommand event.

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.