是否可以从 emacs 向 xterm 发送'cd'命令?



在Emacs中,我不喜欢shell-mode/eshell-mode,因为它们不能充分利用zsh而且它们很糟糕。

所以我希望使用xterm作为外部子流程。

(global-set-key (kbd "M-<f2>")
                (lambda () (interactive)
                  (start-process "XTerm" nil "xterm")))

现在 xterm 的 PWD 与 Emacs default-directory 同步,这个术语现在是一个完整的术语。但是有一个问题:我 sub-rountine 的启动时间总是令人失望。

所以我希望只启动 xterm 一次,在 Emacs 中,如果它发现有一个名为 XTerm 的子进程正在运行,1( 切换到它 2( 将 xterm 中运行的 shell 的 PWD 设置为 Emacs 的default-directory

可以这样做吗?

如果两者都不可能,那么有了tmux,我们能实现这一目标吗?

这是我的设置:

(defvar terminal-process)
(defun terminal ()
  "Switch to terminal. Launch if nonexistant."
  (interactive)
  (if (get-buffer "*terminal*")
      (switch-to-buffer "*terminal*")
    (term "/bin/bash"))
  (setq terminal-process (get-buffer-process "*terminal*")))
(global-set-key "C-t" 'terminal)

您能详细说明一下启动时间吗?我的大约是 0.3 秒。

UPD 我的dired自定义中的一小段

我的dired设置中有这个:

(add-hook
 'dired-mode-hook
 (lambda()
   (define-key dired-mode-map (kbd "`")
     (lambda()(interactive)
       (let ((current-dir (dired-current-directory)))
         (term-send-string
          (terminal)
          (format "cd %sn" current-dir)))))))

其中terminal是:

(defun terminal ()
  "Switch to terminal. Launch if nonexistant."
  (interactive)
  (if (get-buffer "*terminal*")
      (switch-to-buffer "*terminal*")
    (term "/bin/bash"))
  (setq terminal-process (get-buffer-process "*terminal*")))

这样做是它为与 dired 缓冲区相同的目录打开一个终端,重用现有*terminal*,或创建一个新(如果不存在(。

总结一下您问题的答案:

是的,这是可能的。它完成的:

(term-send-string
 (terminal)
 (format "cd %sn" default-directory))

如果 xterm 不是一个硬性要求,只是以某种方式从 emacs 启动 zsh,那么看看 AnsiTerm,或者我的偏好,MultiTerm。 它们在 emacs 中实现了一个终端模拟器(如 xterm(,因此您可以在缓冲区中运行任何终端应用程序(例如 zsh(。 我喜欢多术语,因为它有更好的默认值 IMO。

然后,您可以使用以下方法更改目录

(defun term-send-cd (&optional dir)
  (interactive "DDirectory: ")
  (let ((dir (if dir (expand-file-name dir) "")))
    (term-send-raw-string (format "cd '%s'n" dir))))

最新更新