Search code examples
phpemacssyntaxcolorssudo

Keep the user's configuration when running emacs in sudo mode


I have to run emacs in sudo mode, to edit some .html or .php files in my /var/www directory. When I run it in normal-user mode, there's no problem with the syntax, and the colors (I installed the php-mode.el extension). Unfortunately when I run it in sudo mode, I lose this configuration. Is there any way to get it back?


Solution

  • Unfortunately when I run it in sudo mode, I loose this configuration, which is sad.

    That's completely expected. When you run a command with sudo you're running it as a different user, usually root. In most cases the target user's configuration will be used.

    Is there any way to get it back ?

    I believe the best option here is to run Emacs normally and then edit the file as root using TRAMP. In this case I think prefixing the file with /sudo:: will do the trick, e.g. C-x C-f /sudo::/var/www/html/foo.php RET. Emacs will prompt you for your password, just like sudo would on the command line.

    TRAMP does a lot more than letting you edit certain files as root via sudo, and it is probably worth your time to browse its manual.