Title: How can I prepare my code so that I can paste it formatted?
Tags: support;faq;formatting;editor
Question: 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?

Comment: Highlight your code and press ctrl+k.

Comment: @jliv902 I have to do it for every piece of code between braces

Comment: Is there some disadvantage to wrapping unaltered copy-pasta in `` and ````` tags that I'm unaware of?

Comment: Yes, it seems that your awareness is missing the tabs. It's easy to realize it if your read the following answer.

Comment: 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 ````` for a while and never noticed anything wrong, but I always replace tabs with spaces anyway.

Comment: By tab I mean '\t'. Another (firstly unexpected) use is to format the code of others (and win precious points).

Comment: Aaah I see.  It seems I am not very altruistic.

Comment: 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.

Comment: 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.

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

Here is the accepted answer: 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:


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

Using Notepad++


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



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

Comment for this answer: Step 3 from Notepad++ worked with Sublime Text: `View / Indentation / Convert Indentation to Spaces`
Comment for this answer: wish I could upvote this again.  this really saves a lot of time!!! I am using Notepad++
Comment for this answer: 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)

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

java: Tool for creating CodeReview questions

shell: Shell script for creating CR questions

javascript: Tool for automatically correcting indentation and formatting of CR &amp; SO code
This last one is also on JSFiddle




{'source': 'StackExchangeDataset', 'subset': 'codereview.meta.stackexchange.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 10, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:01/02/14', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<support><faq><formatting><editor>', 'Title': 'How can I prepare my code so that I can paste it formatted?', 'ViewCount': 295, 'id': 1376, 'user_AboutMe': None, 'user_AccountId': 1022237.0, 'user_DisplayName': 'Liviu', 'user_Reputation': 131.0, 'user_Views': 9.0, 'user_id': 25610.0}}