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/vimReplace /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.
Open the
.bashrcfile in your home directory:nano ~/.bashrcScroll to the bottom of the file and add the following line:
export EDITOR=/usr/bin/vimSave the file and exit the editor.
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 visudoIf 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.