> On Jan 29, 2024, at 3:47 PM, Vikas Rawal <[email protected]> wrote:
>
> Hi all,
>
> Is it possible to search and replace a regex/string in an org file excluding
> the code blocks from its scope?
>
> Say, I want to replace all opening quotes (") by backticks(``) in the text
> but not in the code blocks. Is there a way one could achieve that?
>
Sure, the recommended way to search and replace programmatically is given in
(info "(elisp) Search and Replace")
Hacking that slightly to test if point is in a src block and using the double
quote preceded by white space as the search string and replacing by that white
space and double backticks gives:
#+begin_src elisp
(goto-char 0)
(while (re-search-forward "\\([[:space:]]\\)\"" nil t)
(unless (eq (org-element-type (org-element-at-point)) `src-block)
(replace-match "\\1``")))
#+end_src
You can roll this into a function and add arguments as desired to allow
different search/replacement strings.
Best,
Chuck