How can I make `sudo crontab -e` use my `sudoedit` environment?

I’ve started using sudoedit <file> instead of sudo vim <file>. One of the advantages is that it uses my local ~/.vimrc. However, when using sudo crontab -e, it uses /root/.vimrc instead. Is there a way to make sudo crontab -e use my local ~/.vimrc?

Here is a related question, about using sudoedit with vimdiff. However, substituting crontab -e for vimdiff doesn’t work.

Asked By: Sparhawk

||

Assuming that you want to be editing root‘s crontab, sudo must give you root authority. After it does so, crontab will invoke ${VISUAL:-${EDITOR:-vi}} (it’ll use $VISUAL unless it doesn’t exist; in that case it’ll use $EDITOR unless it doesn’t exist; in that case it’ll use vi).

You have a few possible solutions. They all subvert the security provided by sudo, but you must already be aware of those issues (and be willing to protect your .vimrc) or you wouldn’t be using sudoedit in the first place.

The best is probably to add an assignment to the HOME variable on the sudo command line so crontab thinks the HOME directory is different:

sudo HOME=$HOME crontab -e

(That command won’t work if there’s whitespace in your home directory path!)

Answered By: Azhrei
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.