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

It's getting quite frustrating to have to have to edit every single line of a large code block to remove tabs, and replace them with whitespaces. While numerous posts talk about this, and it's marked status-completed, I don't think the Team understands what's going on.

It's true that the parser will convert the tabs to spaces, but it doesn't do it well. Just try and edit a huge crappily formatted code block, and you'll see in the preview what it's doing, and nothing lines up right. The only way to fix these posts in the end is to delete the whitespace on the front of every line, and then space one at a time until you have the line positioned where you want it. You have to do this to every single line.

If we can't get the markup engine to take care of this, then how about getting us a tool in the editor that will zap the tabs into spaces. That way instead of backing up to the beginning of the line (for every line) we can back up to where we want the line and stop there.

I'm sure there might be other ways to do it also, please contribute an answer if you have an idea.

The thrill is gone from doing ten minute edits to get the codeblocks right, and my spacebar is going on this keyboard, so it's getting worse.

share|improve this question
3  
+1, I seem to be copying a lot of source to TextPad to format. –  user7116 Jun 16 '11 at 15:24
2  
I usually copy the post into a text editor and find/replace tab with space, but it'd be a useful tool in the built-in editor –  Michael Mrozek Jun 16 '11 at 15:25

3 Answers 3

up vote 2 down vote accepted

I got really tired of this today and basically fixed the problem with a few lines of userscript (install link).

This adds a \t button to the post edit toolbar that when clicked replaces all occurrences of literal tabs with 4 spaces. It's much faster than manually reindenting.

I've tested this script in Chrome 12.0.742.100, and it seems to work without any issues. OTOH, it seems patchy in FireFox 4.0.1 and 5.0 (tried both). There's a weird issue with the way the page load events occur and when Greasemonkey calls the userscript, the gist is that if you change $(document).ready to $(window).load everything should be dandy. I couldn't find a way to test which situation I'm in, so for now the script defaults to the working behavior in Chrome. I'm sure SO will have an answer in the near future, and hopefully the script will support both browsers equally well.

[aside] It seems that all tabs ought to just get replaced with 4 spaces (no matter what) on initial post ingest.

share|improve this answer
    
hmmm, it didn't see the \t (FF 4.0.1) –  Lance Roberts Jun 29 '11 at 5:35
    
@Lance tested with Chrome, will see what's up as soon as I finish walking home. –  Mark Elliot Jun 29 '11 at 5:42
    
Ok, it works in SO, just didn't work in Meta, I'll mod the script in my browser, thanks. –  Lance Roberts Jun 29 '11 at 6:11
    
I noticed today when I needed it that it wasn't working (FF 5.0). I wonder if it has to do with the changes they made to editing? The link is still the same though. –  Lance Roberts Jul 10 '11 at 18:48
    
@Lance, the changes make the script incompatible; since more changes are expected I've held off updating. If I have time later I'll push a change that should fix it in the interim. –  Mark Elliot Jul 10 '11 at 22:01

well, let's see.

Spaces

code using spaces for indentation

Code
    code indented 1 tab
    ohai, i get error 1417 wot do i do?

        code indented 2 tabs
        i haz a doubt

Mixed spaces and tabs

code using tabs and spaces for indentation

Code
        code indented 1 tab
        ohai, i get error 1417 wot do i do?

            code indented 2 tabs
            i haz a doubt

Tabs

code using tabs for indentation

Code
    code indented 1 tab
    ohai, i get error 1417 wot do i do?

        code indented 2 tabs
        i haz a doubt
share|improve this answer
7  
Creepy how familiar your example text looks to me. –  Octavian Damiean Jun 16 '11 at 8:36
2  
So apparently, by your example, the problem comes from mixed spaces and tabs (which I would believe). So that's where the fix needs to come in. –  Lance Roberts Jun 16 '11 at 14:38
3  
What does this answer say? How does it help? The problem is when editing I cannot type tabs in my browser (because the tab key is bound to the browser controls), so I do all editing with spaces. And editing happens only to posts that are poorly formatted to start with, which more often than not come with tabs. –  Kerrek SB Oct 4 '12 at 17:51

I'm absolutely in favour of such a tool, and quite urgently so.

Let me explain.

The need to edit a post extensively arises mainly from posts that are poorly presented in the first place. Unfortunately, more often than not, such posts use tabs. Now, when editing in my web browser, I can't use Tab comfortably because it's bound to my brower's controls.

So now the issue of mixing styles becomes a problem. Let me repeat that there is no problem with well-formatted tabs-only posts. Those are fine. The problem are those posts that aren't fine: I can't edit them with tabs, and to edit them at all I need to use spaces. Basically, this means I have to convert everything to spaces manually (or by using an external tool).

For those heavy editing jobs, and those only, I would absolutely welcome a tool built into the rich editor that does that job for me. And it would allow me to make a lot more poorly-presented questions look nice and get more attention, and have fun while doing it.

Thanks!

share|improve this answer

You must log in to answer this question.

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