Why use vi/vim to edit root files when you can use Emacs to do it?

Emacs TRAMP mode allows you to easily access and edit remote files as if they were local. So you get the benefits of using all your local settings without the need of close your current instance or open a new one.

I don’t use TRAMP frequently because I don’t need to edit remote files anymore as I used to but it’s interesting to share that you can use TRAMP to edit root local files as long you have sudo permissions.

I already shared a post on how to Open remote files with emacs. The principle is the same but in this case I’ll share how to edit a file that has root permissions.

Steps:

  1. Open the file with the root permissions by pressing C-x C-f.
  2. Type C-x C-q to toggle read-only mode for the current buffer.
  3. When you finish editing the file, type C-x C-w to save the file.
  4. Delete the whole existing patch on minibuffer
  5. Type /sudo::/youfile.txt. Enter.

Note that the :: double colon is required.

  -rw-r--r--   1 root  wheel     895 Jun 20 16:41 myfile.txt

A gif worth more than 1000 words:

Editing root files with emacs.

You can actually use TRAMP to open a new dired buffer as root using using C-x D and then all the following buffers will be opened as sudo.

References:

Few posts related to subject:

Good luck!