7

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?

Wolf
  • 1,567
  • 8
  • 17
  • 30
  • Related, not exactly a duplicate: https://unix.stackexchange.com/questions/4859/visual-vs-editor-whats-the-difference – Michael Homer Oct 17 '16 at 02:29
  • in Debian you can also configure the program executed when you call "editor" by executing once: "update-alternatives --config editor". There you can select the one you prefer (default is nano). This will e.g. make so that when you press "v" in less you get the editor you want, so you can switch from nano to vi. – user1708042 Apr 07 '20 at 07:37

4 Answers4

6

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.

jsw85
  • 76
  • $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 'SO- stop being evil' 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 01: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 'SO- stop being evil' Oct 18 '16 at 11:45
5

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.

  • Note that sensible-editor itself incorporates the environment variable lookups and fallbacks, so if sensible-editor exists one need not do pretty much any of the other stuff, and one uses it in place of your script rather than as part of it. Conversely, if sensible-editor does not exist, and the environment variables do not exist, the editor controlled by Debian's/RedHat's "alternatives" system is a better fallback than hardwired names. http://jdebp.uk./FGA/unix-editors-and-pagers.html – JdeBP Jan 02 '20 at 15:21
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."

Dave
  • 1,025
  • (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 04:54
  • Probably a better way of putting it. Thanks! – Dave Oct 19 '16 at 15:20
0

A simpler version of Giles' answer that could be inlined to launch the editor in a line of script could be something like:

command ${VISUAL:-${EDITOR:-$(command -v vi)}} $file

(This would need tweaking to get it to work with default editor arguments, though, like when EDITOR="emacs -nw". The exec calls in the linked answer have this issue as well. command may be a bit safer than exec.)