简体   繁体   English

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

[英]Is it possible to send an 'cd' command to xterm from emacs?

In Emacs, I don't like shell-mode / eshell-mode since they cannot take full use of zsh and they suck much. 在Emacs中,我不喜欢shell-mode / eshell-mode因为它们不能充分利用zsh并且会占用很多资源。

So I hope to use xterm as the external subprocess. 因此,我希望将xterm用作外部子进程。

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

And now the PWD of xterm is synced with Emacs default-directory and the term is now a full-feathered one. 现在,xterm的PWD已与Emacs的default-directory同步,并且该术语现在已成为全功能default-directory But there is ONE problem: I the startup time of the sub-rountine is always disappointing. 但是有一个问题:子例程的启动时间总是令人失望。

So I hope starting xterm only once and when in Emacs, if it finds there is a subprocess called XTerm running, 1) switch to it 2)set the PWD of shell running in xterm to default-directory of Emacs. 因此,我希望仅在emacs中启动一次xterm,如果它发现有一个名为XTerm的子XTerm正在运行,则1)切换到它; 2)将xterm中运行的Shell的PWD设置为Emacs的default-directory

Is it possible to do so? 有可能这样做吗?

If neither is possible, then with tmux , can we achieve this goal? 如果两者都不可行,那么使用tmux可以实现这个目标吗?

Here's my setup: 这是我的设置:

(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)

Could you elaborate more on the start-up time? 您能详细介绍一下启动时间吗? Mine is around 0.3s. 我的大约是0.3秒。

UPD A small snippet from my dired customization UPD我dired定制中的一小段

I've got this in my dired setup: 我在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 %s\n" current-dir)))))))

where terminal is: 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*")))

What this does is it opens a terminal for the same directory as dired buffer, reusing the existing *terminal* , or creating a new one if it's absent. 它的作用是为与dired缓冲区相同的目录打开一个终端,重用现有的*terminal* ,如果不存在则创建一个新*terminal*

To summarize the answer to your question: 总结问题的答案:

Yes, it's possible. 是的,有可能。 It's done with: 它完成了:

(term-send-string
 (terminal)
 (format "cd %s\n" default-directory))

If xterm is not a hard requirement, only that you somehow launch zsh from emacs, then take a look at AnsiTerm , or my preference, MultiTerm . 如果对xterm的要求不是很高 ,则只需以某种方式从emacs启动zsh,然后看看AnsiTerm或我的偏好MultiTerm They implement a terminal emulator (like xterm) in emacs, so you can run whatever terminal application (eg zsh) in a buffer. 它们在emacs中实现了终端仿真器(如xterm),因此您可以在缓冲区中运行任何终端应用程序(例如zsh)。 I like MultiTerm because it has better defaults IMO. 我喜欢MultiTerm,因为它具有更好的默认IMO。

Then you can change directories with 然后您可以使用

(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))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM