Question

In linux, how can I determine the default text editor, terminal, etc?

does it vary by distro?

Was it helpful?

Solution

I don't think that the notion of default editor or terminal makes sense. For the editor, there is the convention of using $EDITOR when it is defined.

On Debian and related (e.g. Ubuntu, Mint) you have paths like /usr/bin/editor and /usr/bin/x-terminal-emulator symlinked (via symlinks in /etc/alternatives/) to some system default.

See also Dacav's answer

OTHER TIPS

I accessed the list while ago programatically in a rather ugly manner and I'm sure its not the best way. The options are stored in a file called defaults.list (I think this is generally the case). The location is less general I think it in /usr/share/applications/ on my ubuntu system although it does appear elsewhere I think. I then treated it as a text file.

The resolution order is $EDITOR -> editor -> some predefined list of console editors. On Debian/Ubuntu, sensible-editor (and sensible-browser and sensible-pager) will do the lookup for you, including looking at the right environment variables. Similar variables are $PAGER, $SHELL, $BROWSER. To look up a file association, you can use xdg-open.

Per-user environment variables tells you that.

  • $EDITOR gives you the command to be launched as text editor;
  • $BROWSER gives you the browser
  • $PAGER gives you the pager (ex. more or less).

This however is valid for command line softwares, while usually desktop environments use their own (internal) variables.

Also in python you can read the environment variables using os.getenv.

This works in ubuntu/gnome:

>>> query_lines = subprocess.check_output(['update-alternatives',
                                           '--query',
                                           'gnome-text-editor']).split('\n')
>>> bestlist = filter(lambda l: 'Best' in l, query_lines)
>>> bestlist[0].split()[1]
'/usr/bin/gedit'

If not on gnome, you could at least get the command-line editor:

>>> query_lines = subprocess.check_output(['update-alternatives',
...                                        '--query',
...                                        'editor']).split('\n')
>>> bestlist = filter(lambda l: 'Best' in l, query_lines)
>>> bestlist[0].split()[1]
'/bin/nano'

Taking hint from @bowler's answer, and purely bash:

editor=$(grep "text/plain" -r -i -I /usr/share/applications/defaults.list | awk -F';' '{print $1}' | awk -F'.desktop' '{print $1}' | awk -F'=' '{print $2}')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top