VISUAL vs. EDITOR – what’s the difference?
The EDITOR
editor should be able to work without use of "advanced" terminal functionality (like old ed
or ex
mode of vi
). It was used on teletype terminals.
A VISUAL
editor could be a full screen editor as vi
or emacs
.
E.g. if you invoke an editor through bash (using C-x C-e
), bash will try first VISUAL
editor and then, if VISUAL
fails (because terminal does not support a full-screen editor), it tries EDITOR
.
Nowadays, you can leave EDITOR
unset or set it to vi -e
.
The accepted answer is probably a good, short treatment, but this will be an attempt to go deeper on when the distinction between VISUAL and EDITOR might still matter (building on Adam Katz's answer).
The POSIX spec still distinguishes between visual mode editors and line editors. This really mattered back in the days when cursor positioning over serial connections was hard (especially because of the speed of the serial connection). The Wikipedia article for vi gives some useful background on the distinction between vi (a visual mode editor) and ex (a line editor). If you dig deep enough down the research, you'll find the "RATIONALE" section of the "ex" spec, which gives a reason for the distinction still being in the spec:
It is recognized that portions of vi would be difficult, if not impossible, to implement satisfactorily on a block-mode terminal, or a terminal without any form of cursor addressing, thus it is not a mandatory requirement that such features should work on all terminals. It is the intention, however, that a vi implementation should provide the full set of capabilities on all terminals capable of supporting them.
I haven't needed this since giving up my 300 baud modem, but I can imagine that people who use slow serial lines to connect to embedded systems (and/or over really dicey connections) might still appreciate being able to have a preferred line mode editor distinct from a "visual" editor like vi. VT100-style terminal codes over a lossy, laggy, narrow connection might be "bloat" in limited applications.
For the rest of us, it seems the "correct" answer seems to be "set them both to be your preferred editor". It might be ok to co-opt this distinction for local/graphical editor (e.g. Sublime or gvim) vs a terminal window editor (e.g. vi or emacs), but there's likely a mountain of legacy reasons why that probably won't work as hoped.
I've concluded that $VISUAL
is graphical and $EDITOR
is command line. If undefined, anything seeking $VISUAL
should then try $EDITOR
next.
(Citation needed: I'd love to get the proper documentation, perhaps a man page or POSIX spec?)
At the moment, I have stuff like this in my ~/.bashrc
and ~/.zshrc
:
EDITOR="$(command -v vim)"
# we have gvim, not in an SSH term, and the X11 display number is under 10
if command -v gvim >/dev/null 2>&1 \
&& [ "$SSH_TTY$DISPLAY" = "${DISPLAY#*:[1-9][0-9]}" ]; then
export VISUAL="$(command -v gvim) -f"
SUDO_EDITOR="$VISUAL"
else
SUDO_EDITOR="$EDITOR"
fi
gvim
without -f
won't work with programs that expect to act on your edits. This definitely includes sudoeditor
(sudo -e
).
This may break if you have whitespace in the path to vim. If that's a problem, either install it properly or else consider symlinks like /usr/local/bin/gvim