Getting nano nanorc settings to work with "sudo nano" command
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.
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
.
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.