The Bash shell relies on environment variables to determine which text editor to use for interactive tasks. Setting a default editor streamlines workflows such as editing cron entries and handling git commit messages. This configuration provides a consistent user experience and prevents unintended editor changes.

The default editor is usually specified through the EDITOR or VISUAL environment variables. Many command-line utilities check these variables to decide which editor to launch. Overriding them ensures your preferred editor, such as nano or vim, opens whenever text editing is required.

On Debian-based systems, you can also manage the default editor system-wide with update-alternatives. This method is useful for multi-user environments and requires administrative privileges. Regardless of the chosen approach, setting a stable default editor reduces confusion and streamlines command-line operations.

Steps to set default text editor using environment variables

Bash allows you to define the EDITOR or VISUAL variable in your shell configuration file. These variables are read by utilities that prompt for textual input. This method affects only the user account where it is configured.

  1. Open or create your ~/.bashrc file with any text editor.
  2. Add a line to export the editor, for example export EDITOR=“/usr/bin/nano”, to define your default editor.
    $ nano ~/.bashrc
    ...
    export EDITOR="/usr/bin/nano"
  3. Save your changes and exit the editor.
  4. Reload your shell configuration to apply the new environment variable.
    $ source ~/.bashrc
  5. Verify that the variable is set correctly.
    $ echo $EDITOR
    /usr/bin/nano

    If you prefer vim, replace /usr/bin/nano with /usr/bin/vim or any other text editor path.

Steps to set default text editor using update-alternatives on Debian-based systems

On Debian-based distributions, you can manage the default editor for all users using the update-alternatives command. This sets the preferred editor system-wide, affecting utilities that invoke the system’s default editor. Administrative permissions are required to modify these settings.

  1. Run update-alternatives in configure mode to list available text editors.
    $ sudo update-alternatives --config editor
    There are 2 choices for the alternative editor (providing /usr/bin/editor).
    
    Selection    Path            Priority   Status
    ------------------------------------------------------------
    * 1          /usr/bin/vim    100       auto mode
      2          /usr/bin/nano   40        manual mode
    Press <enter> to keep the current choice[*], or type selection number:
  2. Select the appropriate number to set your preferred text editor.
  3. Confirm the new default editor by repeating the previous command.
    $ sudo update-alternatives --config editor

    This method updates the default editor for all users on the system.

Discuss the article:

Comment anonymously. Login not required.