Emacs外壳模式:如何将区域发送到外壳?

Emacs外壳模式:如何将区域发送到外壳?,emacs,Emacs,是否有一些模块或命令可以让我将当前区域发送到shell 我想要类似Python模式的Python发送区域,它将所选区域发送到当前运行的Python shell。M-x shell命令打开区域 阿卡 M-|您希望命令自动执行,还是只想在准备时输入命令行 M-xappend to bufferRET将在点处将所选文本输入指定的缓冲区,但shell不会执行该命令 它的包装函数可以自动为缓冲区选择*shell*(或者在shell模式下根据当前缓冲区更智能地选择/提示),然后调用append to buf

是否有一些模块或命令可以让我将当前区域发送到shell


我想要类似Python模式的
Python发送区域
,它将所选区域发送到当前运行的Python shell。

M-x shell命令打开区域

阿卡


M-|

您希望命令自动执行,还是只想在准备时输入命令行

M-x
append to buffer
RET将在点处将所选文本输入指定的缓冲区,但shell不会执行该命令

它的包装函数可以自动为缓冲区选择
*shell*
(或者在shell模式下根据当前缓冲区更智能地选择/提示),然后调用append to buffer

您可以简单地录制一个键盘宏来复制区域,切换到
*shell*
,然后拖动并输入(如果需要)

F3M wC xb
*外壳*
RETC-yRETF4
C-xC-kn
myexecuteregioninshell
RET
M-x
insert kbd宏
RET
myexecuteregion在shell中
RET

