Open a file in the default text editor programmatically The 2019 Stack Overflow Developer...

Getting crown tickets for Statue of Liberty

What is this business jet?

Will it cause any balance problems to have PCs level up and gain the benefits of a long rest mid-fight?

Short story: man watches girlfriend's spaceship entering a 'black hole' (?) forever

Are spiders unable to hurt humans, especially very small spiders?

Loose spokes after only a few rides

Correct punctuation for showing a character's confusion

How to support a colleague who finds meetings extremely tiring?

What is the most efficient way to store a numeric range?

The phrase "to the numbers born"?

Can a flute soloist sit?

What does Linus Torvalds mean when he says that Git "never ever" tracks a file?

Why can I use a list index as an indexing variable in a for loop?

How do I free up internal storage if I don't have any apps downloaded?

Kerning for subscripts of sigma?

Is Cinnamon a desktop environment or a window manager? (Or both?)

Button changing its text & action. Good or terrible?

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past

Accepted by European university, rejected by all American ones I applied to? Possible reasons?

How to obtain a position of last non-zero element

The difference between dialogue marks

What is this sharp, curved notch on my knife for?

Does adding complexity mean a more secure cipher?



Open a file in the default text editor programmatically



The 2019 Stack Overflow Developer Survey Results Are InText editor with split screen optionOpen .desktop files in text editor by defaultAny text-editor which list numbers of words in a file?Text editor that allows for coloring of arbitrary text (and exporting thereof)?Launch editor from a shell script with textXDG resolves filename as text/plainCommand for the default in-terminal text editorArch linux + I3 + URXVT: ROFI opens nano editor in bash instead of URXVTArchLinux: Still having the wrong editor in some casesWhat is the correct way to associate MIME type with applications that have multiple desktop entries?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















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?










share|improve this question









New contributor




Ivan Molodetskikh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • 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
    14 hours ago













  • 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
    14 hours ago













  • 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
    14 hours ago













  • 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
    14 hours ago






  • 1





    Good point, I added a paragraph about that.

    – Ivan Molodetskikh
    14 hours ago


















1















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?










share|improve this question









New contributor




Ivan Molodetskikh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • 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
    14 hours ago













  • 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
    14 hours ago













  • 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
    14 hours ago













  • 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
    14 hours ago






  • 1





    Good point, I added a paragraph about that.

    – Ivan Molodetskikh
    14 hours ago














1












1








1








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?










share|improve this question









New contributor




Ivan Molodetskikh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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?







desktop editors mime-types






share|improve this question









New contributor




Ivan Molodetskikh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ivan Molodetskikh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 14 hours ago







Ivan Molodetskikh













New contributor




Ivan Molodetskikh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 14 hours ago









Ivan MolodetskikhIvan Molodetskikh

62




62




New contributor




Ivan Molodetskikh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ivan Molodetskikh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ivan Molodetskikh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • 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
    14 hours ago













  • 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
    14 hours ago













  • 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
    14 hours ago













  • 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
    14 hours ago






  • 1





    Good point, I added a paragraph about that.

    – Ivan Molodetskikh
    14 hours ago



















  • 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
    14 hours ago













  • 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
    14 hours ago













  • 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
    14 hours ago













  • 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
    14 hours ago






  • 1





    Good point, I added a paragraph about that.

    – Ivan Molodetskikh
    14 hours ago

















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
14 hours ago







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
14 hours ago















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
14 hours ago







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
14 hours ago















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
14 hours ago







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
14 hours ago















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
14 hours ago





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
14 hours ago




1




1





Good point, I added a paragraph about that.

– Ivan Molodetskikh
14 hours ago





Good point, I added a paragraph about that.

– Ivan Molodetskikh
14 hours ago










1 Answer
1






active

oldest

votes


















0














The *.desktop file you get 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 store their 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).






share|improve this answer


























  • update-alternatives seems to not be present on my system (Arch Linux).

    – Ivan Molodetskikh
    14 hours ago











  • 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
    14 hours ago











  • Yeah, x-terminal-emulator isn't present here either, unfortunately.

    – Ivan Molodetskikh
    14 hours ago











  • 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
    14 hours ago












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Ivan Molodetskikh is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511931%2fopen-a-file-in-the-default-text-editor-programmatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The *.desktop file you get 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 store their 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).






share|improve this answer


























  • update-alternatives seems to not be present on my system (Arch Linux).

    – Ivan Molodetskikh
    14 hours ago











  • 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
    14 hours ago











  • Yeah, x-terminal-emulator isn't present here either, unfortunately.

    – Ivan Molodetskikh
    14 hours ago











  • 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
    14 hours ago
















0














The *.desktop file you get 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 store their 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).






share|improve this answer


























  • update-alternatives seems to not be present on my system (Arch Linux).

    – Ivan Molodetskikh
    14 hours ago











  • 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
    14 hours ago











  • Yeah, x-terminal-emulator isn't present here either, unfortunately.

    – Ivan Molodetskikh
    14 hours ago











  • 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
    14 hours ago














0












0








0







The *.desktop file you get 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 store their 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).






share|improve this answer















The *.desktop file you get 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 store their 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).







share|improve this answer














share|improve this answer



share|improve this answer








edited 13 hours ago

























answered 14 hours ago









cprncprn

3621715




3621715













  • update-alternatives seems to not be present on my system (Arch Linux).

    – Ivan Molodetskikh
    14 hours ago











  • 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
    14 hours ago











  • Yeah, x-terminal-emulator isn't present here either, unfortunately.

    – Ivan Molodetskikh
    14 hours ago











  • 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
    14 hours ago



















  • update-alternatives seems to not be present on my system (Arch Linux).

    – Ivan Molodetskikh
    14 hours ago











  • 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
    14 hours ago











  • Yeah, x-terminal-emulator isn't present here either, unfortunately.

    – Ivan Molodetskikh
    14 hours ago











  • 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
    14 hours ago

















update-alternatives seems to not be present on my system (Arch Linux).

– Ivan Molodetskikh
14 hours ago





update-alternatives seems to not be present on my system (Arch Linux).

– Ivan Molodetskikh
14 hours ago













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
14 hours ago





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
14 hours ago













Yeah, x-terminal-emulator isn't present here either, unfortunately.

– Ivan Molodetskikh
14 hours ago





Yeah, x-terminal-emulator isn't present here either, unfortunately.

– Ivan Molodetskikh
14 hours ago













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
14 hours ago





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
14 hours ago










Ivan Molodetskikh is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Ivan Molodetskikh is a new contributor. Be nice, and check out our Code of Conduct.













Ivan Molodetskikh is a new contributor. Be nice, and check out our Code of Conduct.












Ivan Molodetskikh is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511931%2fopen-a-file-in-the-default-text-editor-programmatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...