1

Is there a way to highlight a region of a org file and execute all code blocks in the region?

What I was looking for is basically to execute all code blocks up to a certain point, where I am writing new code in a block. org-babel-execute-buffer will execute all code blocks including the new one. And errors in the code may cause Emacs to hang. Hence the question above.

1 Answer 1

1

You can use narrow-to-region, then run org-babel-execute-buffer, then widen again:

(defun org-babel-execute-region (beg end &optional arg)
   (interactive "r")
   (narrow-to-region beg end)
   (org-babel-execute-buffer arg)
   (widen))

It's actually short enough to do with existing keybindings without a new function:

C-x n n C-c C-v C-b C-x n w

C-x n n does the narrowing, C-c C-v C-b does the babel evaluation and C-x n w widens the buffer.

BTW, this is a general technique of using a function that operates on a buffer and having it operate on only part of the buffer. There are some functions however where it will not work: those that widen the buffer as part of their operation; but they tend to be more specialized and you are probably not going to run into them often, but you should keep in mind this possibility. If things don't seem to work, check the source code of the function and see if it does a widen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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