This idea was actually exposed to me while reading a Pivotal Labs post on pair programming over tmux. The idea of autosaving in MacVim hadn’t occurred to me previously, though in retrospect it makes perfect sense. I had a veritable nervous twitch before I would switch focus to another application (iTerm, chrome, etc.) from MVIM. “:wq!” ,possibly a few ctrl-]’s and a few more “:wq!”‘s. It seems like a small thing, but given that this is a keystroke I hit probably hundreds of times in a day small things add up.
I searched chrome for “MVIM auto saving”, and came across this Stackoverflow post which gave me just the snippet I was looking for:
autocmd BufLeave,FocusLost * silent! wall
I dropped this into my gvimrc, restarted my MacVim, and quickly added a few lines to a file. I then (without saving!) went over to my terminal and cat’ed the file, and sure enough there were my changes! I’m greatly looking forward to the extra keystrokes this is going to save me, hopefully in turn reducing another little piece of the friction slowing down my ability to create great code. An important caveat is that this is dependent on the FocusLost command, which to quote the help “Only for the GUI version and a few console versions where this can be detected.” This makes me a little worried about the need to teach my fingers to auto-save again under certain contexts (like when working in tmux), but hopefully that doesn’t prove to be too challenging.