emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Search and replace excluding code blocks?


From: Berry, Charles
Subject: Re: Search and replace excluding code blocks?
Date: Wed, 31 Jan 2024 01:58:58 +0000


> On Jan 29, 2024, at 3:47 PM, Vikas Rawal <vikasrawal@gmail.com> 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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]