I would like to scan through a (latex) document and recursively delete -- WITH QUERY -- all consecutive, repeated words that need not be on the same line and/or the same case. Here is a simple example of the sort of texts I have in mind.

This is an example Example of the sort of texts I would
would like to handle.

I have found two previous emacs.stackexchange posts on this topic:

regexp to find two consecutive and identical words not necessarily in the same line

Find and remove consecutive duplicated words while ignoring case

Is there a way to combine these two AND add

  • the ability to delete the duplicate word, with query -- ie ask me before deleting it
  • recursively run through the whole document (eg like flyspell)

Bonus request: Is it possible to highlight the repeated word (eg like flyspell)?

I'm complete newbie to regexp; thanks for your patience and help!

1 Answer 1


You can use query-replace-regexp with the following parameters:

  • \b\(\w+\)\W+\1 for matching a word (saved as a matching subexpression by means of \( and \)), followed by non-word characters, then followed by the saved first word. \b matches a word delimiter, to avoid matching parts of words.
  • \1 as the replacement (the matched word)

If you want to highlight double words, then use the same matching expression with highlight-regexp.

  • You have to check for case sensitive or not. Default is insensitive. For more information C-h f query-replace-regexp RET
    – gigiair
    Jan 18, 2022 at 18:31

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.