emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Search and replace excluding code blocks?
@ 2024-01-29 23:47 Vikas Rawal
  2024-01-31  1:58 ` Berry, Charles
  0 siblings, 1 reply; 2+ messages in thread
From: Vikas Rawal @ 2024-01-29 23:47 UTC (permalink / raw)
  To: org-mode mailing list

[-- Attachment #1: Type: text/plain, Size: 277 bytes --]

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?

Thanks,

Vikas

[-- Attachment #2: Type: text/html, Size: 400 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Search and replace excluding code blocks?
  2024-01-29 23:47 Search and replace excluding code blocks? Vikas Rawal
@ 2024-01-31  1:58 ` Berry, Charles
  0 siblings, 0 replies; 2+ messages in thread
From: Berry, Charles @ 2024-01-31  1:58 UTC (permalink / raw)
  To: Vikas Rawal; +Cc: org-mode mailing list



> 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-31  2:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-29 23:47 Search and replace excluding code blocks? Vikas Rawal
2024-01-31  1:58 ` Berry, Charles

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).