2

I often see instructions that include vim or nano, meaning to open the file in that step in your text editor of choice. Is there an agnostic command I can use in place of the specific program that would open the input in the user's default in-terminal text editor, whether it's vim, nano, or something else?

I see editor mentioned in the Similar Questions sidebar—is that still limited to Debian-based distros? And is there any alternative?

3

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.

  • $EDITOR only makes sense if it's defined. Invoking ed hardly counts as standard and is abysmal in terms of user-friendliness. edit is perfectly fine on Debian, but may invoke vi on other systems (where does it invoke ex?). – Gilles Oct 17 '16 at 22:38
  • Gilles: You're right, it only makes sense if it's been defined. It was the closest thing to a "universal" command per the OP's question. "Ed is the standard" is an old joke, since it's the one defined in the POSIX standard (see "man ed" or Google "Ed is the standard" for more info). I don't know about "edit" on other systems, but mine invokes ex (Arch Linux), so it can't be considered universal. – jsw85 Oct 18 '16 at 1:22
  • POSIX also defines vi. Jokes are fine, but don't present them as something serious — I've heard that one before but your target audience hasn't. – Gilles Oct 18 '16 at 11:45
2

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.

0

If you are using Bash, which is the default terminal language for all linux distros (as far as I'm aware), there is something called environmental variables. Many of these variables are set up by your distribution, where some need to be set or 'exported' manually. $EDITOR is one in particular (need caps and dollar sign). The dollar sign signifies the variable being called on, and the caps signify that its an environmental variable. $HOME is a common env. variable which is shorthanded by ~.

So to answer your questions succinctly, the command you would run to use a users default editor edit file.txt

$EDITOR file.txt

To set or export the default editor to vim put this in the user's $HOME/.bashrc file:

export EDITOR=/bin/vim

then run:

source $HOME/.bashrc

If the $EDITOR variable is not set, then there really isn't a default editor.

As far as alternatives go, there are infinitely many by aliases:

Alias E="$EDITOR"

After being sourcing the .bashrc file, this would allow me to run the first command above by:

E text.vim

However, don't get too carried away with aliases, as they can be a major hinderence towards more advanced bash usage.


Another useful and similar environmental variable is $PAGER, which can be used to open the default program for document viewing.

$PAGER file.txt

The usual default for this env variable is "less."

  • (1) bash isn’t a “terminal language,” it’s a shell, also known as a command-line interpreter (CLI).  (2) I believe that pretty much every shell in use these days supports environment variables, so you don’t really need to caveat that.  On the other hand, there are some shells that don’t understand the export command, and I’m not sure about source and alias.  (3) alias must be in lower case (unless you have defined an alias for Alias). – Scott Oct 17 '16 at 4:54
  • Probably a better way of putting it. Thanks! – Dave Oct 19 '16 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.