Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Emacs loads my .emacs.d/init.el startup file just fine when run as a regular user, but refuses to load it when run as sudo emacs file.type. I have tried changing the owner of .emacs.d to root with no success.

share|improve this question

5 Answers 5

I'm a little of topic, but you could use Emacs as a normal user, and tramp to open file as root:

C-xC-f /sudo::/path/to/file

will ask you for your sudo password, and open the file as root.

share|improve this answer

When you run as sudo add -s to preserve your environment. It's not maintaining your environment, thus your HOME directory is lost. Emacs looks in ~/.emacs, ~/.emacs.el, or ~/.emacs.d/init.el, so if you lose HOME, you lose the pathing to your startup files.

share|improve this answer

sudo -s didn't work for me. Here is the work-around I used.

Load emacs as a normal user (so your settings take effect)

  • Type M-x customize-themes
  • Use the link to migrate your settings to a theme.
  • Give your theme a name. (I unclicked the Remove saved theme settings from Custom save file. Don't know if that affected it)
  • Click save theme and note where the theme is saved.
  • Exit emacs and copy your theme file to your default emacs theme folder (e.g. ./usr/share/emacs/24.4/etc/themes/)
  • Now, open emac with sudo.
  • Type M-x customize-themes again and you should see your newly created theme as selectable.
  • Click Save Theme Settings and your sudo session will now use your custom theme in the future.

I wish there was a more elegant way of fixing this but it gets the job done.

share|improve this answer

jmq is right. In alternative tricky solution. If you are the only one allowed to be sudoer or to login as root, you can add a symlink in /root/ to your .emacs.d directory. For example:

ls -l /root/.emacs
lrwxrwxrwx. 1 root root 18 May  5 11:14 /root/.emacs -> /home/<user>/.emacs
share|improve this answer

jmq has the right idea, the issue is because emacs looks in ~/ directories to find your .emacs file and your installed scripts.

His suggestion of sudo -s, if it works, is the simplest, but there's a good chance it won't work. The problem is really a matter of how your sudoers is setup. The sudoers is likely setup to reset your environment, which means it doesn't preserve the HOME directory for your user when it runs the sudo command. The possible options are presented here as the top rated response, and using -s can't work around most of the sudoers settings that will screw it up in the first place.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.