How to Change the Default Text Editor in Bash, Zsh, and Ksh

When working in a Unix-like terminal, many commands and programs rely on a default text editor—used for editing configuration files, writing commit messages, or reviewing content. On systems like Debian, Ubuntu, or Linux Mint, this default is typically Nano, a lightweight and simple-to-use editor. However, many users prefer to switch to a more powerful tool like ViM for its advanced features and speed. If you’re using Bash, Zsh, or Ksh, changing the default editor is straightforward and improves your workflow. If you’re using other shells, like csh or tcsh, you might want to follow this related guide for detailed instructions.

This tutorial walks you through how to set ViM as the default editor for Bash, Zsh, and Ksh shells, both temporarily and persistently. We’ll explore setting the right environment variables, saving those settings across sessions, and ensuring compatibility with various tools. This adjustment can enhance your shell usage, especially when combined with advanced Bash tweaks such as smarter tab completion and recalling the last argument from the previous command.

Setting the Default Editor Temporarily

To begin, you can temporarily change the default text editor by setting two environment variables: EDITOR and VISUAL. These are recognized by most programs that invoke a text editor. Setting both ensures broader compatibility, as some applications prioritize one over the other. The change only lasts for the current session—it disappears after you log out or close the terminal.

Start by checking the path to your desired editor using which vim, which typically returns /usr/bin/vim. Then, set the variables as follows:

export EDITOR=/usr/bin/vim
export VISUAL=/usr/bin/vim

This change affects programs immediately in the current terminal. However, since it’s only temporary, this method is best for quick testing or short-term changes.

Making the Change Persistent for All Users

For a more permanent solution, you need to add the export commands to a configuration file that gets executed on shell startup. If you want the change to apply system-wide—for every user on the machine—you should modify the global profile file.

Append the following lines to /etc/profile as root:

echo "export EDITOR=/usr/bin/vim" >> /etc/profile
echo "export VISUAL=/usr/bin/vim" >> /etc/profile

This ensures that every new shell session for all users will use ViM as the default editor. It’s a good approach for shared servers or organizational setups where a consistent environment is needed.

Keep in mind that changes to /etc/profile require administrative privileges. You may need to use sudo depending on your system setup.

Setting the Editor for a Single User

If you’d rather customize the default editor on a per-user basis, you should edit files specific to the user’s shell. Each shell reads its own configuration file at startup:

  • Bash reads from ~/.bashrc
  • Zsh reads from ~/.zshrc
  • Ksh reads from ~/.kshrc

Add these two lines to the appropriate file:

export EDITOR=/usr/bin/vim
export VISUAL=/usr/bin/vim

For example, if you’re using Bash, run:

echo "export EDITOR=/usr/bin/vim" >> ~/.bashrc
echo "export VISUAL=/usr/bin/vim" >> ~/.bashrc

After editing the file, either restart the terminal or source the configuration manually (e.g., source ~/.bashrc) to apply the changes immediately. This approach is ideal for developers or power users who want full control over their environment without affecting others.

Conclusion

Changing your default text editor in Bash, Zsh, or Ksh is a small tweak that can lead to significant productivity gains. Whether you’re switching from Nano to ViM or another preferred tool, adjusting the EDITOR and VISUAL environment variables ensures your shell and its programs behave exactly how you want them to. Making the change either temporarily or permanently depends on your usage needs—whether for quick tests or long-term preference.

Taking a few minutes to configure your environment properly can greatly improve your command-line experience. You’ll not only save time but also enjoy a smoother workflow, especially when combined with other shell optimizations like enhanced tab completion or smart command recall. For users of other shells like csh or tcsh, don’t forget to check out this separate guide to maintain consistency across environments.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *