Choose a Safe Default Editor with sensible-editor

Debian's sensible-editor works out which editor to open by checking a short chain of environment variables, and the order is easy to get wrong. You will open a file with the editor selected for the current context, override that choice for one command, and set a persistent user preference without touching system-wide alternatives.

The examples use sensible-utils version 0.0.22, installed on this machine. Allow about ten minutes. You need a shell and a text editor already installed. The checks below are ordinary user commands. Nothing in this guide needs sudo, and the examples do not edit an existing file.

1. Check the command and package

Confirm which executable is being used and record the installed package version:

$ command -v sensible-editor
/usr/bin/sensible-editor
$ dpkg-query -W -f='${Package} ${Version}\n' sensible-utils
sensible-utils 0.0.22

The command accepts optional arguments, which it passes to the editor as file names or other editor arguments. It does not provide a common editor interface beyond choosing and launching a command.

Checkpoint: if command -v prints nothing, stop here and install sensible-utils through your normal package-management process. Do not copy a replacement script into /usr/bin.

2. See the selection order

sensible-editor looks for the first usable candidate in this order:

  1. VISUAL
  2. EDITOR
  3. SENSIBLE_EDITOR
  4. SELECTED_EDITOR, normally written by select-editor
  5. The editor command selected by Debian's alternatives system
  6. nano, nano-tiny, then vi

Unset and empty variables are skipped. A variable may contain whitespace-separated parameters, so it can name an editor with a fixed option. The command tries a candidate and moves to the next one when it cannot execute it, with exit status 126 or 127. If all candidates fail, the command reports an error.

The order is easy to misread: VISUAL wins even when EDITOR is also set. Check the values before diagnosing an unexpected editor:

$ printf 'VISUAL=%s\nEDITOR=%s\nSENSIBLE_EDITOR=%s\nSELECTED_EDITOR=%s\n' \\
    "$VISUAL" "$EDITOR" "$SENSIBLE_EDITOR" "$SELECTED_EDITOR"
VISUAL=vim
EDITOR=nano
SENSIBLE_EDITOR=
SELECTED_EDITOR=

Your output will differ. An empty line is not a configured choice.

3. Override the editor for one command

Use an environment assignment on the same command when you need a temporary choice. This safe test uses /bin/echo, so it does not open an interactive editor:

$ VISUAL=/bin/echo sensible-editor /tmp/example.txt
/tmp/example.txt
$ printf 'exit status: %s\n' "$?"
exit status: 0

The assignment applies only to this invocation and does not change your shell's future environment. Replace /tmp/example.txt with the file you actually intend to edit after you have checked the selected editor.

To pass an editor option, include it in the variable value, then pass the file separately:

$ VISUAL='/bin/echo --verbose' sensible-editor /tmp/example.txt
--verbose /tmp/example.txt

That value is split into words by sensible-editor; it is not a full shell command line. Do not put shell pipelines, redirections or untrusted text into an editor variable and expect shell syntax to work. The selected command is executed directly.

4. Set a persistent user choice

For an interactive preference, set EDITOR or VISUAL in your shell startup configuration. VISUAL has higher priority, so set both deliberately or remove an older value that would override your choice.

$ printf '%s\n' 'export EDITOR=vim' 'export VISUAL=vim' >> "$HOME/.profile"
$ . "$HOME/.profile"
$ printf 'EDITOR=%s VISUAL=%s\n' "$EDITOR" "$VISUAL"
EDITOR=vim VISUAL=vim

This appends state to $HOME/.profile, so it is a persistent change. Before running the command, inspect the file and make a backup if it already contains editor settings:

$ grep -nE '(^|export )(VISUAL|EDITOR)=' "$HOME/.profile"

Recovery: edit the file and remove the lines you added, then start a new login shell or source the file again. If your shell or desktop does not read .profile, use the startup file documented for that environment. Do not use sudo for a per-user preference.

5. Choose through Debian's selector

If you want Debian's interactive selector to write the selected command, run:

$ select-editor

Choose an installed editor when prompted. The selected value is then considered after the environment variables and before the alternatives-managed editor command. This is a user-level choice, not a system-wide alternatives change.

Do not assume that select-editor will win while VISUAL or EDITOR is set. Clear a temporary shell override before testing the selection:

$ env -u VISUAL -u EDITOR -u SENSIBLE_EDITOR sensible-editor /tmp/example.txt

That command may open an interactive editor. Use it only with a real path you intend to edit, and save or quit using that editor's normal controls.

6. Diagnose a failed launch

Capture the status immediately if the command does not start the editor:

$ sensible-editor /path/to/file
$ status=$?
$ printf 'sensible-editor exit status: %s\n' "$status"
sensible-editor exit status: 127

These are the conditions for which the script tries another candidate; a real editor failure after it starts is not necessarily recoverable through another fallback.

Check the candidates without changing configuration:

$ command -v "$VISUAL"
$ command -v "$EDITOR"
$ command -v nano
$ command -v nano-tiny
$ command -v vi

Empty variables make command -v unhelpful, so inspect their values first. Also check for a loop: setting EDITOR=sensible-editor makes the command call itself. The script guards against the direct case, but indirect loops through wrappers can still occur. Set a real editor command while investigating.

Done means