April 17, 2007

Comparing and merging files with Meld

Author: Nikos Kouremenos

When you're editing text, you may find yourself with two or three similar versions of a file. Often you are not sure which file has what content, what was deleted in one, what was added in another, and where exactly. You know what content your file should contain, but you are not sure which version to start from. It's one of those moments you wish your computer were smart enough to understand your problem and fix it for you. Well, that's not going to happen, but there is an open source application that you can run to help. Meet Meld, a visual diff and merge tool.

With Meld you can compare two or three files and edit them in place (diffs update dynamically). You can compare two or three directories and launch file comparisons. And lastly, you can browse and view a working copy from a popular version control systems such as CVS, Subversion, Bazaar, or Mercurial.

Meld is easy to install -- just untar and run it. You may want to run make to generate translations. Alternatively you can make a symlink to the untarred executable by typing ln -s melddir/meld /usr/local/bin/meld, or type make prefix=/usr/local install and you will have the Meld executable in /usr/local/bin/.

The project offers packages for many distributions, including Debian, Fedora, Gentoo, Slackware, and Arch Linux, as well as FreeBSD and Solaris.

Since Meld is a GNOME application written in Python, it depends on the gnome-python library to run, and if you want syntax highlighting in your documents, make sure you have gtksourceview installed.

Meld offers three kinds of viewing differences and doing comparisons: file comparison, directory comparison, and version control browser.

File comparison

With file comparison, you compare two or three files to find and then work on the differences.

File comparison - click to enlarge

To start, choose File -> New. In the File Comparison tab point to the files you want to compare by clicking the Browse button and selecting each file. When ready, click OK. In the display window, lines and characters that differ between the files are highlighted with different colors. Where you see black arrows between the two files, you can click on them to pass the content of one file to the other. In this example, if you click on the arrow pointing to the right next to the Peace and stability paragraph, Freedom (on the right) will be replaced by the text that has a cyan background on the left. If you need to undo, press Ctrl-Z.

Let's say that for some reason you want to add the Peace and stability cyan-highlighted section from the left document above of the Freedom highlighted section of the right document. Hold down the Ctrl key and notice that the single arrow gets replaced by two arrows pointing up and down. Click the one pointing up, and there you have it -- the text in question from the left document is added above of the text of the right document. When you're happy with the result, you can save the changes you've made by clicking the Save Button.

Directory comparison - click to enlarge

Directory comparison

With directory comparison, you compare two or three directories to discover and then work on differences in their contents. Choose File -> New, and in the Directory Comparison tab point to the directories you want to compare the same way you selected files above. Notice again the use of colors in the comparison pane. be@latin.po is green; it is a new file and as such is missing from the gajim_orig directory. In the same window, de.po is red, which means files differ. You can double-click a file to start a file comparison. Click the Down and Up buttons to travel between the files that have been marked by Meld as different, new, or missing. If you find a file that has two versions and want to keep only one of them, select the version you want to keep and click the Left or Right button (depending from which directory you copy).

Finally, Meld can also act as a version control browser for examining and committing or reverting local changes. Keep in mind though that it is not intended to be a complete version control client.

Stephen Kennedy, the main developer of Meld, has created a powerful tool. If the colors and possible actions confuse you at first, either stick to the basics and learn more progressively, or try reading the Help text. Power users should read the Filtering section in File Comparison chapter of the Help, as it allows you to ignore certain types of differences in order to locate only important differences, which can be useful when you're working with long text files.

Nikos Kouremenos is a computer science student who has been active in the open source community since 2002.

Click Here!