2011-06-08 56 views
29

有一些模塊或命令可以讓我將當前區域發送給Shell嗎?我想要有類似Python模式的「python-send-region」,它將選定的區域發送到當前運行的Python shell。Emacs Shell模式:如何發送區域到shell?

+0

好問題。夥伴。我正在尋找同樣的東西,除了你的帖子以外找不到任何東西。很難相信沒有人從sh腳本中將基本行和區域發送者寫入當前shell。將搜索一下,並會寫我自己,如果不會找到。 – VitoshKa 2011-08-13 18:50:58

回答

32

確定,寫一個簡單位。可能會花一些時間來寫一個完整的小模式。

暫時下面的函數將發送當前行(或區域,如果標記處於活動狀態)。對我來說做得不錯:

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

享受。

+0

帽子,謝謝很多。只是一件事,不確定是否真的由於你的樂趣(我是emacs的新手):輸出不會自動向下滾動。 – vemv 2012-03-07 08:13:20

+5

@vemv,'(setq comint-scroll-to-bottom-on-output t)'將解決你的問題。 – VitoshKa 2012-03-07 11:17:44

+0

魔法!對一個相對較老的問題快速回答是最終的樂趣。 – vemv 2012-03-07 11:39:52

5

M-x shell-command-on-region

aka。

M- |

+5

它做了一些不同的事情 - 它會提示輸入shell命令並將選擇發送到該命令。我希望區域直接發送到shell(例如,如果我突出顯示「ls」併發送它,它會像將ls粘貼到* Shell *緩衝區中一樣) – 2011-06-09 02:13:16

+0

這很好,如果你只是把它管到bash ,或任何你想要的外殼。有點破解,但它可以讓你做到這一點,而無需編寫任何額外的功能。 – WLPhoenix 2014-03-17 03:57:54

+1

@WLPhoenix你能詳細說明一下嗎? – nicolas 2015-04-30 09:13:19

1

你想讓命令自動執行,還是隻是在準備好的命令行中輸入?

M-Xappend-to-bufferRET將進入選擇的文本到在點指定的緩衝區,但命令將不會被外殼來執行。

一個包裝函數可以自動爲緩衝區選擇*shell*(或者根據shell模式中的當前緩衝區更智能地選擇/提示),然後調用append-to-buffer。

您可以簡單地記錄一個鍵盤宏以複製區域,切換到*shell*,抽出並輸入(如果需要)。

F3M-瓦特C-Xb*shell*RETC-ýRETF4
C-XC-KÑmy-execute-region-in-shellRET
M-Xinsert-kbd-macroRETmy-execute-region-in-shellRET
(global-set-key (kbd "C-c e") 'my-execute-region-in-shell)

11
(defun shell-region (start end) 
    "execute region in an inferior shell" 
    (interactive "r") 
    (shell-command (buffer-substring-no-properties start end))) 
+0

這非常出色 - 可以讓我複製我最關心的Acme編輯器的功能。 – sea6ear 2012-11-12 22:58:31

7

的Mxappend-to-bufferRET

6

我寫發送/管行或代碼的區域的封裝殼進程,基本上類似的東西,ESS爲R.它還允許多個外殼進程存在,並讓你選擇發送區域到哪一個。

看一看這裏:http://www.emacswiki.org/emacs/essh

+0

美麗的包! – Nisba 2017-09-01 20:03:17

1

修改尤爾根斯回答上方以一個特定的緩衝操作提供了以下功能,將發送區域,然後切換到緩衝區中,在另一個窗口中顯示出來,該緩衝區命名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 
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()"))