Setting the editor when sudo-ing crontab edits

I recently needed to make edits to root’s crontab. To do this, normally I would execute sudo crontab -e. On this machine, however, the root user had emacs set as its default editor, so the crontab command inherited that default in the sudo session. I’m totally unfamiliar with emacs, even to the point of having to do a web search to figure out the key combination to exit (it’s C-x C-c).

To set a different editor for your crontab session, run:

sudo VISUAL=/usr/bin/vi crontab -e

Of course, you could replace /usr/bin/vi with something else if you wanted.