I'm currently working on a file that has a lot of duplicate lines splattered across the file and I'm trying to remove those duplicates. I tried using replace-regexp with \(.+^J\)\(\(.+^J\)+?\)\1+ => \1\2 which is working okay. Then I realised that there'll be some lines not caught by that so I captured it in a macro. But now I'm stuck running that several times and not knowing if/when it'll ever be done.

I get the feeling I should be able to use some sort of function to do this but I'm not entirely sure how. What should I be doing instead?

  • Replace Regexp will work on the region, run mark-whole-buffer before you run it to make sure it covers the whole buffer. I have never seen replace-regexp miss something it should have replaced, if that is really happening it's a bug. Jun 22, 2015 at 13:07
  • @jordonbiondo sometimes a regexp replace will create a new string that matches the original regexp. Eg, search & replace two spaces with one space: if the original has four spaces in a row, the replacement will be two spaces. Silly example, but I occasionally run into similar problems with real regexps.
    – Tyler
    Jun 22, 2015 at 13:15
  • Example: HereIsAFirstRow HereIsASecondRow HereIsAFirstRow HereIsASecondRow Running the regex will create HereIsAFirstRow HereIsASecondRow HereIsASecondRow which can be matched by the regex
    – DrugCrazed
    Jun 22, 2015 at 13:20

2 Answers 2


Why not use a simpler function than regex?

M-x delete-duplicate-lines

is often what I use. It is a function in sort.el.gz. If there is a special reason for keeping/retaining one copy of the duplicate line (say you want to keep the last one but delete all others), then you may want to explore repeatable macros with regex conditions.

  • Infuriatingly, the copy of emacs that I have at work is 23.3.1 so that function isn't available to me :-( However, I'll mark as accepted.
    – DrugCrazed
    Jun 23, 2015 at 8:48

You can run a macro endlessly till the first error occurs (like reaching the end of buffer) by passing it a 0 prefix. Using the default keyboard macro binding, that would be M-0 F4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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