Cleaning Up Whitespace in your Github Diffs [Bookmarklet]

February 20, 2015

Ever had those pesky whitespace changes show up in your github diffs? They’re usually just trailing whitespace, or from converting to tabs (or spaces if you prefer). They often aren’t what you are trying to focus on, and luckily Github has a way of removing them.

But adding a ?w=1 query param is a hassle. So here’s a way to shave a few seconds off with a bookmarklet. Drag and drop this link to your bookmarks bar 〉 Remove GitHub Whitespaces

Here’s the code un-minified in case you’re wondering.

	var url = window.location.href;
	if (url.indexOf('') === -1) { alert('Meant to be used on'); return; }
	var insertAt = ((url.indexOf('?') === -1) ? false :  url.indexOf('?')) || ((url.indexOf('#') === -1) ? false :  url.indexOf('#')) || (url.length);
	url = url.replace('?', '');
	var redirectUrl = url.substr(0, insertAt) + "?w=1&" + url.substr(insertAt, url.length);
	window.location.href = redirectUrl;

While we’re on the topic of whitespace, if you are using SublimeText, there’s a handy plugin called TrailingSpaces that can highlight and delete trailing whitespace from your files.