Open a file in the default text editor programmatically The 2019 Stack Overflow Developer Survey Results Are In Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara 2019 Community Moderator Election ResultsText 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?
Mortgage adviser recommends a longer term than necessary combined with overpayments
Match Roman Numerals
Didn't get enough time to take a Coding Test - what to do now?
Why are PDP-7-style microprogrammed instructions out of vogue?
The following signatures were invalid: EXPKEYSIG 1397BC53640DB551
How do I design a circuit to convert a 100 mV and 50 Hz sine wave to a square wave?
What was the last x86 CPU that did not have the x87 floating-point unit built in?
Identify 80s or 90s comics with ripped creatures (not dwarves)
Would an alien lifeform be able to achieve space travel if lacking in vision?
should truth entail possible truth
What force causes entropy to increase?
For what reasons would an animal species NOT cross a *horizontal* land bridge?
Why can't wing-mounted spoilers be used to steepen approaches?
Why can't devices on different VLANs, but on the same subnet, communicate?
Presidential Pardon
US Healthcare consultation for visitors
How to type a long/em dash `—`
Why doesn't a hydraulic lever violate conservation of energy?
First use of “packing” as in carrying a gun
60's-70's movie: home appliances revolting against the owners
One-dimensional Japanese puzzle
Sub-subscripts in strings cause different spacings than subscripts
Variable with quotation marks "$()"
How to support a colleague who finds meetings extremely tiring?
Open a file in the default text editor programmatically
The 2019 Stack Overflow Developer Survey Results Are In
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
2019 Community Moderator Election ResultsText 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;
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
New contributor
|
show 2 more comments
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
New contributor
If a GUI text editor is the first preference of the user, they should setEDITOR
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♦
yesterday
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
yesterday
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♦
yesterday
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
yesterday
1
Good point, I added a paragraph about that.
– Ivan Molodetskikh
yesterday
|
show 2 more comments
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
New contributor
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
desktop editors mime-types
New contributor
New contributor
edited yesterday
Ivan Molodetskikh
New contributor
asked yesterday
Ivan MolodetskikhIvan Molodetskikh
83
83
New contributor
New contributor
If a GUI text editor is the first preference of the user, they should setEDITOR
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♦
yesterday
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
yesterday
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♦
yesterday
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
yesterday
1
Good point, I added a paragraph about that.
– Ivan Molodetskikh
yesterday
|
show 2 more comments
If a GUI text editor is the first preference of the user, they should setEDITOR
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♦
yesterday
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
yesterday
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♦
yesterday
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
yesterday
1
Good point, I added a paragraph about that.
– Ivan Molodetskikh
yesterday
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♦
yesterday
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♦
yesterday
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
yesterday
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
yesterday
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♦
yesterday
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♦
yesterday
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
yesterday
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
yesterday
1
1
Good point, I added a paragraph about that.
– Ivan Molodetskikh
yesterday
Good point, I added a paragraph about that.
– Ivan Molodetskikh
yesterday
|
show 2 more comments
1 Answer
1
active
oldest
votes
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).
update-alternatives
seems to not be present on my system (Arch Linux).
– Ivan Molodetskikh
yesterday
Good catch, it's for Debian derivatives. Tryx-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
yesterday
Yeah,x-terminal-emulator
isn't present here either, unfortunately.
– Ivan Molodetskikh
yesterday
Well, the only "official" way of doing it I found isxdg-terminal
but I can't find it installed on any of my distributions. You might need to duplicate what it does.
– cprn
yesterday
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
update-alternatives
seems to not be present on my system (Arch Linux).
– Ivan Molodetskikh
yesterday
Good catch, it's for Debian derivatives. Tryx-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
yesterday
Yeah,x-terminal-emulator
isn't present here either, unfortunately.
– Ivan Molodetskikh
yesterday
Well, the only "official" way of doing it I found isxdg-terminal
but I can't find it installed on any of my distributions. You might need to duplicate what it does.
– cprn
yesterday
add a comment |
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).
update-alternatives
seems to not be present on my system (Arch Linux).
– Ivan Molodetskikh
yesterday
Good catch, it's for Debian derivatives. Tryx-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
yesterday
Yeah,x-terminal-emulator
isn't present here either, unfortunately.
– Ivan Molodetskikh
yesterday
Well, the only "official" way of doing it I found isxdg-terminal
but I can't find it installed on any of my distributions. You might need to duplicate what it does.
– cprn
yesterday
add a comment |
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).
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).
edited yesterday
answered yesterday
cprncprn
3771715
3771715
update-alternatives
seems to not be present on my system (Arch Linux).
– Ivan Molodetskikh
yesterday
Good catch, it's for Debian derivatives. Tryx-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
yesterday
Yeah,x-terminal-emulator
isn't present here either, unfortunately.
– Ivan Molodetskikh
yesterday
Well, the only "official" way of doing it I found isxdg-terminal
but I can't find it installed on any of my distributions. You might need to duplicate what it does.
– cprn
yesterday
add a comment |
update-alternatives
seems to not be present on my system (Arch Linux).
– Ivan Molodetskikh
yesterday
Good catch, it's for Debian derivatives. Tryx-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
yesterday
Yeah,x-terminal-emulator
isn't present here either, unfortunately.
– Ivan Molodetskikh
yesterday
Well, the only "official" way of doing it I found isxdg-terminal
but I can't find it installed on any of my distributions. You might need to duplicate what it does.
– cprn
yesterday
update-alternatives
seems to not be present on my system (Arch Linux).– Ivan Molodetskikh
yesterday
update-alternatives
seems to not be present on my system (Arch Linux).– Ivan Molodetskikh
yesterday
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
yesterday
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
yesterday
Yeah,
x-terminal-emulator
isn't present here either, unfortunately.– Ivan Molodetskikh
yesterday
Yeah,
x-terminal-emulator
isn't present here either, unfortunately.– Ivan Molodetskikh
yesterday
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
yesterday
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
yesterday
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
-desktop, editors, mime-types
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♦
yesterday
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
yesterday
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♦
yesterday
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
yesterday
1
Good point, I added a paragraph about that.
– Ivan Molodetskikh
yesterday