Commands.page Logo

How to Change Default Editor for Visudo in Ubuntu

When managing sudo privileges on Ubuntu, the visudo command ensures syntax safety before saving changes. By default, this tool often opens in the nano or vi editor, which may not suit every administrator’s preference. This guide explains how to configure your system to use a different text editor, such as vim or emacs, whenever you execute the visudo command.

Set the Editor Environment Variable

The visudo command checks the EDITOR environment variable to determine which text editor to launch. You can set this variable temporarily for your current session or permanently for all future sessions.

To change the editor temporarily, open your terminal and run the following command:

export EDITOR=/usr/bin/vim

Replace /usr/bin/vim with the path to your preferred editor. Once executed, running sudo visudo will open the sudoers file in vim instead of the default editor.

Make the Change Permanent

To ensure this setting persists after you close the terminal, you need to add the export command to your shell configuration file. For most Ubuntu users using bash, this file is .bashrc.

  1. Open the .bashrc file in your home directory:

    nano ~/.bashrc
  2. Scroll to the bottom of the file and add the following line:

    export EDITOR=/usr/bin/vim
  3. Save the file and exit the editor.

  4. Apply the changes immediately by running:

    source ~/.bashrc

Verify the Configuration

Confirm that the change was successful by running the visudo command. The system should now launch your specified editor.

sudo visudo

If the correct editor opens, your configuration is complete. You can now edit sudo privileges using your preferred text editing environment without altering the system-wide default editor alternatives.