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?
EDITORaccordingly, 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?"$EDITOR" "$file"(or possibly"${EDITOR:-vi}" "$file"to use a default value) in the script, and then document the fact that$EDITORis 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.$EDITORif it's a terminal editor. I'd need to launch an actual terminal.