如何仅在需要时运行 emacs 守护进程?

hug*_*omg 7 emacs

从我读到的内容来看,加速 emacs 启动的一种方法是emacs --daemon在登录时运行,然后使用emacslient而不是打开文件emacs,这将访问正在运行的 emacs 服务器而不是创建新的 emacs 实例。

但是,除非绝对必要,否则我宁愿不要将 proframs 放在我的自动启动中,作为加快登录过程的一种方式。是否有一种可靠的方法来检测 emacs 服务器是否正在运行?这将让我编写一个简单的脚本,当我第一次用 emacs 打开文件时,它会生成 emacs 服务器。

#!/bin/sh
if emacs_daemon_is_not_running # <-- How do I do this?
then
    emacs --daemon
fi
emacsclient -c "$@"
Run Code Online (Sandbox Code Playgroud)

小智 12

您甚至不需要测试 emacs 是否已经在运行。emacsclient如果 emacs 守护进程尚未运行,则可以启动它。来自emacsclient(1)

   -a, --alternate-editor=EDITOR
          if the Emacs server is not running,  run  the  specified  editor
          instead.   This can also be specified via the `ALTERNATE_EDITOR'
          environment variable.  If the  value  of  EDITOR  is  the  empty
          string,  run `emacs --daemon' to start Emacs in daemon mode, and
          try to connect to it.
Run Code Online (Sandbox Code Playgroud)

我使用别名 ,ge来编辑文件,定义如下:

alias ge="emacsclient -c -n --alternate-editor=\"\""
Run Code Online (Sandbox Code Playgroud)


meu*_*euh 5

您可以使用emacsclient自身来测试是否有连接:

#!/bin/sh
if ! emacsclient -e 0 >&/dev/null
then emacs --daemon
fi
emacsclient -c "$@"
Run Code Online (Sandbox Code Playgroud)

-e 0 表示计算表达式“0”,它只打印 0。如果 emacsclient 无法连接到服务器,则返回代码为非零。