Problem summary

Imagine, you have 2 shopping lists:

  • 1 huge one with hundreds of entries - list A, copied into buffer A
  • 1 containing dozens of entries - list B, copied into buffer B


  • 75% of the entries of list B are already part of list A, but the other rest of list B not—and you don't have a clue or overview which specific entries of list B are the new ones compared to list A.

  • You want to extend list A by list B, so that the "new list A" contains all entries of list A and B without any doppler—so, each entry just 1 time.

To make a simple example:

  List A:        List B:      What you want—"new list A":

  * Tomato       * Garlic     * Tomato
  * Leek         * Tomato     * Leek
  * Garlic       * Cherries   * Garlic
  * Lentils      * Leek       * Lentils
  * Banana                    * Banana
  * Almond milk               * Almond milk
                              * Cherries


How can that be done as efficient as possible?


  • I'm not able to program.
  • I'm very new to the emacs command "ediff", yet.
  • I'm relatively new to emacs, using it just for 1 year on a daily basis so far.
  • I'm getting familiar with using keyboard macros, and keyboard marcos built of a series of keyboard macros—like keyboard macro D(A + 7xB + 3xC)

Details on research:

I've already researched on it using web search engines and starting to turn to the emacs command "ediff".

What I've tried:

I tried M-x ediff-buffers, but the outcome wasn't very useful: Both buffers were compared automatically, but not in a smart way; comparison wasn't content sensitive, but line by line, so it said "line 1, 2, 3,... are not the same", when I needed it to say "Cherries is different".

Next thing I've tried was M-x ediff-buffers3 for merging, but it had the same problem:

It just let me choose, if for example line 2 of buffer A or buffer B will be used for the merge version, when I wanted emacs to automatically figure out what entries of list B are the new ones for list A and then merging list A only with these new entries.

I see the possibility of taking list B and manually take each entry of it—one by one—and search for it in list A. This way I would figure out by what entries of list B the list A needs to be extended, but that's an arse full of work.

There gotta has to be a more efficient way with any emacs commands.

  • "comparison wasn't content sensitive, but line by line". How so? I don't see that with ediff-buffers. It doesn't compare lines. Say exactly what you're doing with it.
    – Drew
    Commented Jun 13, 2020 at 20:43
  • what's the record separator? it's only lines? it's list A's order important? it's case sensitive? because @db48x answer is good enough if the example is true to the use case and order doesn't need to be preserved.
    – Muihlinn
    Commented Jun 13, 2020 at 21:03

1 Answer 1


You don't need to do anything fancy with ediff or keyboard macros. Just concatenate the two files together, then remove any duplicates.

You can do that at the command line: cat a b | sort | uniq > c

Or you can do it in emacs: paste both lists into a single buffer, then select everything with C-x h. Run M-x sort-lines to sort them, then M-x delete-duplicate-lines to remove the duplicates. Save it to a new file.

  • 1
    Good answer, if the use case is only about lines (it's not too clear that it is, but the example shown is). If the text overlaps are not just lines then a diff/merge command can help.
    – Drew
    Commented Jun 13, 2020 at 20:44
  • Oh, fantastic! Worked like a charm! Thank you, db48x!
    – starquake
    Commented Jun 14, 2020 at 11:57
  • You're welcome.
    – db48x
    Commented Jun 14, 2020 at 18:58

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.