简体   繁体   English

在 emacs ansi-term shell 中复制/粘贴

[英]Copy/Paste in emacs ansi-term shell

I have configured my emacs to run zsh shell within ansi-term.我已经将我的 emacs 配置为在 ansi-term 中运行 zsh shell。 However, copy/paste no longer works ie nothing is getting pasted from kill-ring to the terminal.但是,复制/粘贴不再有效,即没有任何内容从 kill-ring 粘贴到终端。 Changing the TERM to vt100, or eterm doesn't solve the problem.将 TERM 更改为 vt100 或 eterm 并不能解决问题。

Would appreciate any ideas or solution.将不胜感激任何想法或解决方案。

To provide context I have configured ansi-term as follows:为了提供上下文,我将 ansi-term 配置如下:

(global-set-key "\C-x\C-a" '(lambda ()(interactive)(ansi-term "/bin/zsh")))
(global-set-key "\C-x\ a" '(lambda ()(interactive)(ansi-term "/bin/zsh")))

You may want to simply switch between character mode and line mode while using the terminal.您可能只想在使用终端时在字符模式和行模式之间切换。 Cc Cj will run term-line-mode , which treats the terminal buffer more like a normal text-buffer in which you can move the cursor and yank text. Cc Cj将运行term-line-mode ,它将终端缓冲区更像是一个普通的文本缓冲区,您可以在其中移动光标和拉取文本。 You can switch back to character mode by running term-char-mode with Cc Ck .您可以通过使用Cc Ck运行term-char-mode来切换回字符模式。

As described in this lovely blog snippet , there's a function, term-paste , in term.el , that does exactly what you want.正如这个可爱的博客片段 中所述term.el有一个函数term-paste ,它完全符合您的要求。 By default it's bound only to S-insert but the blog's recommended Cc Cy seems like a good suggestion.默认情况下它只绑定到S-insert但博客推荐的Cc Cy似乎是一个很好的建议。

ansi-term , in char-mode , takes the ordinary bindings for the terminal emulation. ansi-term ,在char-mode ,采用终端仿真的普通绑定。 You need a new binding, plus a way to output to ansi-term correctly.您需要一个新的绑定,以及一种正确输出到ansi-term I use this:我用这个:

(defun ash-term-hooks ()
  ;; dabbrev-expand in term
  (define-key term-raw-escape-map "/"
    (lambda ()
      (interactive)
      (let ((beg (point)))
        (dabbrev-expand nil)
        (kill-region beg (point)))
      (term-send-raw-string (substring-no-properties (current-kill 0)))))
  ;; yank in term (bound to C-c C-y)
  (define-key term-raw-escape-map "\C-y"
    (lambda ()
       (interactive)
       (term-send-raw-string (current-kill 0)))))
  (add-hook 'term-mode-hook 'ash-term-hooks)

When you do this, Cc Cy will yank.执行此操作时,Cc Cy 将猛拉。 It only does one yank, though, and you can't cycle through your kill-buffer.但是,它只执行一次猛拉,并且您无法在终止缓冲区中循环。 It's possible to do this, but I haven't implemented it yet.这样做是可能的,但我还没有实现它。

The above solutions work well for copying text from some buffer to ansi-term, but they aren't able to copy text from ansi-term to another buffer (eg copy a command you just ran to a shell script you're editing).上述解决方案适用于将文本从某个缓冲区复制到 ansi-term,但它们无法将文本从 ansi-term 复制到另一个缓冲区(例如,将您刚刚运行的命令复制到您正在编辑的 shell 脚本)。 Adding this to my .emacs file solved that problem for me (in Emacs 24.4):将此添加到我的 .emacs 文件中为我解决了这个问题(在 Emacs 24.4 中):

(defun my-term-mode-hook ()
  (define-key term-raw-map (kbd "C-y") 'term-paste)
  (define-key term-raw-map (kbd "C-k")
    (lambda ()
      (interactive)
      (term-send-raw-string "\C-k")
      (kill-line))))
(add-hook 'term-mode-hook 'my-term-mode-hook)

Note that if you want to bind kill/yank to a keystroke that starts with the ansi-term escape characters (by default Cc and Cx), and want this to work in the unlikely event that those change, you can instead define your keystrokes (without the leading escape) to term-raw-escape-map , as is done in user347585's answer.请注意,如果您想将 kill/yank 绑定到以 ansi-term 转义字符(默认情况下为 Cc 和 Cx)开头的击键,并希望它在这些更改的不太可能发生的事件中起作用,您可以改为定义您的击键(没有领先的转义)到term-raw-escape-map ,就像在 user347585 的回答中所做的那样。

These other solutions don't work well for me, switching between character mode and line mode causes ansi-term to stop working properly randomly, and setting ansi-term's term-paste to Cc Cy (based on Glyph's link), didn't work the code snippet was for term, not ansi-term:这些其他解决方案对我来说效果不佳,在字符模式和行模式之间切换会导致 ansi-term 随机停止正常工作,并将 ansi-term 的 term-paste 设置为 Cc Cy(基于 Glyph 的链接),不起作用代码片段是针对术语的,而不是针对 ansi 术语的:

    (eval-after-load "ansi-term"
    '(define-key ansi-term-raw-map (kbd "C-c C-y") 'term-paste))

我启用了xterm-mouse-mode ,之后我可以使用鼠标选择文本并使用 Mac OS X 中 emacs GUI 中ansi-term中的标准 Mac 命令 C 按钮进行复制,

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

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