2

I am using a command which affects the entire buffer, replacing all the ocurrences of a given expression

(goto-char (point-min)) (replace-string "foo" "bar")

Is it possible to apply a command like this only to the ocurrences outside an org-babel source block? That is, only the first foo on this example text should be replaced by bar, but not the second one:

* Test
testing foo

#+begin_src lisp
(setq foo 3)
#+end_src
Drew
  • 75,699
  • 9
  • 109
  • 225
Mario Román
  • 271
  • 2
  • 10

1 Answers1

3

The following is a modification of replace string programmatically to avoid replacing inside source blocks:

#+begin_src elisp
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "foo" nil t)
      (unless (org-in-src-block-p) (replace-match "bar"))))
#+end_src

You may use search-forward instead of re-search-forward which searches for regular expressions.

You may also use header arguments to pass the strings as variables.

Juancho
  • 5,395
  • 15
  • 20