What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 133 Stack Exchange communities.

Is it just me or is inserting 4 spaces in front of every line of my code very annoying?

I would love if there was some way to put XML code in my posts without having to put 4 spaces in front of each line everytime (which is pretty tedious when it's like 20-30 lines).

I've always liked a forum's [code] tags for this purpose because if there is something like [blah] then it just gets put in as plain text as [blah]. With Stack Overflow/Super User/Server Fault though, it would get stripped out unless it has 4 spaces before it on the same line.

Just look:

<html>
  <body>
  </body>
<html>

which ends up being (with< stripped)

    html>
      body>
      /body>
    /html>

If instead we could do something like

    html>
  body>
  /body>
/html>

so that only the first line had to be prefixed with 4 spaces it'd be OK also.

Or if SO could be told to ignore all HTML tags(but not strip them) inside of pre-code then that'd be great.

Is there any hope of getting some easier way to put XML code in questions/answers?

share|improve this question
9  
I just configure my editor to use two spaces for indentation. And then double-indent before copying the code I intend to use. Editors are handy tools... –  Shog9 Jan 5 '10 at 18:25
    
I use tabs for indentation which doesn't seem to work for me in the browser. –  Earlz Jan 5 '10 at 19:41
1  
I prefer the 4 spaces, much better than [code] (german keyboard) –  Carlos Heuberger Jan 5 '10 at 19:57
7  
@earlz - That's because using tabs for indentation is evil. :P –  Super Long Names are Hilarious Jan 5 '10 at 19:58
3  
Using spaces for indentation is evil. With tabs I can change from 2 space to 8 space from my editor without touching a single file. Also, there is the whole makefile issue. –  Earlz Jan 5 '10 at 20:14
    
the server-side MarkdownSharp parser should convert leading tabs to spaces. Not sure if the client-side Showdown.js does. –  Jeff Atwood Jan 7 '10 at 11:09
2  
My editor can change tabs to any number of spaces. And convert them to actual spaces. Editors are powerful juju... –  Shog9 Jan 12 '10 at 20:13

7 Answers 7

up vote 30 down vote accepted

I would love if there was some way to put xml code in my posts without having to put 4 spaces in front of each line everytime(which is pretty tedious when its like 20-30 lines)

There is. You paste it, then select all the code and hit the code button on the toolbar, or the Ctrl+K keyboard shortcut.

share|improve this answer
    
nifty!! Didn't know about that (even though it was right in front of me) –  Earlz Jan 5 '10 at 19:42
3  
@earlz i didn't see it for a while either. and it took me a while to even trust any of those editor buttons--there are so many buggy javascript wysiwyg editors out there that i had learned not to use them before i got to SO. –  Kip Jan 5 '10 at 20:37
    
... and 3 years after Ctrl+K doesn't work in Opera. –  Tomasito Mar 18 '13 at 23:54

Most programmers would automatically think:

"Hey, this is too complicated and tedious. Do I have to do it every time? Surely someone else has already thought the same thing, so there must already be another, better, way to do it. Let's search the documentation to see if I'm right."

Normally I use the Firefox plugin "It's All Text!" to edit my post (or at least the code portion) in my favourite editor, which happens to be vim, and then select all the code and hit >> to indent it by one "soft tab".

Or, you know, you can always select your code and hit the little binary button above the edit box which will also indent it appropriately.

share|improve this answer
6  
well, as a programmer, my initial thought was "i'm going to ignore this javascript wysiwyg toolbar, because those things are usually buggy as hell" –  Kip Jan 5 '10 at 20:39
    
@Kip I would've made fun of you too if I hadn't done the exact same thing for a while. I usually use the same plugin Ether mentioned, and I have an Emacs macro that does the 4-space indenting, so I usually use that despite knowing about the toolbar button now –  Michael Mrozek Aug 21 '10 at 13:11

I ported Michael's C#/.NET code and built a web UI around it.

Hosted app:

stackoverflowIndentFourSpaces - "Indent Four Spaces (for SO)"

(scroll to bottom of app above for links to the open source PHP code)

Core function ported over:

<?php
function normalize_indent($code, $num_of_spaces_to_indent)
{
    /* This function is a port/fork of [Michael Stum](http://meta.stackoverflow.com/users/91/michael-stum)'s
    *  #C/.NET code <http://meta.stackexchange.com/questions/34588/4-spaces-for-code-tags-is-annoying/34594#34594>.
    */
    $spaces = str_repeat(' ', $num_of_spaces_to_indent);
    $removeIndent = 0;
    $code = str_replace("\t", '    ', $code);
    $codeLines = preg_split("/\012\015?/", $code);
    foreach($codeLines as $s)
    {
        if($s === '') continue;
        $tmp = 0;
        foreach(str_split($s) as $c)
        {
            if ($c != " ")
            {
                break;
            }
            $tmp++;
        }
        if($removeIndent == 0 || $tmp < $removeIndent)
        {
            $removeIndent = $tmp;
        }
    }
    $codeFormatted = '';    
    foreach($codeLines as $s)
    {    
        $codeFormatted .= $spaces;
        $codeFormatted .= (strlen($s) >= $removeIndent ? substr_replace($s, '', 0, $removeIndent) : $s) . "\n";
    }

    return $codeFormatted;
}
share|improve this answer

I've written a little .NET tool to do that - it removes the leftmost indent (so when I copy/paste from Visual Studio I don't have to worry about some stuff already indented 3 or 4 times) and then adds 4 spaces. It's a simple MultiLine Textbox and a Button. I don't like the "Code" button on the toolbar as sometimes it messes up the indent, and it doesn't seem to remove the leftmost indent.

