Root, crontab and Vim
#274 Henry, Monday, 14 September 2015 10:20 PM (Category: UNIX Tools)
(Tags: vim cron)

I have often come across a problem when trying to edit my cron jobs, as root, under Linux, with Vim.

Usually, after a fresh installation, I change my environment variables of EDITOR and VISUAL to point to Vim. When I do this, if I edit my crontab with

crontab -e

and Vim kicks in, then when I exit, all my changes are gone. No reason, just gone. When I ask about this, the usual response is "Why are you using crontab -e? That's dangerous, you should be doing it like this:

crontab -l > ct   # store current cron to a text file
vi ct             # edit that text file
crontab ct        # load the text file back into cron

and probably yes, technically, that is the safe way to do it. But that's not what I want to do. I want to edit it in situ and have my changes work. It will not work with Vim, but it will work with vi.

I've been setting up a new server and ran into this very same problem. I did some research finally, found out what was happening, and found solutions.

Old vi will edit a file and save that file back onto the same filename. Crontab -e relies on this behaviour.

New Vim, by default, wants to baby the user. It has implemented a wonderful backup scheme. The default behaviour now is save the new file to a new name, rename the old file with a tilde on the end, and then rename the new file to the new name. And while it's doing this, it creates what appears to be a changelist file with a dot in front and a new extension and a tilde on the end.

So if you edit the file testfile.txt and save your changes, you end up with these files:

-rw-r--r-- 1 tester users  946 Sep 14 22:19 .testfile.txt.un~
-rw-r--r-- 1 tester users   74 Sep 14 22:19 testfile.txt
-rw-r--r-- 1 tester users   37 Sep 14 22:19 testfile.txt~

Wow. What a pile of shit. This is the behaviour that wrecks it for crontab -e. There is a fix for it.

Add these lines to your .vimrc:

set nobackup
set nowritebackup

Yes, twice, you have to turn this behaviour off with effort. Once you do this, Vim will revert to the original vi behaviour and will save no backup files, and crontab -e will work as expected.

I actually go a bit further. Every time Vim gets updated in Slackware, it creates a new copy of a default configuration file in


This file gets loaded before your .vimrc and it has default configurations that will change your behaviour. I ran into this problem long time ago. Vim would do things that I was not expecting and did not want, and no matter what I did in my .vimrc, it would do something else. Eventually I located this file and found that it controlled initial behaviour. So now, every time Vim is updated, this file is renamed. My .vimrc is the only thing loaded and Vim behaves the way I want.

And finally, yes, I use Vim as root, and not vi. I change my system editor as root. I edit my cron jobs recklessly in situ. I do what I am warned not to do. It's what I want to do, and it's what I do. I don't want Unix tools to baby me, or get in my way, or do things I do not want. I want Unix tools to do what I want them to do, the way I want them to. That's one of the great things about Unix. But lately it seems that more tools are trying to protect people against themselves. At least with some effort you can get around that.