Emacs 在 su 到 root 后加载用户配置文件

Emacs loading user configuration file after su to root

在我的 ArchLinux 数字计算器上,我有两个帐户:一个用户帐户 (benj) 和根帐户。

对于管理任务,我使用我的用户帐户通过 ssh 进入机器。打开 shell 后,我使用 su 切换到 root。

运行 emacs 现在会显示初始化过程中出错的错误

Warning (initialization): An error occurred while loading ‘/home/benj/.emacs.d/init.el’:

File is missing: Cannot open load file, No such file or directory, ~/.emacs.d/init-modules/emacs-lisp-package-archive.el

To ensure normal operation, you should investigate and remove the cause of the error in your initialization file.  Start Emacs with the ‘--debug-init’ option to view a complete error backtrace.

我的 ~benj/.emacs.d/init.el 是模块化的,而 ~root/.emacs.d/init.el 不是。我不明白为什么 emacs 试图加载我的用户的配置文件。

echo $HOME              # Shows /root
alias | grep emacs      # Shows nothing
which emacs             # /user/bin/emacs
emacs --version         # GNU Emacs 26.1
                        # Copyright (C) 2018 Free Software Foundation, Inc.
                        # GNU Emacs comes with ABSOLUTELY NO WARRANTY.
                        # You may redistribute copies of GNU Emacs
                        # under the terms of the GNU General Public License.
                        # For more information about these matters, see the file named COPYING.

但是,当在 TTY1 上登录 root 时,M-x describe-variableuser-init-file returns 上预期的 /root/.emacs.d/init.el

C-hig (emacs)Find Init 说:

How Emacs Finds Your Init File

Normally Emacs uses the environment variable ‘HOME’ (*note HOME: General Variables.) to find ‘.emacs’; that’s what ‘~’ means in a file name. If ‘.emacs’ is not found inside ‘~/’ (nor ‘.emacs.el’), Emacs looks for ‘~/.emacs.d/init.el’ (which, like ‘~/.emacs.el’, can be byte-compiled).

However, if you run Emacs from a shell started by ‘su’, Emacs tries to find your own ‘.emacs’, not that of the user you are currently pretending to be. The idea is that you should get your own editor customizations even if you are running as the super user.

More precisely, Emacs first determines which user’s init file to use. It gets your user name from the environment variables ‘LOGNAME’ and ‘USER’; if neither of those exists, it uses effective user-ID. If that user name matches the real user-ID, then Emacs uses ‘HOME’; otherwise, it looks up the home directory corresponding to that user name in the system’s data base of users.