Emacs Advent Calendar 21: Emerge

Mon 2015-12-21

Although Emacs' diff-mode can be used for rudimentary file merging, Emerge mode is more convenient for that purpose.

To start merging two files, run M-x emerge-files. It will ask for files A and B and start comparing them. The result is displayed in three windows: a buffer visiting A, a buffer visiting B and the merge buffer.

The merge buffer has two submodes: in fast mode single keystrokes invoke merging functions, so the usual Emacs editing commands are not available. In edit mode everything works as usual and the merging commands have to be prefixed with C-c C-c. To switch from fast mode to edit mode press e, to switch back press C-c C-c f. As a convention I will show all commands without the prefix for the remainder of this post.

The buffers can be scrolled in parallel with ^, v, < and >.

By default, differences in the merge buffer are in the A version. To change the default to B, press d b. Use d a to change default back to A.

The keys n and p walk through the differences. To switch the current difference to A or B, press a or b respectively. If you want to combine both versions, press x c. Normally you will want to fine tune the combination by hand in edit mode afterwards.

When you are finished merging, press q. This will leave you with the source files and a merge buffer that you can save. To abort a merge instead, press C-].

If you run M-x emerge-files with a prefix, it will ask for a file name to automatically save the merge buffer to after the merge is finished.

This is the basic use of emerge mode, have a look at chapter 28.5 (Emerge) of the Emacs Reference Manual for information on additional commands and features such as three-way merging.

Tags:

This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.