3

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 Apr 11 '19 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? – Ivan Molodetskikh Apr 11 '19 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 Apr 11 '19 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. – Ivan Molodetskikh Apr 11 '19 at 14:25
  • 1
    Good point, I added a paragraph about that. – Ivan Molodetskikh Apr 11 '19 at 14:27
2

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

$HOME/.local/share/applications/
/usr/share/applications/

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).

|improve this answer|||||
  • update-alternatives seems to not be present on my system (Arch Linux). – Ivan Molodetskikh Apr 11 '19 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 Apr 11 '19 at 14:29
  • Yeah, x-terminal-emulator isn't present here either, unfortunately. – Ivan Molodetskikh Apr 11 '19 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 Apr 11 '19 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.