When opening an A file, I'd like to check if there's a conflicting file B that follows some naming convention. If this happens, I'd like to transparently merge the content of B in A, keeping an ancestor C file (with known location and naming pattern) as a reference.

This should happen automatically and without any specific interaction by the user besides opening the A file. The ediff utility looks to be for interactive usage only.

If there are conflicting lines, they may just be both merged together with the typical dash-delimited diff output. The user may find and solve these conflicts manually with the enabled minor-mode smerge-mode on the A file buffer.

Ignoring the logic to find the B and C file, how can I just merge files automatically on Emacs? I'm not using any Version Control.

1 Answer 1


EDIT in response to your comment

Seems like indeed configuring the smerge-begin-re, smerge-lower-re, and smerge-end-re as follows before you activate smerge-mode

(setq-local smerge-begin-re "^#ifndef \\(.*\\)\n"
        smerge-end-re "^#endif \\(.*\\)\n"
        smerge-lower-re "^#else \\(.*\\)\n")

seals the deal.

This probably only works for 'simple' conflicts (see comments in 'smerge-mode.el'), but I guess that is good enough in this case.


AFAIK, 'merging' files is functionality provided by 'diff' tools. Emacs only provides modes/interfaces for interactive merging of output from certain diff tools, it does not provide the diff tools itself.

I am not sure, but it looks to me that the 'typical dash-delimited' diff output, is typical for Git. I don't know how to achieve that without Git.

Without version control, 'unconditional' merge of files can be achieved using the diff command with the -D flag e.g.:

diff --ifdef=AUTOMERGE file1.txt file2.txt > output.txt

From elisp you could invoke it as follows

(shell-command (format "diff -D AUTOMERGE %s %s > %s" "file1.txt" "file2.txt" "output.txt"))

However, unfortunately, smerge-mode does not seem to work for this output format.

Also, I don't know of any other interactive merging tool for this format. But if you know some elisp, then I guess it should not be very hard to create one yourself.

And of course, other people might know of any existing/better solutions.

  • 1
    Smerge provides the variables smerge-end-re, smerge-base-re and smerge-begin-re. I guess tweaking them would seal the deal?
    – cidra
    Commented Jan 11 at 10:54
  • 1
    Good find! Yeah, seems like that will do it... Commented Jan 11 at 13:15

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.