简体   繁体   English

将bash命令发送到emacs中的开放终端缓冲区

[英]Sending bash commands to an open terminal buffer in emacs

I've been trying to improve my emacs life lately, and one thing I've done is make use of projectile and perspective to organize my buffers sensibly. 最近,我一直在尝试改善emacs的生活,而我所做的一件事是利用projectileperspective合理地组织缓冲区。

As part of this I wrote an elisp function to open up (or return to) a named ansi-term buffer that is project-specific. 在此过程中,我编写了一个elisp函数来打开(或返回)特定于项目的命名ansi-term缓冲区。 This allows me to quickly drop into a bash terminal for the project I'm currently looking at. 这使我可以快速进入当前正在查看的项目的bash终端。

What I have been having trouble finding out after plumbing the interwebs is whether or not it is possible to send bash commands to an open ansi-term buffer from within emacs. 在遍历Interweb之后,我一直无法找到的问题是,是否有可能从emacs中将bash命令发送到开放的ansi-term缓冲区。 Specifically, I'm trying to make sure the ansi-term buffer cds to the correct project root directory when it's first opened. 具体来说,我正在尝试确保将ansi-term缓冲区cds首次打开时正确的项目根目录。 This requires grabbing context from the projectile package first, so it's not something I can plop into my .bashrc . 这需要首先从projectile包中获取上下文,因此我无法将其放入.bashrc

Ideally I would be able to write an elisp function that: 理想情况下,我将能够编写一个elisp函数:

1) selects an ansi-term buffer by name (since I can have one open with a unique name for every project) 1)按名称选择ansi-term缓冲区(因为我可以为每个项目打开一个具有唯一名称的缓冲区)

2) sends and executes a command in that buffer 2)在该缓冲区中发送并执行命令

Is there any way to do this? 有什么办法吗?

EDIT 编辑

Final solution for anyone who is interested: 有兴趣的人的最终解决方案:

(defun visit-project-term-buffer ()
  "Create or visit a terminal buffer."
  (interactive)
  (if (not (get-buffer (persp-ansi-buffer-name)))
  (progn
    (split-window-sensibly (selected-window))
    (other-window 1)
    (ansi-term (getenv "SHELL"))
    (rename-buffer (persp-ansi-buffer-name))
    (end-of-buffer)
    (insert (format "cd %s" (projectile-project-root)))
    (term-send-input))
(switch-to-buffer-other-window (persp-ansi-buffer-name))))

Does this work for you? 这对您有用吗? It switches to a buffer named *terminal* and runs echo hello : 它切换到名为*terminal*的缓冲区并运行echo hello

(defun my-echo ()
  (interactive)
  (switch-to-buffer "*terminal*")
  (end-of-buffer)
  (insert "echo hello")
  (term-send-input))

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

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