7

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.

9
  • 4
    When using nano through the mechanism of sudo none of your personalized settings are applied because it is not you executing nano, rather it is the superuser. Try setting the desired setting in /root/.nanorc instead.
    – user649240
    Jun 20, 2017 at 5:01
  • I edit /etc/nanorc. I don't edit /root/.nanorc even if it exists.
    – DK Bose
    Jun 20, 2017 at 5:27
  • 4
    @DKBose /etc/nanorc affects all users of the computer. You should take it into account in a multi-user environment.
    – Melebius
    Jun 20, 2017 at 6:45
  • 1
    Your settings might be overriding using /root/.nanorc move it somewhere else to see what happens: sudo mv /root/.nanorc{,.bk}
    – Ravexina
    Jun 20, 2017 at 7:48
  • @Melebius, thanks for explaining. I didn't know that.
    – DK Bose
    Jun 20, 2017 at 11:21

2 Answers 2

7

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.

5

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.

3
  • Very flexible, except, you'd probably want to use a static path to the .nanorc file (like alias nano='nano --rcfile /home/myUserName/.nanorc') instead of using ~. That way it won't look in the root home directory for .nanorc. Sep 29, 2022 at 7:55
  • @Brōtsyorfuzthrāx -- as I explicitly wrote in the answer, if you use these lines your own (sudo-issuing) user's $HOME/.nanorc is used. This is because the tilde (~) is expanded first.
    – simon
    Nov 8, 2022 at 3:07
  • You're right. It is expanded first (I didn't know that). (But don't use sudo su before the command instead of sudo, without a static path). Nov 8, 2022 at 3:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .