I love that I can edit files on remote hosts right from the comfort of my local Emacs session. Even better, you can edit those same files as root, given the proper configuration.
Unfortunately, the interface for this is a bit fiddly. I find myself taking one of two approaches:
C-x C-v and edit the filename to use /sudo:root@….C-x C-f, edit the directory to use /sudo:root@…, then open the file I want.Since I didn’t like either of these, I wrote two functions to assist:
find-file-as-root behaves as find-file, except the default shown is changed to be /sudo:root@…toggle-alternate-file-as-root is similar to toggle-read-only (C-x C-q), in that it will swap back and forth between the unprivileged version of a file and the as-root version.I bind these to C-c C-x C-q and C-c x f, respectively.
There are two caveats. The first is that you must have tramp-default-proxies-alist configured for the sudo method to work on remote hosts. The second is that it is impossible to know for sure what the previous file name was after invoking toggle-alternate-file-as-root. Any buffer-local values are lost after replacing the buffer with the sudo version, and if I used a persistent structure outside of the buffer, it would require more complexity to prevent it from going stale. So instead, it just make a best effort to come up with something reasonable. Edge cases are going to fail, such as when you are editing a file as another non-root user. Patches are welcomed.
Discussion