Choosing a default text editor on Linux influences many everyday administration tasks where programs automatically open an editor for configuration or notes. Utilities such as crontab, visudo, and version control tools often rely on an implicit choice that may be unfamiliar or lack desired features. Selecting a preferred editor provides consistent behavior and reduces errors caused by switching interfaces.

Most command-line tools respect environment variables like $EDITOR and $VISUAL to decide which program to start. Shell initialization files such as /~/.bashrc and /~/.profile can export these variables so interactive sessions consistently launch the same editor. On Debian and Ubuntu systems, the update-alternatives mechanism also exposes a generic editor alternative that some utilities and scripts use via the /etc/alternatives symlink tree.

Careful configuration avoids surprises when editing privileged files or running scripts that invoke an editor non-interactively. A missing path, typo, or misconfigured shell startup file may prevent a login shell from starting cleanly or leave tools without an editor. The steps below focus on shell-based configuration for a typical bash environment, with optional system-wide settings for Debian-derived distributions.

Steps to set or change the default editor in Linux command line:

  1. Install the preferred text editor on a Debian or Ubuntu system if it is not already present.
    $ sudo apt update && sudo apt install --assume-yes vim
    Reading package lists... Done
    Building dependency tree... Done
    Reading state information... Done
    The following NEW packages will be installed:
      vim
    ##### snipped #####

    Replace vim with another editor such as nano or emacs if a different editor is preferred.

  2. Inspect the current values of the $EDITOR and $VISUAL environment variables.
    $ echo "$EDITOR"
     
    $ echo "$VISUAL"

    An empty line indicates that no default editor is currently defined for that variable.

  3. Set the preferred editor as the default for the current shell session.
    $ export EDITOR=/usr/bin/vim

    Use the full path reported by command -v vim to avoid ambiguity when multiple versions are installed.

  4. Optionally set the $VISUAL variable to the same editor for applications that prioritize it.
    $ export VISUAL=/usr/bin/vim

    Many interactive tools consult $VISUAL before $EDITOR when choosing an editor.

  5. Verify that the environment variables now point to the desired editor.
    $ echo "$EDITOR"
    /usr/bin/vim
    $ echo "$VISUAL"
    /usr/bin/vim
  6. Test the temporary configuration by invoking a program that launches an editor.
    $ crontab -e
    crontab: installing new crontab

    crontab uses $VISUAL or $EDITOR to determine which editor to start.

  7. Append the $EDITOR and $VISUAL definitions to the /~/.bashrc file so future interactive shells inherit them automatically.
    $ printf '\nexport EDITOR=/usr/bin/vim\nexport VISUAL=/usr/bin/vim\n' >> ~/.bashrc

    Incorrect syntax in /~/.bashrc can prevent interactive shells from loading configuration or running commands as expected.

    For Zsh, append the same lines to /~/.zshrc instead of /~/.bashrc.

  8. Reload the shell configuration file in the current terminal session.
    $ source ~/.bashrc
  9. In a new terminal session, confirm that the variables are set without manual exports.
    $ echo "$EDITOR"
    /usr/bin/vim
  10. On Debian and Ubuntu systems, adjust the system-wide editor alternative used by generic editor invocations.
    $ sudo update-alternatives --config editor
    There are 3 choices for the alternative editor (providing /usr/bin/editor).
     
      Selection    Path                Priority   Status
    ------------------------------------------------------------
    * 1            /bin/nano            40        auto mode
      2            /usr/bin/vim.basic   30        manual mode
      3            /usr/bin/vim.tiny    15        manual mode
     
    Press <enter> to keep the current choice[*], or type selection number:
    ##### snipped #####

    Selecting a new entry updates the /etc/alternatives symlink so programs using the generic editor command follow the new default.

  11. Confirm the selected alternative editor after any change.
    $ sudo update-alternatives --display editor
    editor - manual mode
      link best version is /usr/bin/vim.basic
      link currently points to /usr/bin/vim.basic
    ##### snipped #####
Discuss the article:

Comment anonymously. Login not required.