I use the following alias:

alias e='emacsclient -a '\'''\'' -t
alias sudo='sudo '

That way I can execute both, commands and aliases, with sudo, including e, which launchs the emacs server if it is not launched yet.

The thing is that, if I have the emacs-daemon running, for example because I have edited a file before, and then I open the editor with sudo, every file which is written by the server, like .historian, changes its owner to root. So, when trying to launch the server again, the server is launched but the client doesn't show up (it frozes just after launching the server) and I have to open the client in another terminal, but emacs doesn't behave correctly anyway. For example, the minibuffer is incorrectly displayed for commands that requires input, like C-x C-f.

How can I solve that situation? How could I launch an emacs server per user basis? Or there's any other solution?e

Provisionally, I have end up with the following function bound to C-x C-F:

(defun reopen-with-sudo ()
   (interactive)
   (if buffer-file-name
       (counsel-find-file
          (concat "/sudo:root@localhost:" buffer-file-name))
     (counsel-find-file (concat "/sudo:root@localhost:"
                           (ido-real-file-name "File:")))))

which solves the problem without any apparent downside, except that the file will be opened twice in two different buffers.

This is the solution I use to edit files as root using a single running emacs daemon:

export EDITOR="emacsclient --tty --create-frame"
alias e="$EDITOR"
alias E=sudoedit

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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