# How can I prepare my code so that I can paste it formatted?

If I paste C++ code with many functions/classes (braces), I have to indent all of them, including their content. I've replaced tabs with spaces in my editor, but still no improvements. What's the best solution?

• Highlight your code and press ctrl+k. Jan 2, 2014 at 17:28
• @jliv902 I have to do it for every piece of code between braces Jan 2, 2014 at 18:05
• Is there some disadvantage to wrapping unaltered copy-pasta in <pre> and <code> tags that I'm unaware of? Jan 3, 2014 at 8:44
• Yes, it seems that your awareness is missing the tabs. It's easy to realize it if your read the following answer. Jan 3, 2014 at 14:08
• By "tabs" do you mean literally the ascii character 9 "\t" is ignored for purposes of formatting, or are four ascii 0x20 " " spaces ignored for purposes of formatting as well? I've been doing <pre><code> for a while and never noticed anything wrong, but I always replace tabs with spaces anyway. Jan 4, 2014 at 2:07
• By tab I mean '\t'. Another (firstly unexpected) use is to format the code of others (and win precious points). Jan 4, 2014 at 2:10
• Aaah I see. It seems I am not very altruistic. Jan 4, 2014 at 2:19
• Neither I am and, to be honest, I don't understand why " '\t' is ignored for purposes of formatting". I just had a problem and I wanted a solution. Jan 4, 2014 at 2:25
• Accepted answer still looks like a mild to moderate hassle - I'm going to try to cook up a bookmarklet. If anybody wants to come up with a list of tokens that demand an indent/dedent in languages other than curly-bracket languages, HTML/XML, and Ruby, they should get it to me somehow. Jan 4, 2014 at 9:21
• Both the question and the answer mention the language, from what you are saying I understand that one can't indent HTML code with tabs. But you're welcome to add another answer, maybe it il gain more points so it will come first (in the page order). Jan 4, 2014 at 12:48

Java is my standard language, but, for formatting code to put here, this is typically what I do, depending on where I am...

For example, someone posts a question to Stack Overflow, and the indenting/formatting is horrible....

First, I edit the post, then select the code that needs indenting/fixing.

Type CtrlX to cut it, then:

• Using Eclipse:

1. AltTab to get to Eclipse
2. AltShiftN Up,Up,Up,Up to start a new untitled text document
3. CtrlV into new document
4. CtrlA, CtrlI, Tab (select everything, make indenting consistent, increase all indent by 4 spaces).
5. CtrlC
6. CtrlF4 to close the untitled text document (don't save)
7. AltTab (copy it all, back to Stack Exchange)
8. CtrlV to paste fixed code back again.

1. Do the same as for Eclipse, to paste to a new document
2. CtrlA, Tab (can't do easily fix indenting, but can indent everything)
3. Edit->Blank Operations->Tab to Spaces
4. CtrlC
5. CtrlW to close file
6. AltTab back to Stack Overflow
7. CtrlV to paste fixed code back again

This works for most C-like languages (Java, C++, C#, whatever).

• Step 3 from Notepad++ worked with Sublime Text: View / Indentation / Convert Indentation to Spaces Jan 2, 2014 at 18:15
• wish I could upvote this again. this really saves a lot of time!!! I am using Notepad++
– Malachi Mod
Jan 2, 2014 at 19:48
• I use VIM: :%> will increase indentation, :%retab will repear tabs (et=expandtab needed or you can use %s/\t/ /g - will change all tabs to spaces)
– user52292
Oct 9, 2014 at 21:41

Some programs to do this have been submitted for review, so you might be able to use one of them: