66

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)

7
  • Duplicate - meta.stackexchange.com/questions/493/…
    – ChrisF Mod
    Commented Jul 22, 2009 at 15:28
  • Too true, but my FR included something additional. Removed dupe feature request.
    – user1228
    Commented Jul 22, 2009 at 15:35
  • Unfortunately I can't cancel my close vote, but no longer a dupe.
    – ChrisF Mod
    Commented Jul 22, 2009 at 15:37
  • 1
    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
    Commented Jul 22, 2014 at 22:58
  • I fully support this. Please please please implement it.
    – user233836
    Commented Jul 12, 2016 at 0:14
  • @StephanKristyn Well, this was asked seven years ago, so I don't think there's much chance of that. You can sort of do this, however, by selecting the chunk of text you want to fix and hitting ctrl-k. That will either indent it, or if it is indented more than four spaces, move it back. It's useful for fixing code that's over-indented.
    – user1228
    Commented Jul 12, 2016 at 12:48
  • Related to meta.stackexchange.com/q/174966/232804
    – Yves M.
    Commented Jul 2, 2022 at 10:47

4 Answers 4

19

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.

1
  • 1
    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
    Commented Nov 28, 2012 at 21:47
14
+100

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

Extension Screenshot


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

Buttons

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.)

4
  • 2
    Drop that puppy on stackapps and see if you can get some help with it.
    – user1228
    Commented Jun 6, 2011 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.
    – brunnerh
    Commented Jun 6, 2011 at 13:36
  • 1
    @Won't: Submitted it here, also added the link to the answer.
    – brunnerh
    Commented Jun 7, 2011 at 5:36
  • I'd make the favicons the arrows if possible, and make the name blank.
    – nanofarad
    Commented Oct 9, 2012 at 19:25
3

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.

5
  • 2
    Actually, that behavior isn't what I'm looking for. It also behaves weirdly when selecting lines that are already tabified.
    – user1228
    Commented Jul 22, 2009 at 15:34
  • Ctrl+K doesn't work properly on list items. meta.stackexchange.com/questions/3792/… Commented Jul 22, 2009 at 15:58
  • 1
    That's a shame. :-(
    – Shog9
    Commented Jul 22, 2009 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 Mod
    Commented Sep 4, 2009 at 14:04
  • 2
    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 StaffMod
    Commented May 27, 2011 at 16:52
0

What if it looked like this:

enter image description here

to address concerns like this:

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

3
  • Please don't reply to comments by adding answers. That's not how things work here.
    – user1228
    Commented Apr 13, 2015 at 14:54
  • @Won't this answer isn't a reply. It's an answer. The comment happens to focus the question on a particular issue that this answer addresses. I'm not looking to have a discussion so see no need to respond. If you must comment please comment on the merits of the idea. Commented Apr 13, 2015 at 15:45
  • @CandiedOrange Didn't we have a discussion about this previously? You must have deleted your answer and reposted (for some reason). As I stated back then, your answer doesn't explain anything, and an answer which expects others to "work it out for themselves" as you stated last time is not a good answer. Remove the comment from your answer as it is confusing, and just write what your answer is about "I've made an image with a mock up of what my idea would look like. It should resolve confusions such as..."
    – James
    Commented Apr 13, 2015 at 18:26

You must log in to answer this question.