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?

Levente
  • 4,607

2 Answers2

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.

Quasímodo
  • 2,104
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.