是否有一些模块或命令可以让我将当前区域发送给Shell?我希望有类似Python-mode的“python-send-region”,它将选定的区域发送到当前运行的Python shell。
答案 0 :(得分:40)
好的,写了一点简单。可能会花一些时间来写一个完整的次要模式。
暂时,以下功能将发送当前行(如果标记处于活动状态,则发送区域)。对我来说做得很好:
(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)
享受。
答案 1 :(得分:11)
(defun shell-region (start end)
"execute region in an inferior shell"
(interactive "r")
(shell-command (buffer-substring-no-properties start end)))
答案 2 :(得分:8)
我编写了一个包,用于将代码行或代码区域发送到shell进程,基本上类似于ESS用于R的代码。它还允许存在多个shell进程,并允许您选择将哪个进程发送到。
答案 3 :(得分:7)
M-x append-to-buffer
RET
答案 4 :(得分:6)
M-x shell-command-on-region
又名。
M- |
答案 5 :(得分:2)
修改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
答案 6 :(得分:1)
您是希望命令自动执行,还是只是准备好进入命令行?
Mx append-to-buffer
RET 会将所选文本输入到指定的缓冲区中,但shell不会执行该命令。
它的包装函数可以自动为缓冲区选择*shell*
(或者根据shell模式中的当前缓冲区更智能地选择/提示),然后调用append-to-buffer。
您可以轻松录制键盘宏来复制区域,切换到*shell*
,然后输入(如果需要)。
F3 的Mw CX B'/ KBD> *shell*
RET 赛扬 RET F4
C-X C-K 名词 my-execute-region-in-shell
RET
M-X insert-kbd-macro
RET my-execute-region-in-shell
RET
(global-set-key (kbd "C-c e") 'my-execute-region-in-shell)
答案 7 :(得分:1)
更新
截至2020年中,以上(出色而有用的)答案看起来有些不完整:sh-mode
具有将外壳区域发送到非交互式外壳的功能,其输出在名为sh-send-line-or-region-and-step
的微型缓冲区中。
或者:在窗口底部的模式栏中单击Shell-script
,然后依次单击Mouse-1
和Execute region
。输出将发送到迷你缓冲区和#<*Messages*>
。
如果微型缓冲区输出不够用,则有参考技术可以将输出重定向到其他缓冲区(不仅是外壳缓冲区,例如参见"How to redirect message/echo output to a buffer in Emacs?")。
您也可以使用C-c C-x
执行所有脚本。 Voilà。
答案 8 :(得分:0)
以下是来自this post的另一种解决方案。
为方便起见,只是复制它。打印声明是关键所在。
(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()"))
答案 9 :(得分:0)
我将接受的答案改成ansi-term / sane-term。
更改:
(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)