1

I have set autowrite so that :make automatically saved the file before running the build command. But now I'm increasingly using :buffer to switch between buffers, and have now noticed that it silently saves the file, unlike :edit, to which autowrite doesn't apply.

I'd like to have :make write the file out, while still having :buffer refuse to switch buffers if current file is unsaved (similarly to :edit).

Is there any way to achieve this?

1
  • 1
    A solution could be to disable autowrite and create your own :Make command so that it saves the file and execute :make. :h user-commands should be helpful here – statox May 28 '20 at 12:26
2

Disable autowrite and add this to your vimrc, it will save your current modified buffer when you execute make:

augroup ag_make | au!
  autocmd QuickFixCmdPre make update
augroup end

Change update to wa if you want to save all changed buffers.

                                                   *:mak* *:make*
:mak[e][!] [arguments]  1. All relevant |QuickFixCmdPre| autocommands are
                       executed.
                        ...
5
  • What is the significance of this wrapping with augroup? Will bare autocmd ... not work? – Ruslan May 28 '20 at 12:45
  • 1
    @Ruslan bare autocmd is dangerous, if you source it twice, it will install autocmd twice. Wrap it inside augroup and call au! will make sure only 1 autocmd is installed. – dedowsdi May 28 '20 at 12:48
  • @Ruslan It get worse if you change sub command, the original autocmd still exists when you source the new autocmd. – dedowsdi May 28 '20 at 12:51
  • Hmm, update seems to be quite tame to protect in this way... – Ruslan May 28 '20 at 12:52
  • @Ruslan It's a habit, a good one, whenever you use autocmd, you wrap it in augroup, UltiSnips has an au entry for this. – dedowsdi May 28 '20 at 12:59

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.