# How do you efficiently paste code from your IDE, if it contains tab characters

It has been said before, that the SO editor is great, but has some drawbacks when pasting code which contains tab characters, see e.g. Improvements to editor for formatting source code

That's alright for the time being, but how do you work around it? Are there any quick tools that allow to replace all tab characters in the clipboard with spaces? Or maybe some web based tools where you paste the code in a textbox, and get a re-formatted result?

I see people editing large posts that contain messy code formatting into something beautiful - I can't believe you're doing this all manually?

I use the firefox plugin "It's All Text!" (which invokes your editor of choice on a text input box) to edit any sizeable block of code in SO posts, and in so doing I can use whatever editor commands I like to massage the content so it is suitable for posting - e.g. indent it by four columns, substitute special characters and tabs, break long lines, etc etc.

• This looks really, really nice, and is very much, what I'm looking for! Thanks. – Chris Lercher Jun 8 '10 at 23:17
• I copy and paste into Notepad, then run a find-and-replace to convert tabs to spaces. Indentation I handle manually. (This is if I don't have an IDE open, which sadly has been the case for some time.) – mmyers Jun 8 '10 at 23:41

You're a programmer, write a program! ;) (You may prefer a script or macro in your editor.)

text = """\
<PASTE TEXT HERE>
"""

four_sp = " " * 4
print four_sp + text.rstrip().replace("\t", "  ").replace("\n", "\n" + four_sp)


(That codepad.org link is saved so it won't expire, feel free to bookmark it.)

Of course, if you'd just use spaces for indentation this wouldn't be a problem... I used to be a diehard fan of tabs, but it took me about a decade to finally see they just cause more problems than they solve, at least for source code.

• Maybe I should really reconsider to use spaces! Maybe I'm too addicted the fact, that I can delete an entire tab by just one keystroke. – Chris Lercher Jun 8 '10 at 23:00
• @chris: You can delete an entire indentation level, using spaces, in any good and properly configured editor. – Gnome Jun 8 '10 at 23:01
• @TheCat: You're right, I just found out, that I can also do that in my editor. It will require me to get used to hit "Shift-Tab" instead of backspace - but I'm really going to consider it. – Chris Lercher Jun 8 '10 at 23:20
• @chris: Look into getting backspace to do that too (even post on SO as it's a programming tool); it's a common feature (e.g. canonical programmer's editors like vim and emacs) and I also find backspace natural for that. – Gnome Jun 8 '10 at 23:24

I just copy and paste it. Markdown handles the rest. See Jeff's answer here for more details.

I'll repaste tabs here, just to show again.

Oarfish
Raven
Sunflower

• The biggest problems arise, when I paste a block, that is already (deeply) indented. Then, when I want to insert something in the pasted block, it doesn't align correctly. Then I find myself replacing all tabs with spaces manually. – Chris Lercher Jun 8 '10 at 23:02
• The tabs are still tabs in the post, actually. They're just rendered as spaces and treated like them for code blocks. If you could view source or edit my post, you could see it. Of course, you can't enter tabs directly into Stack Overflow, so I generally move everything back to my text editor (Cream, as it were). @chris_l – Grace Note Jun 8 '10 at 23:03

I don't use tabs. But if I did, my editor allows for quick and painless conversion to spaces. I suggest you find a similar editor...