Command for the default in-terminal text editor
You can use $EDITOR
, provided that it's been defined:
$EDITOR filename.txt
But I think most docs use nano
because if someone's blindly following along, it's a safe bet to use. If the user has decided they actually prefer one editor over another, they'll know enough to replace it with vim
, emacs
, etc themselves.
edit
may work well on Debian-based systems, but on others it invokes ex
, which isn't recommended.
If the environment variable VISUAL
is set, use that.
Otherwise, if the environment variable EDITOR
is set, use that.
Otherwise, Unix tradition defaults to vi
. This is not at all user-friendly — people who use vi know how to set up their system to invoke it, your application should be friendly to those users who don't. Unfortunately there's no good, portable way to find a decent editor. You can try xdg-mime query default
, but even where the utility is available, it doesn't always work. On Debian and Debian-like systems, invoke sensible-editor
, which does all that stuff for you — but I don't know of anything like it on other Unix variants.
This yields code like
#!/bin/sh
if [ -n "$VISUAL" ]; then
exec $VISUAL "$@"
elif [ -n "$EDITOR" ]; then
exec $EDITOR "$@"
elif type sensible-editor >/dev/null 2>/dev/null; then
exec sensible-editor "$@"
elif cmd=$(xdg-mime query default ) 2>/dev/null; [ -n "$cmd" ]; then
exec "$cmd" "$@"
else
editors='nano joe vi'
if [ -n "$DISPLAY" ]; then
editors="gedit kate $editors"
fi
for x in $editors; do
if type "$x" >/dev/null 2>/dev/null; then
exec "$x" "$@"
fi
done
fi
Most programs do whitespace splitting on $VISUAL
and $EDITOR
, but not all.