Home » Software Development » Editor » Vim » Save Vimdiff Output To HTML

Save Vimdiff Output To HTML

The script ( vimdiff_to_html.sh ) matches the filename from first directory to same filename from second directory and takes a difference between two files using vimdiff and saves a html of diff into output directory under the matching name of the filename

for example, out put of below command, “vimdiff first/a.c second/a.c” will be saved as html in out/a/Diff.html

$ vim vimdiff_to_html.sh




#cleanup any previous reports
rm -rf $REPORTS

for f in $(find $DIR1 -name "$SEARCH_FILE_TYPES")
        echo "file found is $f"
        file_without_extention=${file_with_extention%$FILE_EXTENTION} #remove extention
        echo "file name for directory is $file_without_extention"

        mkdir -p $REPORTS/$file_without_extention

        cd $REPORTS/$file_without_extention #to save output Diff.html is respective directory
        vimdiff $TOP_DIR/$f $TOP_DIR/$DIR2/$file_with_extention -c ":TOhtml" -c ":wq!"
        cd $TOP_DIR #back to top dir for next iteration

To demonestrate, the working of above script, we create two directories “first” and “second”, both of this directories will have same file “a.c” but there will be difference in source code, which we want to identify and save to html.

$ mkdir first
$ vim first/a.c
#include <stdio.h>

int main(void) {
        int a;
        printf("Hello World\n");
        return 0;
$ mkdir second
$ vim second/a.c
#include <stdio.h>

int main(int argc, char **argv) {
        int a;
        printf("Hello World\n");
        a = 10;
        return 0;

and we will create a “out” directory where the script will save the difference html.

Now, lets run the script as,

$ bash vimdiff_to_html.sh

Above script will auto exit and create a file as,

$ tree out/
└── a
    └── Diff.html

Now, if we open this DIff.html using any browser, it will show as,


Above is a automated script , which inside use a simple command as below,

$ vimdiff first/a.c second/a.c -c :TOhtml -c :wq!

You may use above command to generate html for single file.

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment