8

Is there any way to autosave the buffer before issuing :make? I use MacVim and make is bound to Command-B, which is very helpful but I cannot seem to figure out how to write the buffer before a make. I looked at all the autocmd events and nothing seemed to fit.

There's a QuickFixCmdPre which should be called before a make but can't seem to get it to work:

~/.vimrc

function! AutoSaveOnMake ()
    if &modified
        write
    endif
endfunction

autocmd QuickFixCmdPre *.c :call AutoSaveOnMake()  
Chris
  • 435
  • 2
  • 8

2 Answers2

19

Vim has a built-in setting for that:

:set autowrite

Write the contents of the file, if it has been modified, on each :next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :!, :make, CTRL-] and CTRL-^ command; and when a :buffer, CTRL-O, CTRL-I, '{A-Z0-9}, or `{A-Z0-9} command takes one to another file.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
3

Instead of creating an autocmd command, why not just update the Command-B mapping?

nnoremap <d-b> :update<bar>make<cr>
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
  • Can't seem to remap Command-B. If I try: nnoremap :updatemake for example, it works, but not . – Chris Aug 22 '12 at 17:17
  • Make sure you put this in you `.gvimrc` file. See http://superuser.com/questions/249448/macvim-re-map-command-key-combinations-like-d-f – Peter Rincker Aug 22 '12 at 17:21
  • Got it working by combining the above remap command with the superuser comment. As you said it has to go in `.gvimrc` – Chris Aug 22 '12 at 19:47