LinuxDojo

How to Change the Default Editor to Nano

Many command-line programs, such as crontab -e and visudo, open in a default editor, which is often vi. This guide shows you how to change that default to nano, a more user-friendly text editor.

The Cause: Environment Variables

The default editor is determined by two environment variables: VISUAL and EDITOR. Programs check these variables to know which editor to open. By default, they may not be set or may be set to vi.

Method 1: The One-Time Solution

You can set the default editor for a single command by putting the environment variable in front of it. This is useful for when you only need to use nano once without changing your permanent settings.

Process: The command EDITOR=nano temporarily sets your editor to nano for the command that follows it. In this example, it tells crontab -e to open the crontab file using nano.

EDITOR=nano crontab -e

Method 2: The Permanent Solution

For a permanent change, you can add the environment variable to your shell's configuration file. This ensures that every new terminal session will automatically use nano as the default editor.

  1. Open your shell configuration file.

    For most users, this file is .bashrc in your home directory. Open it with nano:

    nano ~/.bashrc
  2. Add the environment variable.

    Add the following line to the end of the file. This line makes nano your permanent default editor.

    export EDITOR=nano
  3. Apply the changes.

    Save and exit the file. For the changes to take effect immediately in your current terminal, run the following command:

    source ~/.bashrc

After following these steps, any command that relies on the default editor, such as crontab -e or visudo, will now open in nano automatically.