I'm writing a program and I need to implement opening a particular file in a text editor. The file type has a different association by default (it's associated to the program itself), so calling xdg-open $file wouldn't work. Using $EDITOR is sub-optimal because it usually contains the terminal text editor, and I'd like to open a GUI text editor if that's the first preference. It is also unclear how to find the default terminal emulator to open a terminal $EDITOR.

I can query the (possibly GUI) editor with xdg-mime query default text/plain, which gives me a .desktop file, but I'm not sure how to go about actually running it, especially since it may contain Terminal=true which then again raises an issue of figuring out what the default terminal is.

To clarify, the program is not a script or something which already runs in the terminal. It is a GUI application, so launching a terminal editor would require figuring out what the default terminal is.

So, what's the best way to programmatically open a file in a default, possibly GUI, text editor?

  • If a GUI text editor is the first preference of the user, they should set EDITOR accordingly, IMHO. What if a user isn't even using X, i.e. they are using your script on a head-less machine without ever having used any graphical environment with their account?
    – Kusalananda
    Commented Apr 11, 2019 at 14:16
  • Then, if it is indeed a terminal editor, how do I open that? I'd need to somehow retrieve the default terminal emulator? Commented Apr 11, 2019 at 14:20
  • My point is that you, as the script writer, should not worry about what editor the user wants or can use. It would be enough to use "$EDITOR" "$file" (or possibly "${EDITOR:-vi}" "$file" to use a default value) in the script, and then document the fact that $EDITOR is use for editing files from within the script. This is what most other utilities do. You would otherwise built in a dependency on a particular set of applications. This is personal opinion on my part though.
    – Kusalananda
    Commented Apr 11, 2019 at 14:24
  • The problem is that it's not a script, so I can't just call $EDITOR if it's a terminal editor. I'd need to launch an actual terminal. Commented Apr 11, 2019 at 14:25
  • 1
    Good point, I added a paragraph about that. Commented Apr 11, 2019 at 14:27

1 Answer 1


The *.desktop file you get by calling xdg-mime query default text/plain is in one of two locations:


So you need to look for it there, then parse the Exec= line and run it passing all the arguments, e.g.:

$(grep Exec /usr/share/applications/seahorse.desktop | cut -d= -f2) "$@"

To determine the default terminal emulator on debian-like distributions you can run the following and similarly parse the output to get the path:

update-alternatives --query x-terminal-emulator

or just try to run the command you need directly:

x-terminal-emulator -e "your command here"

If you need to make it distribution agnostic you'll need to implement Desktop Environment guessing as it seems to depend on the way each DE stores its configuration. You might want to take a look at xdg-terminal code which is part of xdg utilities package responsible for running the default terminal (but for some reason isn't distributed).

  • update-alternatives seems to not be present on my system (Arch Linux). Commented Apr 11, 2019 at 14:28
  • Good catch, it's for Debian derivatives. Try x-terminal-emulator -e $your_command but my guess is it's Debian as well... I'll update the answer when I find a more POSIX solution.
    – cprn
    Commented Apr 11, 2019 at 14:29
  • Yeah, x-terminal-emulator isn't present here either, unfortunately. Commented Apr 11, 2019 at 14:31
  • Well, the only "official" way of doing it I found is xdg-terminal but I can't find it installed on any of my distributions. You might need to duplicate what it does.
    – cprn
    Commented Apr 11, 2019 at 14:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .