crontab: temp file must be edited in place

If you’ve ever tried to edit your crontab on OS X, you’ve probably seen this error message:

crontab: temp file must be edited in place

I banged my head on this for a few minutes before running across this walkthrough on solving the problem.

The post recommends an… interesting workaround involving aliasing crontab to set an environment variable and adding an if $VIM_CRONTAB == "true" block into your .vimrc.

Since I only needed to crontab -e once, I opted to simply :set nobackup nowritebackup.

Curiosity kicked in, however. It turns out that Vim recognizes crontab tempfiles as the crontab filetype, so a solution presents itself:

autocmd filetype crontab setlocal nobackup nowritebackup

This means that for files of the crontab type only (just crontab), Vim won’t write backup files, and crontab -e will be happy.

Now, crontab -e should work like a charm with Vim, allowing you to schedule cronjobs to your heart’s content.