emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Berry, Charles" <ccberry@health.ucsd.edu>
To: Vikas Rawal <vikasrawal@gmail.com>
Cc: org-mode mailing list <emacs-orgmode@gnu.org>
Subject: Re: Search and replace excluding code blocks?
Date: Wed, 31 Jan 2024 01:58:58 +0000	[thread overview]
Message-ID: <89C7F745-73D0-44F5-B5A2-0EE6090BDB70@health.ucsd.edu> (raw)
In-Reply-To: <CALtzAB1jWiTzr+21Bed5R7MhbQU+3r-HTNu+f3XL7VinkO=kYA@mail.gmail.com>



> 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	other threads:[~2024-01-31  2:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-29 23:47 Search and replace excluding code blocks? Vikas Rawal
2024-01-31  1:58 ` Berry, Charles [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=89C7F745-73D0-44F5-B5A2-0EE6090BDB70@health.ucsd.edu \
    --to=ccberry@health.ucsd.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=vikasrawal@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).