What is the `editor` command in bash?

33,687

Solution 1

This is a Debian-ism (and therefore appears in Ubuntu, Mint, etc.). They've setup a link called editor. You can trace it back as follows:

$ which editor
/usr/bin/editor

$ ls -l /usr/bin/editor 
lrwxrwxrwx 1 root root 24 Nov 24 19:10 /usr/bin/editor -> /etc/alternatives/editor

$ ls -l /etc/alternatives/editor
lrwxrwxrwx 1 root root 18 Nov 24 19:46 /etc/alternatives/editor -> /usr/bin/vim.gnome

$ ls -l /usr/bin/vim.gnome
-rwxr-xr-x 1 root root 2403392 Oct 26  2012 /usr/bin/vim.gnome

So you can see that editor is just a Unix link to the executable vim.gnome.

Using editor?

I don't think I'd go in that direction of setting up editor in any meaningful way for users, given it's not what I would consider standard.

Additionally you can set the $EDITOR environment variable to point to anything you want, vim, gedit, emacs, etc. But this variable is only guaranteed to be used by other tools such as sudo, git, and subversion that are specifically designed to be tied into using the variable $EDITOR.

Implementation ideas

I would merely setup an alias of your own choice and either instruct the users that it's available to them via their $HOME/.bashrc file as alias X, or set it up as a system configuration in the file /etc/profile.d/our_aliases.sh, as alias X.

Or you could just tell users that the systems' provide gedit, gvim, vim, emacs, etc. and cut through the sugar coating and teach them about these things right off the bat.

Or you could provide a text file called /etc/help.txt which they could run via a command help (alias help="less /etc/help.txt") in a shell that would provide basic commands and how to perform various tasks. This approach allows you to customize the help as time goes by with new features or tips and it gives them more than just the editor convenience command.

Solution 2

The editor program called by some programs when you tell them to edit a file. You have to set the environment variable yourself.

If you use csh or tcsh, at the shell prompt:

setenv EDITOR vim

If you use bash or ksh, then enter:

EDITOR=vim; export EDITOR

(Replace "vim" with the editor you want to use.)

The EDITOR variable is the one you need by default for some applications to invoke the editor. There is another variable called VISUAL used to specify the screen-oriented editor. Generally you will want to set it to the same value as the EDITOR variable. Originally EDITOR would have be set to ed (a line-based editor) and VISUAL would've been set to vi (a screen-based editor).

VISUAL="vim"; export VISUAL
VISUAL="$EDITOR"; export VISUAL  #even better

When you have done this, most Unix programs that use text editors will use the editor you have set. By following the commands above, you will set the default editor for the current computing session only. To make these changes permanent, you will need to place the appropriate commands described above in your .login or .cshrc files (for csh or tcsh users) or your .profile file (if you use bash or ksh).

There is also another environment variable, if you pretend to use Emacs. It's ALTERNATE_EDITOR, and has the same effect, but it's used when Emacs is invoked with --alternate-editor option.

From the GNU Emacs Manual:

The option --alternate-editor=command is useful when running emacsclient in a script. It specifies a command to run if emacsclient fails to contact Emacs. For example, the following setting for the EDITOR environment variable will always give an editor, even if Emacs is not running:

EDITOR="emacsclient --alternate-editor vi +%d %s"

Solution 3

You will have to specify the editor in the environment variable EDITOR. Add the following command in your .profile:

export EDITOR=editor

If you want vim as your default editor, you will add instead

export EDITOR=vim

Personal preference: I use the absolute path name of the editor in the environment variable. For example,

export EDITOR=/usr/bin/vim

Solution 4

The editor command is ubuntu specific. It is not portable or even common (outside of ubuntu). The command is also set system wide, and not per-user. The way it works is that /usr/bin/editor is just a symlink to an editor on the system. As such it is the same for all users.

The $EDITOR environment variable is fairly standard. It is used by many programs which need the user to edit something. As such, it is this variable you should use when you want to invoke the user's editor.

Solution 5

This is what I am using in my script:

# Find a suitable text editor
editor="$VISUAL"
[ -z "$editor" ] && editor="$EDITOR"
[ -z "$editor" ] && which editor >/dev/null && editor=editor
[ -z "$editor" ] && which nano   >/dev/null && editor=nano
[ -z "$editor" ] && which vi     >/dev/null && editor=vi
[ -z "$editor" ] && editor=no_editor_found

# Use the editor we found
$editor "$filename"

I have not wrapped quotes around the final call to $editor to ensure that the emacsclient example above will work.

Because of that, I think it is important to always set some value for editor, otherwise the last line could unintentionally execute $filename. In my case I chose no_editor_found but you could equally use pico or ed there.

Share:
33,687

Related videos on Youtube

Dan Ross
Author by

Dan Ross

West Coast Canadian rocking the startup scene in the Middle East with expats from all over the world. When I'm home, I'm big on high quality bacon, local beer, and homegrown awesomeness. I'm always down for a new adventure. On the other side of the planet, I usually just tag along with the locals on whatever craziness they happen to be up to. Slowly learning the Arabic that makes up most of our data. Ma'asa Salama!

Updated on September 18, 2022

Comments

  • Dan Ross
    Dan Ross almost 2 years

    I was looking for a command that would hopefully open the current user's favourite text editor, because I am writing out some instructions with commands in a blog. I was expecting a command like edit, and I found editor. For me, it started vim in the terminal, which is close to what I wanted; I use vim-gtk. I started searching for a way to make editor use vim-gtk and found this question that explains an $EDITOR environment variable, but I am not sure if that is even related to the editor command. I tried man editor, but that just pulled up the vim manpage.

    How can I make editor use a text editor of my choice, or should I be using a different command?

  • Stéphane Chazelas
    Stéphane Chazelas about 11 years
    Saying sh doesn't make sense. sh is usually the system's shell. It used to be the Thomson shell, then the Bourne shell and then nowadays on most systems, one shell or another implementing the POSIX sh specification. The export EDITOR=vim syntax is POSIX, but not Bourne nor Thomson. The export VISUAL=$EDITOR works in bash and some implementations of ksh. You need export VISUAL="$EDITOR" in most other shells. VISUAL=$EDITOR export VISUAL will work in all Bourne-like shells including the Bourne shell.
  • Dan Ross
    Dan Ross about 11 years
    Ya, users will know what their editor is called, and I will just tell them to edit a particular file instead of showing a code example. I was on a roll with the code examples and I didn't realize that I was getting a little pedantic. It's too bad that Ubuntu doesn't set the $EDITOR variable, now I can't rely on that or the editor symlink.
  • Stéphane Chazelas
    Stéphane Chazelas about 11 years
    It is not Ubuntu specific, it comes from Debian and uses Debian's alternatives mechanism. It's available in all Debian derivatives, not only Ubuntu.
  • Dan Ross
    Dan Ross about 11 years
    standards, sigh...
  • elpie89
    elpie89 over 8 years
    Is it common and accepted to set EDITOR to a multi-word value like that? If so, I cannot safely use which "$EDITOR" or which $EDITOR to check if it is present.