File and Directory diff in color in Midnight Commander

Midnight Commander (MC) is a lean, but powerful, two-panel file manager that, admittedly, promotes productivity while working from a console. Among other features, it implements a user menu with pre-defined actions that can be performed on the selected files or directories. This menu can be further customized by the user on a directory-, user- or system-basis. While searching for ready-made actions for MC‘s user menu, I came across an excellent set, which adds the ability to display the differences (diff output) between two versions of a file or directory in color.

Add the necessary code

The code that needs to be added to the user menu is the following:

+ t r & ! t t
d       Diff against file of same name in other directory
        if [ "%d" = "%D" ]; then
          echo "The two directores must be different"
          exit 1
        fi
        if [ -f %D/%f ]; then        # if two of them, then
          diff -up %f %D/%f | sed -e 's/\(^-.*\)/\x1b[1;31m\1\x1b[0m/g' \
                                  -e 's/\(^\+.*\)/\x1b[1;32m\1\x1b[0m/g' \
                                  -e 's/\(^@.*\)/\x1b[36m\1\x1b[0m/g' | less -R
        else
          echo %f: No copy in %D/%f
        fi

D       Diff current directory against other directory
        if [ "%d" = "%D" ]; then
          echo "The two directores must be different"
          exit 1
        fi
        diff -up %d %D | sed -e 's/\(^-.*\)/\x1b[1;31m\1\x1b[0m/g' \
                             -e 's/\(^\+.*\)/\x1b[1;32m\1\x1b[0m/g' \
                             -e 's/\(^@.*\)/\x1b[36m\1\x1b[0m/g' | less -R
        fi

The code snippet is a contribution by James Ogley to this OpenSUSE wiki page about Midnight Commander and is released under the GFDL 1.2 license.

Here are the instructions on how to add this code to the user menu:

  1. Having launched Midnight Commander, press F9
  2. Go to the Command menu and hit the Edit menu file
  3. MC lets you customize the user menu for the current directory [Local], current user [Home] or make the modification system-wide. You will need to be root to change the latter. Most probably the Home choice is what you need.
  4. You can now paste the code block between two other user-defined commands.

How to use these actions

In order to show the differences between two versions of the same file, browse to the directories that contain each version, so that the directory in panel A contains the A version of some.file and the directory in panel B contains the B version of some.file. Then press F2 and lowercase d.

In order to show the differences between the contents of the directories in the two panels, just press F2 and then uppercase D.

File and Directory diff in color in Midnight Commander by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2007 - Some Rights Reserved

George Notaras avatar

About George Notaras

George Notaras is the editor of the G-Loaded Journal, a technical blog about Free and Open-Source Software. George, among other things, is an enthusiast self-taught GNU/Linux system administrator. He has created this web site to share the IT knowledge and experience he has gained over the years with other people. George primarily uses CentOS and Fedora. He has also developed some open-source software projects in his spare time.