lostwebsite.net blog

Annex to the Lost Website

Superuser-edit in Emacs

with one comment

I’m a big fan of GNU Emacs. It’s a very good programming editor but tends to be much more then that once you start digging into its feature and extensions people have spent a lot of time to write.

This makes Emacs quite unique in the world of generic text editor. Emacs users very often have their editor fired and ready to go. It might be because they always have a file to edit; programmers do that. It might also be because they use some Emacs extensions to fill-in for other programs like their IRC or mail client, like I do.

For me Emacs is always on. I can use it for most of my editing tasks by using the remote controller gnuclient.

What I could not use it for, and which annoyed me, was that I couldn’t edit files owned by root inside Emacs opened as my current use.

Luckily, somebody on Planet Emacsen lead me to discover you can use TRAMP to edit files using the ‘sudo’ method. TRAMP enables you to access remote files from inside Emacs, like on an FTP or SFTP server, for
instance. The ‘sudo’ access method, in that case, is just a twist of the concept, allowing you to edit files as another user on your own system.

TRAMP comes standard with Emacs 22. I personnaly use Emacs 23.something on all the computers I use, so I have not personally tested it on Emacs 22 but I have the feeling it should work fine.

To open a file as root, for example, do:

M-x find-file, /sudo::/etc/motd

And, TA-DA, it might work out of the box like that!

Yes, might. On my systems, I prefer to use ZSH. The prompt I use, and a ZSH-specific feature, conflict with TRAMP default configuration. TRAMP loads a shell with sudo in background and needs to see the shell prompt to make sure its able to send editing commands. So to be able to use TRAMP, I had to change the prompt for Emacs, or make TRAMP recognize my active prompt.

Changing the prompt is easier than it look, and can be made into a special-case for Emacs. TRAMP set the TERM environment variable to ‘dumb’ when it loads the shell. Since the dumb terminal mode is not
something I use or want to use, I just did something like this in root’s .zshrc:

if [ $TERM = "dumb" ]; then
   ...
   unsetopt zle
   PS1='$ '
fi

‘$’ being the cannonical UNIX shell prompt. The unsetopt zle disables the feature that might conflict with ZSH.

Written by fdgonthier

August 26, 2008 at 2:52 am

Posted in Emacs, Tips and Tricks

Tagged with , ,

One Response

Subscribe to comments with RSS.

  1. [...] http://lostwebsite.wordpress.com/2008/08/26/superuser-edit-in-emacs/ Kommentera GillaBli först att gilla denna post.Kommentera [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.