Environment: CentOS 8

Question: When I enter sudo crontab -e it opens in Vim. However Nano is set as the default editor and for every other type of file it is used as expected. Why might this be? Is there a way around this?

Background: I followed these steps to make Nano the default editor.

# nano /root/.bashrc

I added these lines and saved the file.

export EDITOR='nano'
export VISUAL='nano'

This is what the entire file consists of.

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc

export EDITOR='nano'
export VISUAL='nano'

I used this command to make the change active.

# source /root/.bashrc

/root/.bashrc is executed only if a shell is started under the root account. Executing a command with sudo usually does not invoke a shell so the file is not used.

sudo would keep the environment variable EDITOR if it is set for the calling user. So that is the solution to your problem:

EDITOR='nano' sudo crontab -e
  • 1
    @myNewAccount It makes no sense to use sudo in /root/.bashrc (unless changing to a non-root user). And why would you have crontab run on every shell startup? You can put export EDITOR='nano' in the ~/.bashrc of the user which calls sudo (then your old sudo command line should work as expected). – Hauke Laging Apr 29 '20 at 23:06
  • 1
    Many systems install sudo with Defaults env_reset which wipes all environment variables, including $EDITOR. That means merely setting EDITOR=nano would be insufficient. In such a case, add Defaults env_keep += "EDITOR" in /etc/sudoers or in a file in /etc/sudoers.d/ to ensure that you are keeping that variable. – Adam Katz May 5 '20 at 15:05

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.