5

I have the habit of running Midnight Commander as my normal user (so that I cannot cause too costly accidents).

Sometimes however I find that editing a configuration file in the system partition becomes desirable.

Neither with nano, nor with the internal mc editor can I save the file, since I don't have sudo privileges.

It would be nice if I could just sudo edit a file, without having to close / restart mc.

Is it possible? How?

2
  • 4
    You can drop into a shell from mc by pressing cntrl-o and then do sudo nano filename, then back to mc by cntrl-o after you edit. Is that good enough? Jan 9, 2021 at 16:09
  • In all honesty, I was hoping for something more comfy ;) Nevertheless, this works. And what is nice is that I'm already in the corresponding directory, so no need to cd. Pretty close.
    – Levente
    Jan 9, 2021 at 16:16

2 Answers 2

6

F9 to bring up the top menu > Command > Edit menu file > User. The menu file will open. Add

+ t r
e       Edit file with elevated priviledges
        xterm -e 'sudo vim %f' &

to it, separated by at least one blank line from the other code blocks already present in the file. Save and close the file. Now when you want to edit a file with priviledges, just press F2 (brings up the user menu) and then e to select this entry.


+ t r guarantees that that entry only shows up for regular files (not for directories, sockets, etc.). See man mc, section Edit Menu File for more.

xterm -e 'sudo vim %f' & is the executed command. It launches a new terminal in the background with the editor — here Vim, choose the one you like best —, so that Midnight Commander does not block. The downside is that the password will be requested each time, no matter how small the interval between the sudos is. If you don't want that:

  • Option 1: Simply use sudo vim %f instead, but that will take MC away from you until you close the editor.

  • Option 2: Make a user entry similar to the one shown above, but one that launches a new MC with root permissions with sudo mc %d %D & (%d %D to make the new MC open in the directories that you are!). You can use a different color scheme for root so you remember to be cautious.

I really like option 2 because it is very convenient to move files around in protected directories such as /usr and /etc.

5
  • Apparently I do not have xterm installed, so I instead did gnome-terminal -e ... I guess it's a similar experience as it would have been with xterm. Opens a new terminal window asking for my password, then the editor. When exiting the editor, this new terminal window closes automatically.
    – Levente
    Jan 9, 2021 at 16:52
  • I like this, quite fancy; the downside however is that each time I edit, even if edits are just a few seconds apart, I need to re-type my password.
    – Levente
    Jan 9, 2021 at 17:01
  • 1
    I have just re-read the last sentence, and turns out it's exactly what I was looking for. Leaving away the extra terminal instance, and having just sudo nano %f in the user menu keeps the same window as mc, so the sudo password is remembered (for a while, as configured), and also no need to resize/adjust the window. Very accepted :)
    – Levente
    Jan 9, 2021 at 19:39
  • 1
    @Levente Glad to hear it was helpful! Note that gnome-terminal has deprecated -e, they recommend -- instead now. I have also expanded a bit the answer with an option 2.
    – Quasímodo
    Jan 9, 2021 at 22:23
  • 1
    sudoedit? Or use $EDITOR? I like vim too (obviously), but there are surely easy ways to have this always do the right thing Jan 10, 2021 at 2:54
4

This method does not give direct mc functionality for the desired action, but you can drop into a shell from mc by pressing cntrl-o and then do sudo nano [filename], then back to mc by cntrl-o after you are done editing.

2
  • 1
    The benefit I see with this one is that the sudo password is remembered for subsequent edits, for however long it is configured system wide; + the editor interface will "inherit" its dimensions and position from my mc window (no need to resize/adjust) (since it remains in the originally used window).
    – Levente
    Jan 9, 2021 at 18:08
  • 1
    If the user has the command prompt set at the bottom line (as in Options > Layout), ctrl-o can be skipped. And I am almost sure MC has a keystroke to put the selected file in the command-line in that case, but I don't remember what it is ):
    – Quasímodo
    Jan 9, 2021 at 22:28

You must log in to answer this question.

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