On an Ubuntu 12.04 machine I've used the alternatives system to set the editor to vim.basic. I get the correct editor when root but not when I sudo to root and run crontab, and I'm trying to understand why.

A bit more detail. The alternative editor is currently set to vim.basic by manual mode (output trimmed):

% sudo update-alternatives --set editor /usr/bin/vim.basic
% update-alternatives --display editor
editor - manual mode
  link currently points to /usr/bin/vim.basic
/bin/nano - priority 40
  slave editor.1.gz: /usr/share/man/man1/nano.1.gz
/usr/bin/vim.basic - priority 30
  slave editor.1.gz: /usr/share/man/man1/vim.1.gz
  slave editor.fr.1.gz: /usr/share/man/fr/man1/vim.1.gz
  slave editor.it.1.gz: /usr/share/man/it/man1/vim.1.gz
  slave editor.pl.1.gz: /usr/share/man/pl/man1/vim.1.gz
  slave editor.ru.1.gz: /usr/share/man/ru/man1/vim.1.gz
Current 'best' version is '/bin/nano'.

If I sudo to root and edit crontab the editor is vim.basic:

% sudo -i
% crontab -e
# editor is vim; :help shows
# *help.txt*      For Vim version 7.3.  Last change: 2010 Jul 20

EDITOR and SHELL as root:

# env | grep -i editor
# echo $0

However as a normal user if I sudo crontab the editor is nano:

% sudo crontab -e
# GNU nano 2.2.6

EDITOR and SHELL as normal user:

% env | grep -i editor
% echo $0

Now I'm noticing that nano has a higher priority (40) than vim.basic (30), so I could change the priority of vim.basic. But that defeats the purpose of manual mode.


I've noticed that using sudo -E gives the correct editor, implying problem is environment related. That is:

sudo crontab -e     # nano editor is used
sudo -E crontab -e  # vim editor is used
  • Have you tried setting VISUAL? – MadHatter Sep 11 '14 at 7:33
  • @MadHatter could be right, try EXPORT EDITOR=vim & EXPORT VISUAL=vim – tachomi Sep 11 '14 at 13:36
  • Thanks @madhatter I'll give that a go. Although EDITOR is already set... – Sonia Hamilton Sep 11 '14 at 23:17

In most configurations, sudo strips most environment variables. You can see the sudo configuration by running sudo -V as root (so sudo sudo -V as a user with sudo permissions).

On Ubuntu, variables are stripped except from a small list, and EDITOR and VISUAL are not in the list to preserve. So when you run sudo somecommand, your per-user editor preferences are not applied when running somecommand.

When you run sudo -E, the file is copied to a temporary location, then sudo runs your editor with no additional privileges, and finally the temporary file is moved to the final location if the editor returns a success status. Since the editor is executed without additional privileges, sudo doesn't strip the environment.

You should use sudo -E whenever possible, since this lets you run your favorite editor without concerns about running programs with elevated privileges. When this isn't possible (for example, to run crontab -e, you can choose your editor by defining your preference again inside the command executed by sudo:

sudo env VISUAL=vim crontab -e
  • Thanks @Gilles. But even with EDITOR and VISUAL stripped, /usr/bin/editor is set to vim.basic by alternatives, so it should be used instead of nano. More investigation required on my part... – Sonia Hamilton Sep 17 '14 at 7:24
  • @SoniaHamilton Hence my last command, which sets the VISUAL variable again after it's stripped. – Gilles 'SO- stop being evil' Sep 17 '14 at 8:50
  • Thanks @Gilles. But what's the purpose of alternatives and /usr/bin/editor then? They seem redundant. Or maybe it's up to each program to look at /usr/bin/editor, and VISUAL/EDITOR are the older, more established mechanisms? – Sonia Hamilton Sep 17 '14 at 22:57
  • @SoniaHamilton Alternatives and /usr/bin/editor are for a system-wide default. The EDITOR and VISUAL environment variables are for per-user defaults. – Gilles 'SO- stop being evil' Sep 18 '14 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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