Starting text editors from terminal as root
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
System: Linux Mint 18.1 64-bit Cinnamon.
Bash: Packaged version 4.3.46(1)-release
.
Objective: To define Bash aliases to launch various CLI and GUI text editors while opening a file in root mode from gnome-terminal
emulator.
Progress: For example, the following aliases work as expected:
For CLI:
alias sunano='sudo nano'
For GUI:
alias suxed='sudo xed'
They both open any file as root.
Problem: I have an issue with gksudo
in conjunction with sublime-text
:
alias susubl='gksudo /opt/sublime_text/sublime_text'
Sometimes it works. It just does not do anything most of the time.
How do I debug such a thing with inconsistent behavior. It does not output anything. No error message or similar.
editors sudoedit
add a comment |
System: Linux Mint 18.1 64-bit Cinnamon.
Bash: Packaged version 4.3.46(1)-release
.
Objective: To define Bash aliases to launch various CLI and GUI text editors while opening a file in root mode from gnome-terminal
emulator.
Progress: For example, the following aliases work as expected:
For CLI:
alias sunano='sudo nano'
For GUI:
alias suxed='sudo xed'
They both open any file as root.
Problem: I have an issue with gksudo
in conjunction with sublime-text
:
alias susubl='gksudo /opt/sublime_text/sublime_text'
Sometimes it works. It just does not do anything most of the time.
How do I debug such a thing with inconsistent behavior. It does not output anything. No error message or similar.
editors sudoedit
add a comment |
System: Linux Mint 18.1 64-bit Cinnamon.
Bash: Packaged version 4.3.46(1)-release
.
Objective: To define Bash aliases to launch various CLI and GUI text editors while opening a file in root mode from gnome-terminal
emulator.
Progress: For example, the following aliases work as expected:
For CLI:
alias sunano='sudo nano'
For GUI:
alias suxed='sudo xed'
They both open any file as root.
Problem: I have an issue with gksudo
in conjunction with sublime-text
:
alias susubl='gksudo /opt/sublime_text/sublime_text'
Sometimes it works. It just does not do anything most of the time.
How do I debug such a thing with inconsistent behavior. It does not output anything. No error message or similar.
editors sudoedit
System: Linux Mint 18.1 64-bit Cinnamon.
Bash: Packaged version 4.3.46(1)-release
.
Objective: To define Bash aliases to launch various CLI and GUI text editors while opening a file in root mode from gnome-terminal
emulator.
Progress: For example, the following aliases work as expected:
For CLI:
alias sunano='sudo nano'
For GUI:
alias suxed='sudo xed'
They both open any file as root.
Problem: I have an issue with gksudo
in conjunction with sublime-text
:
alias susubl='gksudo /opt/sublime_text/sublime_text'
Sometimes it works. It just does not do anything most of the time.
How do I debug such a thing with inconsistent behavior. It does not output anything. No error message or similar.
editors sudoedit
editors sudoedit
edited Oct 7 '18 at 12:34
Vlastimil
asked Apr 5 '17 at 12:22
VlastimilVlastimil
8,4621565146
8,4621565146
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You shouldn’t run an editor as root unless absolutely necessary, you should set sudoedit
up appropriately. Then you can do
SUDO_EDITOR="/opt/sublime_text/sublime_text -w" sudoedit yourfile
sudoedit
will check you’re allowed to do this, make a copy of the file that you can edit with changing ids, start your editor, and then, when the editor exits, copy the file back if it has been changed.
I’d suggest a function rather than an alias:
function susubl {
export SUDO_EDITOR="/opt/sublime_text/sublime_text -w"
sudoedit "$@"
}
although as Jeff Schaller pointed out, you can use env
to put this in an alias and avoid changing your shell’s environment:
alias susubl='env SUDO_EDITOR="/opt/sublime_text/sublime_text -w" sudoedit'
The -w
option ensures that the Sublime Text invocation waits until the files are closed before returning and letting sudoedit
copy the files back.
Typically you’d just pick a favourite editor and setSUDO_EDITOR
(orVISUAL
orEDITOR
) in your shell startup files, and then usesudoedit
directly. But that doesn’t solve the X v. terminal conundrum (unless you useemacsclient
).
– Stephen Kitt
Apr 5 '17 at 14:03
add a comment |
Expanding on Stephen Kitt's answer, just filling a gap really
Find out, on what path your editor is located, e.g.:
$ which nano
/usr/local/bin/nano
As you can see, I use compiled
nano
, not the packaged version; no matter this can change from system/config to other system/config.
The CLI text editors like
vi
ornano
do not seem to have the wait option, so for mynano
I can write a function like this:
sunano()
{
export SUDO_EDITOR='/usr/local/bin/nano'
sudoedit "$@"
}
On the contrary, the GUI text editors like Linux Mint's integrated
xed
, free programs like Visual Studio Code (code
), or paid programs like Sublime Text (subl
) all seem to have the wait option, and you have to use it in order to avoid the problem described in my question, you can use something similar to these functions:
suxed()
{
export SUDO_EDITOR='/usr/bin/xed -w'
sudoedit "$@"
}
sucode()
{
export SUDO_EDITOR='/usr/bin/code -w'
sudoedit "$@"
}
susubl()
{
export SUDO_EDITOR='/opt/sublime_text/sublime_text -w'
sudoedit "$@"
}
What the --wait
option effectively does is, that the editor will wait on the terminal until you close it, thus waiting on close of the editor, enabling further actions to be planned and done on editor close, in this instance to save the sudoedit
changes. Normally, it would just free the terminal, and you would get a new prompt.
Imagine another useful usage for this:
code /home/vlastimil/.bash_aliases -w && source /home/vlastimil/.bash_aliases
Hopefully, with this example, I make myself clearer.
This can be further generalized to:
# text editors as root; the proper way through sudoedit
sudoedit_internal()
{
editor_path=${1}
editor_wait_option=${2}
shift 2
export SUDO_EDITOR="${editor_path} ${editor_wait_option}"
sudoedit "${@}"
}
# proprietary, yet probably most popular - Sublime Text
susubl() { sudoedit_internal '/opt/sublime_text/sublime_text' '-w' "${@}"; }
# my personal editor of choice - Visual Studio Code
sucode() { sudoedit_internal '/usr/bin/code' '-w' "${@}"; }
# Linux Mint built-in editor - Xed
suxed() { sudoedit_internal '/usr/bin/xed' '-w' "${@}"; }
# my personal CLI editor of choice - Nano
sunano() { sudoedit_internal '/usr/local/bin/nano' '' "${@}"; }
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
});
}
});
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%2f356113%2fstarting-text-editors-from-terminal-as-root%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You shouldn’t run an editor as root unless absolutely necessary, you should set sudoedit
up appropriately. Then you can do
SUDO_EDITOR="/opt/sublime_text/sublime_text -w" sudoedit yourfile
sudoedit
will check you’re allowed to do this, make a copy of the file that you can edit with changing ids, start your editor, and then, when the editor exits, copy the file back if it has been changed.
I’d suggest a function rather than an alias:
function susubl {
export SUDO_EDITOR="/opt/sublime_text/sublime_text -w"
sudoedit "$@"
}
although as Jeff Schaller pointed out, you can use env
to put this in an alias and avoid changing your shell’s environment:
alias susubl='env SUDO_EDITOR="/opt/sublime_text/sublime_text -w" sudoedit'
The -w
option ensures that the Sublime Text invocation waits until the files are closed before returning and letting sudoedit
copy the files back.
Typically you’d just pick a favourite editor and setSUDO_EDITOR
(orVISUAL
orEDITOR
) in your shell startup files, and then usesudoedit
directly. But that doesn’t solve the X v. terminal conundrum (unless you useemacsclient
).
– Stephen Kitt
Apr 5 '17 at 14:03
add a comment |
You shouldn’t run an editor as root unless absolutely necessary, you should set sudoedit
up appropriately. Then you can do
SUDO_EDITOR="/opt/sublime_text/sublime_text -w" sudoedit yourfile
sudoedit
will check you’re allowed to do this, make a copy of the file that you can edit with changing ids, start your editor, and then, when the editor exits, copy the file back if it has been changed.
I’d suggest a function rather than an alias:
function susubl {
export SUDO_EDITOR="/opt/sublime_text/sublime_text -w"
sudoedit "$@"
}
although as Jeff Schaller pointed out, you can use env
to put this in an alias and avoid changing your shell’s environment:
alias susubl='env SUDO_EDITOR="/opt/sublime_text/sublime_text -w" sudoedit'
The -w
option ensures that the Sublime Text invocation waits until the files are closed before returning and letting sudoedit
copy the files back.
Typically you’d just pick a favourite editor and setSUDO_EDITOR
(orVISUAL
orEDITOR
) in your shell startup files, and then usesudoedit
directly. But that doesn’t solve the X v. terminal conundrum (unless you useemacsclient
).
– Stephen Kitt
Apr 5 '17 at 14:03
add a comment |
You shouldn’t run an editor as root unless absolutely necessary, you should set sudoedit
up appropriately. Then you can do
SUDO_EDITOR="/opt/sublime_text/sublime_text -w" sudoedit yourfile
sudoedit
will check you’re allowed to do this, make a copy of the file that you can edit with changing ids, start your editor, and then, when the editor exits, copy the file back if it has been changed.
I’d suggest a function rather than an alias:
function susubl {
export SUDO_EDITOR="/opt/sublime_text/sublime_text -w"
sudoedit "$@"
}
although as Jeff Schaller pointed out, you can use env
to put this in an alias and avoid changing your shell’s environment:
alias susubl='env SUDO_EDITOR="/opt/sublime_text/sublime_text -w" sudoedit'
The -w
option ensures that the Sublime Text invocation waits until the files are closed before returning and letting sudoedit
copy the files back.
You shouldn’t run an editor as root unless absolutely necessary, you should set sudoedit
up appropriately. Then you can do
SUDO_EDITOR="/opt/sublime_text/sublime_text -w" sudoedit yourfile
sudoedit
will check you’re allowed to do this, make a copy of the file that you can edit with changing ids, start your editor, and then, when the editor exits, copy the file back if it has been changed.
I’d suggest a function rather than an alias:
function susubl {
export SUDO_EDITOR="/opt/sublime_text/sublime_text -w"
sudoedit "$@"
}
although as Jeff Schaller pointed out, you can use env
to put this in an alias and avoid changing your shell’s environment:
alias susubl='env SUDO_EDITOR="/opt/sublime_text/sublime_text -w" sudoedit'
The -w
option ensures that the Sublime Text invocation waits until the files are closed before returning and letting sudoedit
copy the files back.
edited Oct 7 '18 at 12:17
Vlastimil
8,4621565146
8,4621565146
answered Apr 5 '17 at 12:46
Stephen KittStephen Kitt
179k25407485
179k25407485
Typically you’d just pick a favourite editor and setSUDO_EDITOR
(orVISUAL
orEDITOR
) in your shell startup files, and then usesudoedit
directly. But that doesn’t solve the X v. terminal conundrum (unless you useemacsclient
).
– Stephen Kitt
Apr 5 '17 at 14:03
add a comment |
Typically you’d just pick a favourite editor and setSUDO_EDITOR
(orVISUAL
orEDITOR
) in your shell startup files, and then usesudoedit
directly. But that doesn’t solve the X v. terminal conundrum (unless you useemacsclient
).
– Stephen Kitt
Apr 5 '17 at 14:03
Typically you’d just pick a favourite editor and set
SUDO_EDITOR
(or VISUAL
or EDITOR
) in your shell startup files, and then use sudoedit
directly. But that doesn’t solve the X v. terminal conundrum (unless you use emacsclient
).– Stephen Kitt
Apr 5 '17 at 14:03
Typically you’d just pick a favourite editor and set
SUDO_EDITOR
(or VISUAL
or EDITOR
) in your shell startup files, and then use sudoedit
directly. But that doesn’t solve the X v. terminal conundrum (unless you use emacsclient
).– Stephen Kitt
Apr 5 '17 at 14:03
add a comment |
Expanding on Stephen Kitt's answer, just filling a gap really
Find out, on what path your editor is located, e.g.:
$ which nano
/usr/local/bin/nano
As you can see, I use compiled
nano
, not the packaged version; no matter this can change from system/config to other system/config.
The CLI text editors like
vi
ornano
do not seem to have the wait option, so for mynano
I can write a function like this:
sunano()
{
export SUDO_EDITOR='/usr/local/bin/nano'
sudoedit "$@"
}
On the contrary, the GUI text editors like Linux Mint's integrated
xed
, free programs like Visual Studio Code (code
), or paid programs like Sublime Text (subl
) all seem to have the wait option, and you have to use it in order to avoid the problem described in my question, you can use something similar to these functions:
suxed()
{
export SUDO_EDITOR='/usr/bin/xed -w'
sudoedit "$@"
}
sucode()
{
export SUDO_EDITOR='/usr/bin/code -w'
sudoedit "$@"
}
susubl()
{
export SUDO_EDITOR='/opt/sublime_text/sublime_text -w'
sudoedit "$@"
}
What the --wait
option effectively does is, that the editor will wait on the terminal until you close it, thus waiting on close of the editor, enabling further actions to be planned and done on editor close, in this instance to save the sudoedit
changes. Normally, it would just free the terminal, and you would get a new prompt.
Imagine another useful usage for this:
code /home/vlastimil/.bash_aliases -w && source /home/vlastimil/.bash_aliases
Hopefully, with this example, I make myself clearer.
This can be further generalized to:
# text editors as root; the proper way through sudoedit
sudoedit_internal()
{
editor_path=${1}
editor_wait_option=${2}
shift 2
export SUDO_EDITOR="${editor_path} ${editor_wait_option}"
sudoedit "${@}"
}
# proprietary, yet probably most popular - Sublime Text
susubl() { sudoedit_internal '/opt/sublime_text/sublime_text' '-w' "${@}"; }
# my personal editor of choice - Visual Studio Code
sucode() { sudoedit_internal '/usr/bin/code' '-w' "${@}"; }
# Linux Mint built-in editor - Xed
suxed() { sudoedit_internal '/usr/bin/xed' '-w' "${@}"; }
# my personal CLI editor of choice - Nano
sunano() { sudoedit_internal '/usr/local/bin/nano' '' "${@}"; }
add a comment |
Expanding on Stephen Kitt's answer, just filling a gap really
Find out, on what path your editor is located, e.g.:
$ which nano
/usr/local/bin/nano
As you can see, I use compiled
nano
, not the packaged version; no matter this can change from system/config to other system/config.
The CLI text editors like
vi
ornano
do not seem to have the wait option, so for mynano
I can write a function like this:
sunano()
{
export SUDO_EDITOR='/usr/local/bin/nano'
sudoedit "$@"
}
On the contrary, the GUI text editors like Linux Mint's integrated
xed
, free programs like Visual Studio Code (code
), or paid programs like Sublime Text (subl
) all seem to have the wait option, and you have to use it in order to avoid the problem described in my question, you can use something similar to these functions:
suxed()
{
export SUDO_EDITOR='/usr/bin/xed -w'
sudoedit "$@"
}
sucode()
{
export SUDO_EDITOR='/usr/bin/code -w'
sudoedit "$@"
}
susubl()
{
export SUDO_EDITOR='/opt/sublime_text/sublime_text -w'
sudoedit "$@"
}
What the --wait
option effectively does is, that the editor will wait on the terminal until you close it, thus waiting on close of the editor, enabling further actions to be planned and done on editor close, in this instance to save the sudoedit
changes. Normally, it would just free the terminal, and you would get a new prompt.
Imagine another useful usage for this:
code /home/vlastimil/.bash_aliases -w && source /home/vlastimil/.bash_aliases
Hopefully, with this example, I make myself clearer.
This can be further generalized to:
# text editors as root; the proper way through sudoedit
sudoedit_internal()
{
editor_path=${1}
editor_wait_option=${2}
shift 2
export SUDO_EDITOR="${editor_path} ${editor_wait_option}"
sudoedit "${@}"
}
# proprietary, yet probably most popular - Sublime Text
susubl() { sudoedit_internal '/opt/sublime_text/sublime_text' '-w' "${@}"; }
# my personal editor of choice - Visual Studio Code
sucode() { sudoedit_internal '/usr/bin/code' '-w' "${@}"; }
# Linux Mint built-in editor - Xed
suxed() { sudoedit_internal '/usr/bin/xed' '-w' "${@}"; }
# my personal CLI editor of choice - Nano
sunano() { sudoedit_internal '/usr/local/bin/nano' '' "${@}"; }
add a comment |
Expanding on Stephen Kitt's answer, just filling a gap really
Find out, on what path your editor is located, e.g.:
$ which nano
/usr/local/bin/nano
As you can see, I use compiled
nano
, not the packaged version; no matter this can change from system/config to other system/config.
The CLI text editors like
vi
ornano
do not seem to have the wait option, so for mynano
I can write a function like this:
sunano()
{
export SUDO_EDITOR='/usr/local/bin/nano'
sudoedit "$@"
}
On the contrary, the GUI text editors like Linux Mint's integrated
xed
, free programs like Visual Studio Code (code
), or paid programs like Sublime Text (subl
) all seem to have the wait option, and you have to use it in order to avoid the problem described in my question, you can use something similar to these functions:
suxed()
{
export SUDO_EDITOR='/usr/bin/xed -w'
sudoedit "$@"
}
sucode()
{
export SUDO_EDITOR='/usr/bin/code -w'
sudoedit "$@"
}
susubl()
{
export SUDO_EDITOR='/opt/sublime_text/sublime_text -w'
sudoedit "$@"
}
What the --wait
option effectively does is, that the editor will wait on the terminal until you close it, thus waiting on close of the editor, enabling further actions to be planned and done on editor close, in this instance to save the sudoedit
changes. Normally, it would just free the terminal, and you would get a new prompt.
Imagine another useful usage for this:
code /home/vlastimil/.bash_aliases -w && source /home/vlastimil/.bash_aliases
Hopefully, with this example, I make myself clearer.
This can be further generalized to:
# text editors as root; the proper way through sudoedit
sudoedit_internal()
{
editor_path=${1}
editor_wait_option=${2}
shift 2
export SUDO_EDITOR="${editor_path} ${editor_wait_option}"
sudoedit "${@}"
}
# proprietary, yet probably most popular - Sublime Text
susubl() { sudoedit_internal '/opt/sublime_text/sublime_text' '-w' "${@}"; }
# my personal editor of choice - Visual Studio Code
sucode() { sudoedit_internal '/usr/bin/code' '-w' "${@}"; }
# Linux Mint built-in editor - Xed
suxed() { sudoedit_internal '/usr/bin/xed' '-w' "${@}"; }
# my personal CLI editor of choice - Nano
sunano() { sudoedit_internal '/usr/local/bin/nano' '' "${@}"; }
Expanding on Stephen Kitt's answer, just filling a gap really
Find out, on what path your editor is located, e.g.:
$ which nano
/usr/local/bin/nano
As you can see, I use compiled
nano
, not the packaged version; no matter this can change from system/config to other system/config.
The CLI text editors like
vi
ornano
do not seem to have the wait option, so for mynano
I can write a function like this:
sunano()
{
export SUDO_EDITOR='/usr/local/bin/nano'
sudoedit "$@"
}
On the contrary, the GUI text editors like Linux Mint's integrated
xed
, free programs like Visual Studio Code (code
), or paid programs like Sublime Text (subl
) all seem to have the wait option, and you have to use it in order to avoid the problem described in my question, you can use something similar to these functions:
suxed()
{
export SUDO_EDITOR='/usr/bin/xed -w'
sudoedit "$@"
}
sucode()
{
export SUDO_EDITOR='/usr/bin/code -w'
sudoedit "$@"
}
susubl()
{
export SUDO_EDITOR='/opt/sublime_text/sublime_text -w'
sudoedit "$@"
}
What the --wait
option effectively does is, that the editor will wait on the terminal until you close it, thus waiting on close of the editor, enabling further actions to be planned and done on editor close, in this instance to save the sudoedit
changes. Normally, it would just free the terminal, and you would get a new prompt.
Imagine another useful usage for this:
code /home/vlastimil/.bash_aliases -w && source /home/vlastimil/.bash_aliases
Hopefully, with this example, I make myself clearer.
This can be further generalized to:
# text editors as root; the proper way through sudoedit
sudoedit_internal()
{
editor_path=${1}
editor_wait_option=${2}
shift 2
export SUDO_EDITOR="${editor_path} ${editor_wait_option}"
sudoedit "${@}"
}
# proprietary, yet probably most popular - Sublime Text
susubl() { sudoedit_internal '/opt/sublime_text/sublime_text' '-w' "${@}"; }
# my personal editor of choice - Visual Studio Code
sucode() { sudoedit_internal '/usr/bin/code' '-w' "${@}"; }
# Linux Mint built-in editor - Xed
suxed() { sudoedit_internal '/usr/bin/xed' '-w' "${@}"; }
# my personal CLI editor of choice - Nano
sunano() { sudoedit_internal '/usr/local/bin/nano' '' "${@}"; }
edited 9 mins ago
answered Oct 7 '18 at 12:16
VlastimilVlastimil
8,4621565146
8,4621565146
add a comment |
add a comment |
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%2f356113%2fstarting-text-editors-from-terminal-as-root%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