1

Here are my personal aliases for editing root owned files:

# CLI superuser nano; compiled; version 2.8.0

function sunano {
    export SUDO_EDITOR='/usr/local/bin/nano'
    sudoedit "$@"
}


# GUI superuser xed; packaged; version 1.2.2

function suxed {
    export SUDO_EDITOR='/usr/bin/xed'
    sudoedit "$@"
}


# GUI superuser sublime-text; packaged; version 3126

function susubl {
    export SUDO_EDITOR='/opt/sublime_text/sublime_text -w'
    sudoedit "$@"
}

Let me take it from end:

  1. Sublime Text works great now thanks to Stephen Kitt's advice.

  2. Xed seems to work good too, it shows that the privileges are elevated, which I personally don't like to be reminded of, but there seems to be no problem with it, colors are there and it didn't even need some wait switch like Sublime.

  3. The problem I have is with Nano as follows:

    If I invoke it as I was used to, e.g.:

    sudo nano /etc/nanorc
    

    The colors are there.

    But if I call it with the new alias:

    sunano /etc/nanorc
    

    There are no colors whatsoever.

    The configuration seems to have been read though, because it looks the same as I've configured it.

EDIT1: Apparently this issue affects at minimum the config file:

-rw-r--r-- 1 root root 8.6K Apr  8 02:30 /etc/nanorc

Other files, e.g. Bash or C++ are colored, I'm confused.

2 Answers 2

1

The issue here is that sudoedit copies the file to a temporary file before opening it in the editor. When the file has an extension, the temporary file is created with the same extension, and filename-based syntax highlighting modes are selected appropriately (e.g. for C files). When the file doesn’t have an extension, as is the case with nanorc, it is created with a random extension; this confuses filename-based syntax highlighting mode selection, and nano ends up treating the file as a standard text file.

If you can reconfigure nano to treat any nanorc* file as a configuration file, you’ll be able to restore the behaviour you’re after. Otherwise I’m not sure there’s a way to handle this automatically.

0
0

try

function sunano {
    export SUDO_EDITOR='/usr/local/bin/nano'
    sudoedit -E "$@"
    #        ^^ 
    #         add this
}

-E or --preserve-env passes your environment variables to the new process

0

You must log in to answer this question.

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