private void button2_Click(object sender, EventArgs e)
{
    int removeIndent = 0;
    foreach (string s in textBox1.Lines)
    {
        if (string.IsNullOrEmpty(s)) continue;
        s = s.Replace("\t","    "); // Tabs to 4 spaces
        int tmp = 0;
        foreach (char c in s)
        {
            if (c != ' ')
            {
                break;
            }
            tmp++;
        }
        if (removeIndent == 0 || tmp < removeIndent)
        {
            removeIndent = tmp;
        }
    }
    var sb = new StringBuilder();
    foreach (string s in textBox1.Lines)
    {
        sb.Append("    ");
        sb.AppendLine(s.Length >= removeIndent ? s.Remove(0, removeIndent) : s);
    }
    textBox1.Text = sb.ToString();
}
share|improve this answer
    
The code button decreases indent whenever I try it; I don't know why it wouldn't work for you. –  mmyers Jan 5 '10 at 18:25
1  
I think it doesn't like tabs vs. spaces or something, I had some issues where it wasn't symmetrical afterwards, and I can't stand unsymmetrical indent :) –  Michael Stum Jan 5 '10 at 18:28
1  
Michael - this is a really helpful utility! I ported your code (to PH-"don't hate me 'cause I'm ugly"-P ;). It's hosted at wittman.org/projects/stackoverflowindentfourspaces (open sourced, hosted at my site and github). Please let me know if you want any changes to the attribution - thanks! –  micahwittman Jan 7 '10 at 0:04
    
Cool. All the stuff on my blog is WTFPL v2 unless otherwise noted anyway :) sam.zoy.org/wtfpl –  Michael Stum Jan 7 '10 at 0:29
    
You may need to change that code though (can't access github's repository from here) to handle both Tabs and Spaces - some IDEs (like Visual Studio) always use spaces, even if you press TAB. The removeIndent variable is the minimum indent. The button2_click is in a class in a namespace, so that line is indented with 8 spaces. All other lines have at least 8 spaces, and some have more. so removeIndent is 8 as this is the indent one can safely remove from all lines. The s.Length >= removeIndent check is only there to preserve empty lines. –  Michael Stum Jan 7 '10 at 0:46
    
Basically I recommend adding an extra line to convert \t into 4 spaces. That way, you'll have consistent indent only consisting of spaces and can then check for the minimum indent. I've made a change to the above code. –  Michael Stum Jan 7 '10 at 0:47
1  
We've conversed on twitter already search.twitter.com/search?q=+from%3Amicahwittman+%40mstum , but for the sake of others: go.wittman.org/r492 now normalizes to spaces too - good call. Hope others find these useful. Cheers! –  micahwittman Jan 7 '10 at 8:10
    
Ok, answer with links to my PHP port on this page: meta.stackexchange.com/questions/34588/… –  micahwittman Jan 7 '10 at 9:32

Most people write out or paste in their code first, then select it, and click the code button. This provides the requisite four spaces for you, and preserves any other indents you set.

I'd agree with you on typing four space manually for each line, but that's why the button's there.

share|improve this answer

There are actually 2 is a way:

  • Select the whole block of xml (after you pasted it) en then click the code button.

  • Enclose your xml with <pre> and </pre> tags.

share|improve this answer
    
Doesn't <pre> strip out any tags inside it, though? –  mmyers Jan 5 '10 at 18:12
    
Apparently it does yes... still trying... damn... doesn't work... I thought I had seen it before. –  fretje Jan 5 '10 at 18:12
    
@fretje: you have to escape them: &lt; &amp; &gt; –  perbert Jan 12 '10 at 20:15

If you're on Linux or Mac, you can use this command for long files:

tr \n \n\t < filename.html | expand -t 4 > filewithspaces.html

This does also turn every tabulator \t into 4 spaces.

share|improve this answer
1  
Sure, but this is so much easier... –  Arjan Aug 10 '13 at 14:59
    
(Also, tabs are fine to format as a code block too.) –  Arjan Aug 10 '13 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .