2

I have a large bash script and I need to rerun certain lines of code. Normally I would comment out any line I don't want to run and then execute the file on the shell. But that is rather cumbersome. I'd much rather select the lines I want to execute and do something like M-x execute-selected-lines-in-the-current-directory. Is there something like that?

user2740
  • 539
  • 1
  • 5
  • 10
  • What does the substring `in-the-current-directory` in the assumed command name `execute-selected-lines-in-the-current-directory` mean? Could you elaborate please? Do you want to run the selected lines in a directory different from the `default-directory` of the current shell script file buffer? – Tobias Feb 25 '19 at 15:23

2 Answers2

2

You could try sh-execute-region which is bound to C-M-x in sh-mode.

Drew
  • 75,699
  • 9
  • 109
  • 225
stsquad
  • 4,626
  • 28
  • 45
2

Use M-| (shell-command-on-region) then sh to pipe the current region to Bash. It works because bash supports running program from STDIN, e.g.,

~ $ echo 'date' | sh
Mon Feb 25 21:54:51 CST 2019

M-| runs via a fresh shell every time, so I'm not sure if it can work in your case.

xuchunyang
  • 14,302
  • 1
  • 18
  • 39