Skip to content

Instantly share code, notes, and snippets.

@NV
Created March 18, 2010 14:14
Show Gist options
  • Save NV/336382 to your computer and use it in GitHub Desktop.
Save NV/336382 to your computer and use it in GitHub Desktop.
UserScript. Replace tabs with 2 spaces

Tab size UserJS

8 spaces for the tab character is too many. I'd prefer 2.

Supported browsers: Firefox, Google Chrome, Opera, and Safari.

beforeafter
jQuery.fn.extend({
	show: function( speed, callback ) {
		...
	}
 	...
jQuery.fn.extend({
  show: function( speed, callback ) {
    ...
  }
  ...

Configure tab size

In Firefox you can change default 2 spaces to something else.

User Script Commands... → Change tab size... Replace tabs with

In other browsers you have to edit REPLACEMENT variable manually.

// ==UserScript==
// @name Tab size
// @namespace http://nv.github.com/tab-size.js/
// @include *
// @description Replace all tab characters with two spaces (or something else)
// @version 1.0
// ==/UserScript==
var REPLACEMENT = ' ';
if (typeof GM_getValue === 'function') {
REPLACEMENT = GM_getValue('tab_replacement') || REPLACEMENT;
}
var pre_elements = document.body.getElementsByTagName('pre');
for (var i=0; i<pre_elements.length; i++) {
var no_tabs = pre_elements[i].innerHTML.replace(/\t/g, REPLACEMENT);
if (pre_elements[i].innerHTML != no_tabs) {
pre_elements[i].innerHTML = no_tabs;
}
}
if (typeof GM_registerMenuCommand === 'function') {
GM_registerMenuCommand('Change tab size...', function setTabReplacement(value){
GM_setValue('tab_replacement', prompt('Replace tabs with', REPLACEMENT));
});
}
@maboloshi
Copy link

Good. Even code blocks in comment boxes are supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment