Getting nano nanorc settings to work with "sudo nano" command
Solution 1
When using an interactive shell, and then using sudo
to execute a command, such as nano
, the current user's settings in application specific configuration files are not sourced. So, in this case, ~/.nanorc
is not sourced, and the settings are not read.
The same condition occurs when the user uses su
to switch to another user, including root
. In both cases the new, or effective, user's settings are loaded instead. So using sudo nano
, or using su
to become root
and then executing nano
, will source the /root/.nanorc
for user settings. The settings in the global configuration file, such as /etc/nanorc
, affect all users on the system and should only be used to set global settings, which the user's file may override anyway.
To cause the effect you are looking for you need to apply the specialized setting to /root/.nanorc
. Then when you sudo nano
the settings for root
will be applied rather than the settings found in ~/.nanorc
.
Solution 2
You can do this with, e.g.:
sudo nano --rcfile ~/.nanorc <file-to-edit>
To make this more convenient, you can do:
alias sudo='sudo '
alias nano='nano --rcfile ~/.nanorc'
The first alias (for sudo
) is a standard technique to allow sudo
to expand users' aliases.
With these lines, you can use sudo nano <file-to-edit>
as normal and your $HOME/.nanorc
will be read.
Related videos on Youtube
nilblank
Updated on September 18, 2022Comments
-
nilblank over 1 year
Anyone know how to get nanorc settings (syntax highlighting, mouse) to work when using the "sudo nano" instance of the editor?
It works as expected when not using nano as a super-user but not with sudo in the mix.
I have all the desired settings in ~/.nanorc, as well as /etc/nanorc, but when editing a file with sudo, none of the personalized settings are applied.
-
Admin almost 7 yearsWhen using
nano
through the mechanism ofsudo
none of your personalized settings are applied because it is not you executingnano
, rather it is the superuser. Try setting the desired setting in/root/.nanorc
instead. -
DK Bose almost 7 yearsI edit
/etc/nanorc
. I don't edit/root/.nanorc
even if it exists. -
Melebius almost 7 years@DKBose
/etc/nanorc
affects all users of the computer. You should take it into account in a multi-user environment. -
Ravexina almost 7 yearsYour settings might be overriding using
/root/.nanorc
move it somewhere else to see what happens:sudo mv /root/.nanorc{,.bk}
-
DK Bose almost 7 years@Melebius, thanks for explaining. I didn't know that.
-
nilblank almost 7 years@GypsySpellweaver, Thank you. That sorted it. I simply did the following
sudo cp ~/.nanorc /root/.nanorc
. Seems to have done the trick. Incidentally, @DK Bose, I had edited/etc/nanorc
with the desired settings but it still failed to use them in the sudo instance. Only having a/root/
specific.nanorc
seems to work — for me, in any case. -
Admin almost 7 yearsGlad to help. Do you want an answer for this?
-
nilblank almost 7 years@GypsySpellweaver. Absolutely. Do you want to do the honors? Or would you prefer I recap it?
-
Admin almost 7 yearsI'll take a go at it.
-