Editing Files as Root

How many times have you brought up a file to edit and discovered that you don’t have write permissions? Usually they’re system files but sometimes you’re tying to help a colleague and don’t have the proper permissions. You can always kill the buffer and start over but that’s sort of a pain.

Bozhidar Batsov over at Emacs Redux has a nice solution; actually 3 nice solutions. He provides the sudo-edit function that reopens the file as root. All you have to do is call it and the problem is solved. Batsov suggests binding it to 【Ctrl+x Ctrl+r】 which has a nice symmetry with 【Ctrl+x Ctrl+f】 so it’s easy to remember but, of course, you can use whatever makes sense in your environment.

After using that setup for a few years, Batsov decided that even calling sudo-edit was inconvenient so he advised ido-find-file (or find-file if you’re not using ido) to check the permissions and automatically reopen the file as root if required. That’s nice because you just open the file as usual and Emacs automatically does the right thing.

He also has a solution for emacsclient that I’ll let you explore on your own. Like the others, it’s a nice solution.

Batsov’s post was from a couple of years ago but I somehow missed it. It is most definitely worth your time if you ever have to edit files you don’t have write permissions to.

This entry was posted in General and tagged . Bookmark the permalink.