1

I'd like to evaluate a bash script line by line. I also might want to jump back and execute a previous line again.

As described in How execute bash script line by line? , one could use the built-in debugging option -x, but this is not very handy, since you don't have the overview of previous and future commands.

For writing software in R, I used RStudio. The editor allows to evaluate the current line as an R-Command by hitting Ctrl+Enter. Afterwards the result is shown in a built-in shell, and the Cursor jumps to the next command.

Is there a simple text-editor (like gedit) that allows to send the current line to a built-in shell/console (bash, zsh,...) and view the result of the evlauation afterwards in the shell?

Community
  • 1
  • 1
Edward
  • 4,453
  • 8
  • 44
  • 82

2 Answers2

3

It's not built in to Emacs, but it's easy to do.

(defun shell-eval-line (pos)
  "Evaluate the line around position as a shell command.
In interactive mode, use the cursor's position."
  (interactive "d")
  (save-excursion
    (goto-char pos)
    (shell-command (buffer-substring
            (line-beginning-position) (line-end-position))) ))

Bind to a key of your liking (C-c ! maybe?) and go.

Add a (next-line) outside the (save-excursion) to make it advance to the next line when it's done, or create a simple macro around it to invoke the function and jump to the next line.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    Typically, in your `$HOME/.emacs.d/init.el` file. If you only want to load it when you need it, and not burden your `init.el` with stuff you rarely use, put it in a separate file and load it from there. For quick ad-hoc experimentation, just paste it in the `*scratch*` buffer and `M-x eval-defun` See also http://www.emacswiki.org/emacs/InitFile. – tripleee Feb 20 '15 at 08:22
1

You can also do it with the editor geany. In their Wiki they have a detailed instruction. In short:

  • Install geany
  • Open the file ~/.config/geany/geany.conf and set send_selection_unsafe=true
  • Restart geany
  • Set a key-binding Edit > Preferences > Keybindings (It is under Format / Send selection to terminal)

Actually you don't have to select the code you want to send. So far I couldn't find out how to instruct geany to jump to the next line afterwards.

R_User
  • 10,682
  • 25
  • 79
  • 120