当命令尝试以外壳模式打开编辑器时,请打开Emacs缓冲区
我喜欢使用Emacs的shell模式,但是它有一些缺陷。其中之一是,当shell命令尝试调时,它不足以打开新的缓冲区。例如,将环境变量" VISUAL"设置为" vim",我从" svn propedit"获得以下内容:
$ svn propedit svn:externals . "svn-prop.tmp" 2L, 149C[1;1H ~ [4;1H~ [5;1H~ [6;1H~ [7;1H~ ...
(可能很难从表示中看出来,但这是一个可怕的,丑陋的混乱局面。)
将VISUAL
设置为" emacs -nw"
,我得到
$ svn propedit svn:externals . emacs: Terminal type "dumb" is not powerful enough to run Emacs. It lacks the ability to position the cursor. If that is not the actual type of terminal you have, use the Bourne shell command `TERM=... export TERM' (C-shell: `setenv TERM ...') to specify the correct type. It may be necessary to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.svn: system('emacs -nw svn-prop.tmp') returned 256
(它可以将" VISUAL"设置为" emacs",但只能在Emacs X窗口中使用,而不能在终端会话中使用。)
有没有办法让shell模式在这里做正确的事情,并代表命令行进程打开一个新的缓冲区?
解决方案
我们可以通过emacsclient
添加到Emacs会话。首先,使用以下命令启动emacs服务器:
M-x server-start
或者将(server-start)添加到.emacs中。然后,
export VISUAL=emacsclient
编辑。
笔记:
- " emacs"和" emacsclient"的版本必须一致。如果安装了多个Emacs版本,请确保调用与运行服务器的Emacs版本相对应的
emacsclient
版本。 - 如果我们在多个Emacs进程/帧中启动服务器(例如,由于((server-start)`在.emacs中),则将在最后一帧中创建缓冲区以启动服务器。
在emacs 23中有emacsclient,gnuserv和multi-tty,它们都对此有用。实际上,我认为在Emacs 23中,emacsclient具有gnuserv的所有有趣功能。
并非完全正确。 ansi-term可以运行emacs很好(尽管我通常为提交日志运行mg,但在极少数情况下,我不直接从emacs进行提交)。如果首先启动"屏幕"并在其中运行,则" eshell"也可以运行emacs。
除了使用emacs客户端/服务器外,我还使用此脚本来调用emacs。
如果emacs尚未运行,它将启动emacs,或者仅在正在运行的emacs中打开新的emacs缓冲区(使用gnuclient)。默认情况下,它在后台运行,但对于需要某些输入的进程,可以在前台运行。例如,当输入更改列表描述时,我将其用作源代码控制编辑器。我有" SVN_EDITOR = emacs sync",因此我可以在emacs shell中执行" svn commit",它将在同一emacs中的新emacs缓冲区中打开svn编辑器。当我关闭缓冲区时," svn commit"继续。很有用。
#!/bin/sh if [ -z $EMACS_CMD ]; then EMACS_CMD="/usr/bin/emacs" fi if [ -z $GNUCLIENT_CMD ]; then GNUCLIENT_CMD="/usr/bin/gnuclient" fi if [ "" = "sync" ]; then shift 1 sync=true else sync=false fi cmd="${EMACS_CMD} $*" lsof $EMACS_CMD | grep $USER >/dev/null 2>&1 if [ "$?" -ne "1" ]; then cmd="${GNUCLIENT_CMD} $*" fi if [ $sync = "true" ]; then $cmd else $cmd & fi
我想做类似的事情,通过Mercurial合并到emacs外壳中。感谢这里的海报,我找到了方向。两步:
- 在.emacs文件中添加:(启动服务器)(更改后请记住要加载文件)
- 在hgrc中:
[merge-tools] emacs.executable = emacsclient emacs.premerge = False emacs.args = --eval "(ediff-merge-with-ancestor \"$local\" \"$other\" \"$base\" nil \"$output\")"