Consider something like:

(while (re-search-forward "Pattern" nil t)
(replace-match "replace"))

How can I skip all lines that contain "Skip" from this replace search? I.e. Pattern should only be replaced in such lines that doesn't contain "Skip".

(defun foo (old new out)
  "Replace matches for regexp OLD by string NEW, except in lines with string OUT."
  (while (re-search-forward old nil t)
    (unless (save-match-data
                (forward-line 0)
                (re-search-forward out (line-end-position) 'NOERROR)))
      (replace-match new))))

Then use (foo OLD NEW "Skip"), for whatever values of OLD and NEW you want.

Add args to replace-match if you don't want the automatic case-sensitivity behavior provided or if you want a literal string replacement.

  • @student, see C-h i g (elisp)Backquote
    – phils
    Sep 4 '17 at 22:01
  • Btw. What does the , mean in front of skip?
    – student
    Sep 5 '17 at 18:51
  • @phils already pointed you to info about backquote + comma syntax. See (elisp) Backquote.
    – Drew
    Sep 5 '17 at 19:28
  • As out isn't used, I can't see how this solution works. What am I missing? Aug 30 '19 at 5:12
  • @BerenddeBoer: Sorry. Should be OK now. I left a literal "Skip" in place of OUT.
    – Drew
    Aug 30 '19 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.