A Zsh 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 ~/.zshrc keeps interactive Zsh sessions from falling back to a program chosen separately by each tool.
$VISUAL is the first editor preference many terminal programs check, while $EDITOR remains the fallback for tools that only honor the older variable. Export both variables to the same editor command unless a graphical editor and a terminal editor need separate choices.
Interactive Zsh reads the user's rc file after the global startup files; for a normal home-directory setup that file is ~/.zshrc. If $ZDOTDIR is set, put the same lines in $ZDOTDIR/.zshrc instead, and keep Debian or Ubuntu's /usr/bin/editor alternative separate from these shell variables.
Related: How to configure Zsh startup files
Related: Set default text editor in Bash
Related: Change the default editor in Linux
Steps to set the default text editor in Zsh:
- Check the editor variables in the active Zsh session.
$ 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 to export.
$ command -v nano /usr/bin/nano
Replace nano with vim, nvim, emacs, micro, or another editor command that exists on the system.
- Open the current user's Zsh rc file.
$ nano ~/.zshrc
Use $ZDOTDIR/.zshrc instead when the account sets ZDOTDIR to store Zsh startup files outside the home directory.
- Add or update both editor exports near the other interactive shell settings.
- ~/.zshrc
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".
- Check the edited rc file for syntax errors.
$ zsh -n ~/.zshrc
No output from zsh -n means Zsh parsed the file without finding a syntax error. Use $ZDOTDIR/.zshrc instead when that is the file you edited.
- Reload the same rc file in the current Zsh terminal.
$ source ~/.zshrc
Use source "$ZDOTDIR/.zshrc" when the startup file lives under a custom ZDOTDIR directory.
- Verify that the current shell exports both editor variables.
$ printf 'VISUAL=%s\nEDITOR=%s\n' "$VISUAL" "$EDITOR" VISUAL=/usr/bin/nano EDITOR=/usr/bin/nano
- Start a new interactive Zsh process and confirm the same values load automatically.
$ zsh -ic 'printf "VISUAL=%s\nEDITOR=%s\n" "$VISUAL" "$EDITOR"' VISUAL=/usr/bin/nano EDITOR=/usr/bin/nano
A matching output confirms that ~/.zshrc applies the editor choice to new interactive Zsh sessions.
- 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 Zsh 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.