# Add increase/decrease tab indent buttons to the editor toolbar

These should work by adding spaces or removing spaces from the beginning of the current line or every line between the start and end of a text selection (inclusive).

(edited this question to supplement Improvements to editor for formatting source code)

-
Duplicate - meta.stackexchange.com/questions/493/… –  ChrisF Jul 22 '09 at 15:28
Too true, but my FR included something additional. Removed dupe feature request. –  Won't Jul 22 '09 at 15:35
Unfortunately I can't cancel my close vote, but no longer a dupe. –  ChrisF Jul 22 '09 at 15:37
@AD7six: Eh, I don't believe that, no matter who says. But I'll do it because it's Lance :| –  Won't Jun 5 '14 at 14:14
I'm not against this idea, but I wonder how many people would then think they could indent paragraphs without applying the formatting as code. (Or: be wondered why indented text is formatted as code.) –  Arjan Jul 22 '14 at 22:58

Yes please on the Tab doing actual spaces instead of taking you out of the box.

Edit: This applied to his original question about allowing Tab to work right in the editing textbox. But indent/dedent buttons would go a long way towards making code formatting user-friendly.

-
I would be fine with Tab only modifying indentation when there is a selection, otherwise Tab would enter/leave the text field. Though I would very much like to be able to choose the indent (3 vs. 4 spaces). –  ErikE Nov 28 '12 at 21:47

Select a block, or put your cursor on a new line, and hit Ctrl+K - it'll automatically indent as needed for a code block.

-
Actually, that behavior isn't what I'm looking for. It also behaves weirdly when selecting lines that are already tabified. –  Won't Jul 22 '09 at 15:34
Ctrl+K doesn't work properly on list items. meta.stackexchange.com/questions/3792/… –  Brad Gilbert Jul 22 '09 at 15:58
That's a shame. :-( –  Shog9 Jul 22 '09 at 16:20
It works for code blocks, though, and if you need to indent a bunch of code that's already indented you can just a dummy character in position one somewhere for long enough to do your insert. –  Joel Coehoorn Sep 4 '09 at 14:04
You should probably update this to note that you can use Ctrl+K to remove indents one stage at a time as demonstrated here. Or you could just merge that question into this one... your choice. ♪ –  Grace Note May 27 '11 at 16:52

Edit: Indentation buttons are now available as a user-script on Stack Apps.

I maded my own buttons now. Currently they reside in my bookmarks toolbar:

And they link somewhere nice, the left one for example:

javascript:var%20tb%20=%20document.getElementById("wmd-input");...

In Firefox and Chrome (the only ones I tested) you can just bookmark scripts, and they nicely convert all the whitespace. Here are the full scripts, nothing special really:

//Indentation Up
var tb = document.getElementById("wmd-input");
var text = tb.value.substring(tb.selectionStart, tb.selectionEnd);
var lines = text.split("\n");
var shift = 0;
for (var i = 0; i < lines.length; i++)
{
lines[i] = "    " + lines[i];
shift += 4;
}
replaceSelection(tb, lines.join("\n"));
void (0);

function replaceSelection(textbox, text)
{
var selectionStart = textbox.selectionStart;
textbox.value = textbox.value.substring(0, textbox.selectionStart) + text + textbox.value.substring(textbox.selectionEnd);
textbox.selectionStart = selectionStart;
textbox.selectionEnd = selectionStart + text.length;
}

//Indentation Down
var tb = document.getElementById("wmd-input");
var text = tb.value.substring(tb.selectionStart, tb.selectionEnd);
var lines = text.split("\n");
var shift = 0;
for (var i = 0; i < lines.length; i++)
{
if (lines[i].substring(0, 4) == "    ")
{
lines[i] = lines[i].substring(4);
shift += 4;
}
if (lines[i].charCodeAt(0) == 9)
{
lines[i] = lines[i].substring(1);
shift++;
}
}
replaceSelection(tb, lines.join("\n"));
void (0);

function replaceSelection(textbox, text)
{
var selectionStart = textbox.selectionStart;
textbox.value = textbox.value.substring(0, textbox.selectionStart) + text + textbox.value.substring(textbox.selectionEnd);
textbox.selectionStart = selectionStart;
textbox.selectionEnd = selectionStart + text.length;
}


Note that the preview will not update after the script has been executed. Some calls which would do that could be included I suppose.

(I am aware that the code is redundant, I just did it this way for simplicity.)

-
Drop that puppy on stackapps and see if you can get some help with it. –  Won't Jun 6 '11 at 13:29
Good idea. Yesterday i wrote a greasemokey script which adds buttons & keyboard shortcuts to the editor, might do some polishing before posting it though. –  H.B. Jun 6 '11 at 13:36
@Won't: Submitted it here, also added the link to the answer. –  H.B. Jun 7 '11 at 5:36
I'd make the favicons the arrows if possible, and make the name blank. –  hexafraction Oct 9 '12 at 19:25