(全局设置键(kbd“C-C e”)'我在shell中的执行区域)
好的,写了一个简单的代码。可能会花一些时间来写一个完整的次要模式

目前,以下功能将发送当前行(或区域,如果标记处于活动状态)。对我来说做得很好:

(defun sh-send-line-or-region (&optional step)
  (interactive ())
  (let ((proc (get-process "shell"))
        pbuf min max command)
    (unless proc
      (let ((currbuff (current-buffer)))
        (shell)
        (switch-to-buffer currbuff)
        (setq proc (get-process "shell"))
        ))
    (setq pbuff (process-buffer proc))
    (if (use-region-p)
        (setq min (region-beginning)
              max (region-end))
      (setq min (point-at-bol)
            max (point-at-eol)))
    (setq command (concat (buffer-substring min max) "\n"))
    (with-current-buffer pbuff
      (goto-char (process-mark proc))
      (insert command)
      (move-marker (process-mark proc) (point))
      ) ;;pop-to-buffer does not work with save-current-buffer -- bug?
    (process-send-string  proc command)
    (display-buffer (process-buffer proc) t)
    (when step 
      (goto-char max)
      (next-line))
    ))

(defun sh-send-line-or-region-and-step ()
  (interactive)
  (sh-send-line-or-region t))
(defun sh-switch-to-process-buffer ()
  (interactive)
  (pop-to-buffer (process-buffer (get-process "shell")) t))

(define-key sh-mode-map [(control ?j)] 'sh-send-line-or-region-and-step)
(define-key sh-mode-map [(control ?c) (control ?z)] 'sh-switch-to-process-buffer)

享受。

M-x
append to buffer
RET

我编写了一个包,它将代码行或代码区域发送到shell进程,基本上类似于ESS用于R。它还允许存在多个shell进程,并允许您选择将区域发送到哪个进程


请看这里:

修改上面的Jurgens答案以操作特定的缓冲区,将提供以下函数,该函数将发送区域,然后切换到缓冲区,并在另一个窗口中显示,名为PYTHON的缓冲区用于说明。目标缓冲区应该已经在运行shell

(defun p-send(start end)
  (interactive "r") ;;Make the custom function interactive and operative on a region
  (append-to-buffer (get-buffer "*PYTHON*") start end) ;;append to the buffer named *PYTHON*
  (switch-to-buffer-other-window (get-buffer "*PYTHON*")) ;;switches to the buffer
  (execute-kbd-macro "\C-m")) ;;sends the enter keystroke to the shell

这里是来自的另一个解决方案

只是为了方便复制而已。打印语句是这里的关键

(add-hook 'python-mode-hook
      'my-python-send-statement)

 (defun my-python-send-statement ()
   (interactive)
   (local-set-key [C-return] 'my-python-send-statement)
   (end-of-line)
   (set-mark (line-beginning-position))
   (call-interactively 'python-shell-send-region)
   (python-shell-send-string "; print()"))

我将公认的答案改编为ansi术语/理智术语

变化:

  • 将“外壳”更改为“ansi术语”
  • (使用当前缓冲区pbuff…)表单不工作,因为行和字符模式在终端模式下工作。它将给您一个只读缓冲区错误。在插入修复之前,包装插入模式命令以打开切换线,但不需要,因为
  • 您可以单独使用进程send string表单,而术语output和游标位置反映了更改

  • 更新
    截至2020年年中,上述(精彩且有用)答案看起来有点不完整:
    sh模式
    具有将shell区域发送到非交互式shell的功能,输出在名为
    sh send line或region and step的小型缓冲区中

    或者:单击窗口底部模式栏中的
    Shell脚本
    ,然后单击
    Mouse-1
    ,然后单击
    executeregion
    。输出被发送到微型缓冲区和
    #

    如果minibuffer输出还不够,那么可以参考一些技术将输出重定向到其他缓冲区(不仅仅是shell缓冲区,请参见示例)。
    您还可以使用
    C-C-x
    执行所有脚本。瞧。

    它做了一些不同的事情——它提示输入shell命令并向该命令发送选择。我希望该区域直接发送到shell(即,如果我突出显示“ls”并发送它,它的作用与将“ls”粘贴到shell缓冲区相同),如果您只是将其通过管道发送到bash,或者任何您想要的shell,那么这就行了。有点像黑客,但它让你不用写任何额外的函数就可以做到。@WLPhoenix你能详细说明一下吗?好问题。伙计。我在找同样的东西,除了你的帖子,什么也找不到。很难相信,没有人从sh脚本向当前shell编写基本行和区域。我会搜索一下,如果找不到,我会写信给自己。脱帽致敬,谢谢。只有一件事,不确定是否真的是因为你的乐趣(我是emacs新手):输出不会自动向下滚动。@vemv,
    (setq comint在输出t上向下滚动)
    将解决你的问题。神奇!对于一个相对较老的问题,快速得到答案是最大的乐趣。这真是太棒了-允许我复制我最关心的Acme编辑器的功能。漂亮的软件包!
    (add-hook 'python-mode-hook
          'my-python-send-statement)
    
     (defun my-python-send-statement ()
       (interactive)
       (local-set-key [C-return] 'my-python-send-statement)
       (end-of-line)
       (set-mark (line-beginning-position))
       (call-interactively 'python-shell-send-region)
       (python-shell-send-string "; print()"))
    
    (defun ansi-term-send-line-or-region (&optional step)
      (interactive ())
      (let ((proc (get-process "*ansi-term*"))
             pbuf
             min
             max
             command)
        (unless proc
          (let ((currbuff (current-buffer)))
            (sane-term)
            (switch-to-buffer currbuff)
            (setq proc (get-process "*ansi-term*"))))
    
        (setq pbuff (process-buffer proc))
    
        (if (use-region-p)
          (setq min (region-beginning)
                max (region-end))
          (setq min (point-at-bol)
                max (point-at-eol)))
    
        (setq command (concat (buffer-substring min max) "\n"))
        (process-send-string proc command)
        (display-buffer (process-buffer proc) t)
        (when step
          (goto-char max)
          (next-line))))
    
    (defun sh-send-line-or-region-and-step ()
      (interactive)
      (sh-send-line-or-region t))
    (defun sh-switch-to-process-buffer ()
      (interactive)
      (pop-to-buffer (process-buffer (get-process "*ansi-term*")) t))
    
    (define-key sh-mode-map [(control ?j)] 'sh-send-line-or-region-and-step)
    (define-key sh-mode-map [(control ?c) (control ?z)] 'sh-switch-to-process-buffer)