6

I'm using Bash on Debian Squeeze. I just read this topic: change default text editor for crontab to vim

I have exported both variables VISUAL and EDITOR to vim for normal user and for root. And this works – when I type crontab -e as normal user or root, crontab runs vim for editing.

But running sudo crontab -e opens nano.

Running sudo echo $EDITOR (or $VISUAL) gave vim int output.

4
  • 3
    You'll find an explanation of why sudo echo $EDITOR doesn't do what you think it does here (and things to try to make your crontab work)
    – Mat
    Dec 23, 2012 at 12:20
  • 1
    @mat: Your comment could probably be a regular answer.
    – Heptite
    Dec 23, 2012 at 13:51
  • Did you run 'update-alternatives --config editor' while you are root?
    – tumiki
    Dec 25, 2012 at 5:58
  • @tumiki: I just checked, it's work, but I'm searching solution for other (non-debian) distros too.
    – Coolmax
    Dec 26, 2012 at 11:59

2 Answers 2

9

By default and for security purposes, sudo does not preserve the user environment.

There are multiple ways to deal with this; for your specific case, I recommend adding VISUAL=/usr/bin/vim (and EDITOR=/usr/bin/vim for programs that use the wrong one) to /etc/environment.

Second choice, if you have a permissive sudo configuration (perhaps because it's just you, or all sudoers have real root access anyway) is to run visudo and add Defaults env_keep="VISUAL EDITOR" to allow all sudoers to specify their own (i.e. preserve that environment variable).

Your existing sudoers file may well already have (or include) one or more env_keep statement(s), e.g. this (from the pastebin example):

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

The first of those env_keep lines will overwrite whatever edits you make above it (assignment), the other lines extend the list (+=). To keep everything working the way you're used to and unless you have a reason to change the set, I recommend adding a line immediately after those:

Defaults    env_keep += "VISUAL EDITOR"

(There are other options to use your preferred editor, but they're either needlessly complicated or overly permissive and dangerous, so you'll have to read man sudoers, figure them out yourself, and understand what the associated problems might be.)

5
  • Love this... except that visudo will launch vi... the thing we're trying to avoid like the plague. sudo env EDITOR=nano visudo is the way to go. Or maybe cat, which is a bit more dangerous. Also, Defaults env_keep="VISUAL EDITOR" only works after env reset, so putting it at the top will not work... the bottom is safest.
    – Ray Foss
    May 13, 2015 at 12:49
  • I am baffled by why placing env_keep="VISUAL EDITOR" under env_reset does not work... Ive only tried on top, under env_reset and at the bottom. only at the bottom works.
    – Ray Foss
    May 19, 2015 at 15:47
  • Without any other information, I'll just guess that there already is a line setting env_keep in the file somewhere, which overrides whatever you put in above and which you override by moving your directive to the end. If you paste your sudoers file to pastebin.com or something and add a link here, I can extract the relevant info into the answer.
    – Gabe
    May 19, 2015 at 16:11
  • it is the default centos sudoers that gives me troubles. I should probably script that. pastebin.com/4s3CiVYJ
    – Ray Foss
    May 19, 2015 at 18:56
  • instead of editing your sudoers file with visudo the recommended approach is to create an include with sudo vim /etc/sudoers.d/02-editor and add the env_keep line there. This way you'll avoid conflicts on package upgrades and can easier maintain and sync your config files (this allows to keep an unmodified sudoers file which contains #includedir /etc/sudoers.d at the very end).
    – ccpizza
    Oct 28, 2019 at 22:14
2

Does this work for you?

sudo VISUAL=vim crontab -e

It works for me. (Debian 10 buster)

1
  • For me this only works with EDITOR, not VISUAL and only once. I prefer the answer from @Gabe as that only needs to be done once. Jul 26, 2022 at 7:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .