15

While reading the git introductory material, I read a piece that brought this question to mind.

How do I use the Terminal to find out what my Default Text Editor is?

Are there a General Commands that I can use to find this out?

0

3 Answers 3

20

In Ubuntu, there is a generic editor command which is set by the Debian alternatives system.

If you do:

editor foobar.txt

the file will be opened using the original editor e.g. vim, nano which is prioritized as editor currently.

You can check the details with:

update-alternatives --display editor

To set a new editor as editor:

sudo update-alternatives --config editor

Also note that bash checks some environment variables for tasks related to it, to be exact bash checks VISUAL, EDITOR one after another. If unset, bash defaults to nano (unless an editor you've installed has overridden this).

Some processes spawned from bash check these environment variables too.

5
  • 1
    Are you sure it's emacs? If I unset VISUAL and EDITOR e.g. sudo visudo runs in nano for me
    – kos
    Dec 14, 2015 at 9:38
  • Check my answer guys, I've updated it a bit, with man page exerpt Dec 14, 2015 at 9:43
  • This is a peculiarity specific to Ubuntu, because it has the Debian alternatives system. On systems, without it git has to check EDITOR variable Dec 14, 2015 at 9:48
  • 1
    Actually no I'm sure it can't be emacs, since emacs is not installed by default. Installing emacs probably overrode your previous configuration.
    – kos
    Dec 14, 2015 at 9:50
  • 1
    this one should be correct answer. imho
    – buncis
    Nov 22, 2017 at 14:36
12

The default editor is as defined by the EDITOR, or VISUAL, environment variable(s).

The default editor is vi if neither were defined. Add

export EDITOR="/bin/nano" 

to your ~/.bashrc file to set, for example, nano as your default editor.

To see if the environment variable is set, you can use

printenv | grep EDITOR

or

set | grep EDITOR

One can dereference the value of the named environment variable by prefixing it with a "$"

$ echo $EDITOR

or

$ echo $VISUAL
5
  • @heemayl: Thanks for your edit, as I forgot to format properly. However, I very specifically wanted to say $EDITOR and not EDITOR. Why? Because one refers to the environment variable by preceding the name with a $ sign. For example $ $EDITOR bla.txt or echo $EDITOR or ... So, I am going to reverse the deletion of the $ sign. Dec 15, 2015 at 16:08
  • Yes, and I wanted to refer to the value of the variable. Dec 15, 2015 at 17:16
  • @heemayl: I'll do an edit,see if you like it, and then delete these comments. It will be a few hours until I can do it though. Dec 15, 2015 at 17:25
  • @heemayl: Is this O.K. with you? Dec 15, 2015 at 21:46
  • 1
    @DougSmythies , identifiers (aka names that define i.e. a variable) are language and context dependent. if you're going to use the $ sign, it should be to dereference the variable, meaning that you're accessing the value of the variable. you don't use the $ sign when assigning a value to that variable; so when you refer to the name of one, you do not use it.
    – user383919
    Dec 16, 2015 at 0:31
4

There is actually git var -l which allows you to list the variables, including GIT_EDITOR variable. Here's mine (private info is unset of course):

$ git var -l
user.name=*****
user.email=****
GIT_COMMITTER_IDENT=****
GIT_AUTHOR_IDENT=****
GIT_EDITOR=editor
GIT_PAGER=pager

As heemayl already pointed out, editor command is the one set by /etc/alternatives/editor. In my case, that's nano (which I assume is default for Ubuntu, because I don't remember consciously making an effort to change my default editor).

But on other systems other than Ubuntu (or I should say which have no Debian's alternatives system) , there is no editor. Let's, however read up the man git:

GIT_EDITOR

This environment variable overrides $EDITOR and $VISUAL. It is used by several Git commands when, on interactive mode, an editor is to be launched. See also git-var(1) and the core.editor option in git- config(1).

And if we look through git-var it tells us

The order of preference is the $GIT_EDITOR environment variable, then core.editor configuration, then $VISUAL, then $EDITOR, and then the default chosen at compile time, which is usually vi.

Thus it is a mere perculiarity of Ubuntu that it has Debian's alternatives system. On other systems which don't have Debian's alternatives systems it would default to vi

0