4

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

#+BEGIN_SRC sh
sudo pacman -Syuu
#+END_SRC

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

is that possible?

3 Answers 3

1

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 () 
(interactive)
(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)
2
  • thx. i tried it and get the following error get-buffer: Symbol's function definition is void: directory-shell-buffer-name. any clue?
    – zeltak
    Commented 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
    Commented Jun 26, 2016 at 23:15
0

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
date
#+END_SRC

reference

0

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
#+END_SRC

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.