4

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.

7 Answers 7

7

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.

3

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.

2

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.

1

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
0

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.

0

You can easily run emacs with the init file of your choice. For example, the following command

sudo emacs --load ~/.emacsroot -nw filename.ex

Loads emacs

  • as super user -> sudo
  • with .emacsroot init file located in the home directory -> --load ~/.emacsroot
  • in console mode -> -nw
  • opening filename.ex file
0

The following works for me:

sudo emacs /etc/host.conf --directory=/path/to/.emacs.d/

Simplified by putting this in .bashrc:

function semacs { sudo emacs --directory=/path/to/.emacs.d/ $*; }

...so all I enter on the command line is:

semacs /etc/host.conf

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .