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.

  • 3
    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 '17 at 5:01
  • I edit /etc/nanorc. I don't edit /root/.nanorc even if it exists. – DK Bose Jun 20 '17 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 '17 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 '17 at 7:48
  • @Melebius, thanks for explaining. I didn't know that. – DK Bose Jun 20 '17 at 11:21

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.