2

My Emacs init file works fine in general. But, if I am in my home directory and if I execute:

pwd

    /home/pedro

sudo emacs /etc/nixos/hosts.nix

For some reason, this opens vanilla Emacs without my init file being loaded. In normal situations, opening a normal file and having vanilla Emacs only happens if I do emacs -q. For instance, emacs -q /home/pedro/Desktop/example.txt.

This gets even weirder for my naive eyes if I change my position on the file structure. After tweaking the position, if I try to open the same file, then Emacs works as expected being loaded with the init file. To illustrate, the following works as expected:

pwd

    /home/pedro

cd /etc/

emacs nixos/hosts.nix

Since this problem might be related to my OS, I must say that I am using NixOS. This is my configuration file.

This does not seem to be the same situation as this previous question. On the previous question, the person was unable to do a sudo emacs. I can always do it, but in a specific case, my init file is not been loaded.

Why does this happen?

Is there some way to do sudo emacs /etc/host being on home and with my init file being loaded?

2
  • 2
    When you open emacs as a super user, it tries to get the init file of that user. Since there's none, it starts vannila emacs. I suggest opening emacs as a normal user and opening a buffer with super user privileges, by using tramp.
    – aadcg
    May 13 at 15:10
  • 2
    Does this answer your question? "sudo emacs" Doesn't Work
    – Tyler
    May 13 at 17:31

2 Answers 2

8

sudo starts a session where the user is root, not you. It is looking for an init file under the home directory of root (usually /root on GNU Linux). It looks in the usual places (/root/.emacs, /root/.emacs.d/init.el etc) and if it doesn't find any, it will start a vanilla emacs session (as @aadcg points out in a comment).

Try

sudo emacs --user pedro ...

to run emacs with an arbitrary user's init file. See the man page of emacs with M-x man RET emacs for all the options.

Depending on how you set up your load-path however, you might have problems: e.g. I use $HOME in my load-path to pick up several libraries, but that expands to /root in the sudo session, so those libraries are not found. One way is to symlink /root/libdir to /home/pedro/libdir in such cases, one symlink per library directory. Another way is to reorganize your init file to not use $HOME. A third way is to provide a /root/.emacs that sets some things up and then loads your init file with (load "/home/pedro/.emacs.d/init.el") or similar - the setting up I mention would provide impedance matching between the root environment and whatever your init file requires.


In general however, it's not a particularly good idea to be running emacs (or a shell) as root: too many opportunities to really damage your system. sudo should be used for targeted operations that have to be done as root.

@aadcg in another answer provides a function that uses tramp to create a "sudo-ed" buffer for the same file. I use tramp directly to do the same thing without the function, but it amounts to the same thing.

I sometimes don't bother with tramp: to edit a file as root, I do sudo ed <file>, running the ancient line editor as root under a command shell within emacs no less, causing untold amounts of mirth for anyone watching :-)

2

@NickD solves the issue from one perspective. There's another way to go about it.

Open emacs without sudo and, whenever you need root privileges to edit a file, use the following command:

(defun my-sudo ()
    "Use Tramp to sudo the current buffer."
    (interactive)
    (when buffer-file-name
      (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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