Does anyone know if its possible to send the bash code in a orgmode sh block and run in in a new eshell/term buffer in emacs? for example as a use case i would like to run/eval a code block like this

sudo pacman -Syuu

and have the command execute within emacs in term, eshell etc?

is that possible?

3 Answers 3


I use the following to send the current line or selected region to the shell associated with a given buffer:

(defun kdm/sh-send-line-or-region () 
(if (use-region-p) 
  (append-to-buffer (get-buffer (directory-shell-buffer-name)) (mark)(point))
  (let (p1 p2)
    (setq p1 (line-beginning-position))
    (setq p2 (line-end-position))
    (append-to-buffer (get-buffer (directory-shell-buffer-name)) p1 p2)
  (let (b)
    (setq b (get-buffer (current-buffer)))
    (switch-to-buffer-other-window (get-buffer (directory-shell-buffer-name)))
    (execute-kbd-macro "\C-m")
    (switch-to-buffer-other-window b)

(global-set-key "\M-s" 'kdm/sh-send-line-or-region)
  • thx. i tried it and get the following error get-buffer: Symbol's function definition is void: directory-shell-buffer-name. any clue?
    – zeltak
    Jun 26, 2016 at 15:06
  • Not sure, but perhaps that is defined by my shell, which is configured with: (use-package shell-current-directory :bind ("M-S" . shell-current-directory) :config (load-file (expand-file-name "shell.el" user-emacs-directory)) )
    – mankoff
    Jun 26, 2016 at 23:15

If you want to simply execute some commands then write them in the source block and execute using C-c C-c while in the block. To execute on a remote machine or as sudo combine use of :dir and tramp, For example:

#+BEGIN_SRC sh :dir /sudo:pacman@localhost:
who am i



Yes, there are several ways this could be done. The most straight forward at the time of writing is probably to use a session:

#+BEGIN_SRC sh :session *my-shell*
sudo pacman -Syuu

This will create a new comint process (like with M-x shell) called "*my-shell*". Blocks whose header specifies the session will be executed in that shell. If you wanted to interact with it outside of Org, you can switch to it in the buffer list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.