All Unkept
Posted in: Django, KDE, Software development  —  11 March 2006

Clipboard diff

Ever been refactoring code or HTML templates and wanted to see the difference between two chunks of text, and whether they can be merged in some way? Implementing this in KDE is just too easy. Here is the script you need:

#!/bin/bash

# Show the difference between the last two items
# on the clipboard

TMP1=`mktemp`
TMP2=`mktemp`
dcop klipper klipper getClipboardHistoryItem 0 > $TMP1
dcop klipper klipper getClipboardHistoryItem 1 > $TMP2
kompare $TMP1 $TMP2
rm $TMP1 $TMP2

Save that to a script, then set up a hotkey to it using KHotKeys (I'm using Ctrl+Win+D, 'd' for diff). To use it, copy chunk 1 to the clipboard using Ctrl-C, then chunk 2, then hit Ctrl+Win+D -- and you have a beautiful diff for your viewing pleasure.

Screenshot of Kompare showing the difference between 2 items on the clipboard

I just used this to conclude that I have two Django templates that are better off inheriting from a base template and overriding for differences, (rather than each including various snippet templates), especially since one of them has an addition I had forgotten to add to the other. I wrote the script ages ago, but thought someone else might find it useful.

For Windows users, I'm sorry, I imagine it would take you about 3 days to reproduce something like this working reliably. I'm not mocking -- I have the pain of using Windows for my day job, and it's like being forced to crawl everywhere on hands and knees after I've learnt to walk.

And don't get me started on software installation. I got myself a python-hosting account the other day, and everything I've tried has just worked. With a bit of help from a web-based tool they have, I had set up a trac/svn app for tracking bugs in about 10 minutes, and added authentication to lock down parts of it in about 20 minutes (trac also has a really nice command line tool for admin, BTW).

At work, on the other hand, using an installer and getting it to work correctly (for things like MS SQL Server, Sharepoint etc), is apparently something of an acquired expertise. Unfortunately for me, 'learning from the school of hard knocks' doesn't seem to have worked yet, so I spent most of this afternoon fighting and losing to various installers.

.

OK, so I digressed completely and rambled about my frustrations with Windows. Sorry about that.

Comments §

§ On 15 March 2006, SmileyChris wrote: 54
Meh, OS rambling is so passe ;)
With Eclipse I do the same - granted not from clipboard, but saving a temporary file and deleting it doesn't take 3 days to do...

§ On 15 March 2006, luke wrote: 55
With regard to 3 days, I meant creating a program that does what that shell script does.

I guess doing it manually every time doesn't take so long, but I have a very low threshold for boredom when it comes to that kind of thing. If I had never used KDE+Linux, though, I think my life would be much more blissfully ignorant.

§ On 15 March 2006, Gunnar wrote: 56
I commited a newbie mistake, though: the script must be made executable. Otherwise it's great.

§ On 10 May 2006, Nicola Larosa wrote: 66
Wow, thanks. I've needed doing this, and saving to temp files by hand has really been a drag.

By the way, Kompare is nice, but it does not show what has changed inside lines. I use Meld for two-way diffs, and KDiff3 for three-way ones (Meld does not show enough info for those).

Keep up the Windows bashing! ;-)

§ On 12 May 2006, Damien wrote: 67
Thanks for the tip. I did run into one minor thing while using it though; if the two chunks of text you are trying to compare are actually the same, then this won't work because Klipper is "smart" and recognizes that the text is the same and doesn't create a clipboard entry for the two identical copies, which means you actually end up comparing your text with something that you put in the clipboard before trying to view the diff. This can be deceiving if that last thing is actually close to what you are trying to compare.

Anyone know of a way to disable this behavior in Klipper?

§ On 23 June 2006, luke wrote: 77
Damien:

No, I don't know how to work-around that. Maybe you could have shortcut that combines the second Ctrl-V with the diff command. That way it could capture the current clipboard, do a 'Ctrl-V' itself (don't know exactly how you'd do this), capture the current clipboard again to a different file and continue as before.

For Windows users:

There is a port of Kdiff3 (a great KDE diffing program that can do directory diff/merge) for Windows. (It's especially powerful in combination with kio-slaves, which you obviously don't work with the Windows version, but it's still useful). One nice thing is it allows copy and paste direct to the panels, so it makes a pretty good substitute for the above.

Add comment

Format:

  • Javascript has to be on to get past my spam protection, and cookies, and there is a delay, sorry for any inconvenience!
  • I reserve the right to moderate comments.