Terminal programs often open a text editor for input. $VISUAL and $EDITOR select the default command-line editor for supporting programs, while Debian's generic editor alternative is a separate setting.

Command-line programs usually read the $VISUAL and $EDITOR environment variables when they need an editor. Shells such as Bash and Zsh pass exported variables to child processes started from the terminal, so the setting must be exported before applications can inherit it. Git adds its own precedence through $GIT_EDITOR and core.editor before it falls back to $VISUAL and $EDITOR.

Persistent editor settings belong in the startup file that the target shell actually reads. Interactive Bash sessions commonly use ~/.bashrc, interactive Zsh sessions use ~/.zshrc, and login-shell requirements may belong in ~/.profile, ~/.bash_profile, or ~/.zprofile. On Debian and Ubuntu, /usr/bin/editor is a separate alternatives link, so change it only when a program calls that generic command instead of using the environment variables.

Steps to change the default command-line editor in Linux:

  1. Open ~/.bashrc in a text editor.
    $ nano ~/.bashrc

    For Zsh, open ~/.zshrc instead. If you are not sure which shell you use, echo $0 prints its name. Shell-specific details are in setting the editor in Bash and setting the editor in Zsh.

  2. Add the following lines at the end of the file.
    export VISUAL=nano
    export EDITOR=nano

    Another installed editor such as vim can replace nano. If the file already sets $VISUAL or $EDITOR, change those lines instead of adding new ones.

  3. Save the file.

    In Nano, Ctrl+X, Y, Enter saves the file and exits.

  4. Print both variables in a new terminal window.
    $ printenv VISUAL EDITOR
    nano
    nano

    A terminal that was already open keeps its old values until you close it.

  5. Open your crontab to confirm that your editor starts.

    Saving changes here can alter your scheduled jobs, so leave the contents as they are.

    $ crontab -e

    Nano should open. If another editor opens, run printenv VISUAL EDITOR in the same terminal to see what it inherited. Systems without the crontab command can skip this check.

  6. Close the editor without saving.
    No modification made

    In Nano, Ctrl+X closes an unchanged file straight away. Your crontab stays as it was, though the message can be worded differently on systems such as Fedora.

    Only programs that run the generic editor command on Debian or Ubuntu need one more change. sudo update-alternatives --config editor lists the installed editors and sets that command for every user.

Share this guide