The default text editor in Linux is essential when using system tools like visudo and crontab. Depending on the distribution, the default might be set to nano, vi, or another editor. If you prefer a different editor, it can be changed by configuring environment variables or system commands.
Setting the right editor improves your workflow when editing configuration files or writing scripts. Popular editors like Vim or Emacs have features that may better suit your needs. You can configure both user-specific and system-wide default editors.
Linux offers flexibility in how the default editor is set. The most common method is by adjusting the $EDITOR variable, or using the update-alternatives command in Debian-based systems.
Steps to set or change the default editor in Linux command line:
- Install the preferred text editor.
$ sudo apt install vim
Ensure the editor is installed before proceeding with configuration.
- Check the current default editor by inspecting the $EDITOR variable.
$ echo $EDITOR /usr/bin/nano
If no default editor is set, the output will be empty
- Manually set the $EDITOR variable for the current session.
$ export EDITOR=/usr/bin/vim
This will temporarily set Vim as the default editor for the current session. You can replace vim with your preferred editor.
- Verify that the editor is set by checking the $EDITOR variable.
$ echo $EDITOR /usr/bin/vim
- Test the new default editor by opening a system tool like crontab or visudo.
$ sudo crontab -e
If the editor is set correctly, your preferred editor should open.
- Permanently set the default editor by adding the $EDITOR variable to your shell configuration file.
$ echo "export EDITOR=/usr/bin/vim" >> ~/.bashrc
This makes the change permanent for future sessions. If you are using a shell other than Bash, modify the corresponding configuration file (e.g., .zshrc for Zsh).
- Reload the shell configuration file to apply the permanent change.
$ source ~/.bashrc
This loads the new configuration into the current session.
- Verify that the editor is now permanently set by checking the $EDITOR variable again.
$ echo $EDITOR /usr/bin/vim
- (Optional) On Debian-based systems, use update-alternatives to change the system-wide default editor.
$ sudo update-alternatives --config editor
Follow the interactive prompts to select from available editors.
- (Optional) Test the new default editor by opening a system tool like crontab or visudo.
$ sudo crontab -e
The editor that opens should be the one you configured as the default.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.