Why I've got no crontab entry on OS X when using vim?

前端 未结 12 1779
执念已碎
执念已碎 2020-12-07 07:41

I would like to use cron on my Mac. I choose it over launchd, because I want to be able to use my new knowledge on Linux as well. However, I cannot seem to get

12条回答
  •  感动是毒
    2020-12-07 08:16

    NOTE: the answer that says to use the ZZ command doesn't work for me on my Mavericks system, but this is probably due to something in my vim configuration because if I start with a pristine .vimrc, the accepted answer works. My answer might work for you if the other solution doesn't.

    On MacOS X, according to the crontab manpage, the crontab temporary file that gets created with crontab -e needs to be edited in-place. Vim doesn't edit in-place by default (but it might do some special case to support crontab -e), so if your $EDITOR environment variable is set to vi (the default) or vim, editing the crontab will always fail.

    To get Vim to edit the file in-place, you need to do:

    :setlocal nowritebackup
    

    That should enable you to update the crontab when you do crontab -e with the :wq or ZZ commands.

    You can add an autocommand in your .vimrc to make this automatically work when editing crontabs:

    autocmd FileType crontab setlocal nowritebackup
    

    Another way is to add the setlocal nowritebackup to ~/.vim/after/ftplugin/crontab.vim, which will be loaded by Vim automatically when you're editing a crontab file if you have the Filetype plugin enabled. You can also check for the OS if you're using your vim files across multiple platforms:

    ""In ~/.vim/after/ftplugin/crontab.vim
    if has("mac")
      setlocal nowritebackup
    endif
    

提交回复
热议问题