A Bash terminal passes exported editor variables to programs launched from that shell, so commands such as crontab -e, sudoedit, and git commit open the editor selected for that account. Setting the editor in ~/.bashrc makes interactive Bash sessions use the same choice instead of falling back to whichever program a tool selects by default.
$VISUAL is the first editor preference many terminal programs check, while $EDITOR is the fallback for programs that only honor the older variable. Export both variables to the same editor command unless a visual editor and a line editor need separate choices.
The change belongs in the Bash startup file that actually loads for the terminal session. On typical Linux desktop terminals and remote shell sessions, interactive non-login Bash shells read ~/.bashrc; Debian and Ubuntu's /usr/bin/editor alternative is separate, so change it only when a program calls the generic editor command instead of reading $VISUAL or $EDITOR.
Related: How to configure Bash login scripts
Related: Change the default editor in Linux
Related: Set the default text editor in Zsh
Steps to set the default text editor in Bash:
- Check the current editor variables in the active shell.
$ printf 'VISUAL=%s\nEDITOR=%s\n' "${VISUAL-}" "${EDITOR-}" VISUAL= EDITOR=
An empty value means no editor preference is exported from the current shell.
- Confirm the editor command you want Bash sessions to export.
$ command -v nano /usr/bin/nano
Replace nano with vim, emacs, micro, or another editor command that exists on the system.
- Open ~/.bashrc for the current user.
$ nano ~/.bashrc
Use the Bash startup file that your terminal reads. Interactive non-login Bash shells read ~/.bashrc when the file exists.
- Add or update both exports near the other interactive shell settings.
- ~/.bashrc
export VISUAL=/usr/bin/nano export EDITOR=/usr/bin/nano
If the file already assigns VISUAL or EDITOR, replace the old lines instead of leaving conflicting values later in the same file.
Quote the value when the editor command includes arguments, for example export VISUAL="code --wait".
- Reload ~/.bashrc in the current terminal.
$ source ~/.bashrc
- Verify that the active shell now uses the editor choice.
$ printf 'VISUAL=%s\nEDITOR=%s\n' "$VISUAL" "$EDITOR" VISUAL=/usr/bin/nano EDITOR=/usr/bin/nano
- Open a new Bash terminal and verify the same values again.
$ printf 'VISUAL=%s\nEDITOR=%s\n' "$VISUAL" "$EDITOR" VISUAL=/usr/bin/nano EDITOR=/usr/bin/nano
This confirms that ~/.bashrc loads the editor choice automatically in a new interactive Bash session.
- Run a command that opens an editor from the shell environment.
$ crontab -e
crontab -e, sudoedit, and many other command-line tools read $VISUAL or $EDITOR when choosing which editor to launch.
On Debian and Ubuntu, sudo update-alternatives --config editor changes the generic /usr/bin/editor link. That setting is separate from the Bash variables above.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.