如何在命令行中启动emacs,但如果服务器已经运行,则使用emacsclient?

3

我正在Ubuntu下使用emacs

如果我想编辑 a.txt,那么我可以点击emacs图标或者使用

$ emacs  --geometry 10x10 --fullscreen --no-splash ~/a.txt &

从bash中开始我的.emacs文件,启动emacs服务器。

如果我想要从命令行编辑另一个文件,我使用:

$ emacsclient -n ~/b.txt

将文件加载到现有的emacs中。

但我总是弄错,各种问题纷至沓来。

如何创建一个bash命令'e',检查emacs服务器是否已经运行,并执行相应的命令?

尝试使用emacsclient -a开关总会产生不良和不确定的行为。

如果在控制台上运行时可以'正确地执行',则额外得分。


1
你使用 emacsclient -a 时遇到了什么问题? - juanleon
1个回答

4
到目前为止,.bashrc 中的这个函数定义似乎是一个完美的解决方案:
function e #open emacs in background and disown it, or if already running use emacsclient
{
 echo "emacs function backgrounds and disowns emacs, or calls client if server already running; see .bashrc";
 local FLUFFY="$@";
 local SERVERSOCKET=/tmp/emacs${UID}/server ;
 echo "trying to open: " $FLUFFY 
 echo " checking: " $SERVERSOCKET "for emacs server " ;
 # test for existence of emacs server socket 
 if [ -e $SERVERSOCKET ]; then
     echo "using emacsclient"
     emacsclient -n $FLUFFY;
 else
     echo "starting emacs: make tea..."
     emacs  --geometry 10x10 --fullscreen --no-splash $FLUFFY & disown ;
 fi;
}

这个词源于以下咒语:

FLUFFY=~/b.txt ; if [ -e /tmp/emacs1000/server ]; then emacsclient -n $FLUFFY; else emacs  --geometry 10x10 --fullscreen --no-splash $FLUFFY & fi;

这个功能是通过检查用户1000的emacs服务器套接字是否存在来实现我想要的效果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接