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
#!/bin/bash

TOP_DIR=$PWD
DIR1=first
DIR2=second

FILE_SEPERATOR="$DIR1/"
FILE_EXTENTION=".c"
SEARCH_FILE_TYPES="*.c"

REPORTS=out

#cleanup any previous reports
rm -rf $REPORTS

for f in $(find $DIR1 -name "$SEARCH_FILE_TYPES")
do
        echo "file found is $f"
        file_with_extention=${f#*$FILE_SEPERATOR}
        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
done

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/
out/
└── a
    └── Diff.html

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

vimdiff_2_html

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