Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
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 '12 at 12:20
1  
@mat: Your comment could probably be a regular answer. –  Heptite Dec 23 '12 at 13:51
    
@mat: thanks for that answer. –  Coolmax Dec 23 '12 at 14:30
    
Did you run 'update-alternatives --config editor' while you are root? –  tumiki Dec 25 '12 at 5:58
    
@tumiki: I just checked, it's work, but I'm searching solution for other (non-debian) distros too. –  Coolmax Dec 26 '12 at 11:59

1 Answer 1

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).

There are other options, but they're either needlessly complicated or overly permissive and dangerous, so you'll have to read man sudoers yourself for those...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.