10
\$\begingroup\$

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?

\$\endgroup\$
  • 3
    \$\begingroup\$ Highlight your code and press ctrl+k. \$\endgroup\$ – jliv902 Jan 2 '14 at 17:28
  • \$\begingroup\$ @jliv902 I have to do it for every piece of code between braces \$\endgroup\$ – Liviu Jan 2 '14 at 18:05
  • \$\begingroup\$ Is there some disadvantage to wrapping unaltered copy-pasta in <pre> and <code> tags that I'm unaware of? \$\endgroup\$ – sqykly Jan 3 '14 at 8:44
  • \$\begingroup\$ Yes, it seems that your awareness is missing the tabs. It's easy to realize it if your read the following answer. \$\endgroup\$ – Liviu Jan 3 '14 at 14:08
  • \$\begingroup\$ 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. \$\endgroup\$ – sqykly Jan 4 '14 at 2:07
  • \$\begingroup\$ By tab I mean '\t'. Another (firstly unexpected) use is to format the code of others (and win precious points). \$\endgroup\$ – Liviu Jan 4 '14 at 2:10
  • \$\begingroup\$ Aaah I see. It seems I am not very altruistic. \$\endgroup\$ – sqykly Jan 4 '14 at 2:19
  • \$\begingroup\$ 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. \$\endgroup\$ – Liviu Jan 4 '14 at 2:25
  • \$\begingroup\$ 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. \$\endgroup\$ – sqykly Jan 4 '14 at 9:21
  • \$\begingroup\$ 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). \$\endgroup\$ – Liviu Jan 4 '14 at 12:48
10
\$\begingroup\$

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.
  • Using Notepad++

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

\$\endgroup\$
  • 1
    \$\begingroup\$ Step 3 from Notepad++ worked with Sublime Text: View / Indentation / Convert Indentation to Spaces \$\endgroup\$ – Liviu Jan 2 '14 at 18:15
  • \$\begingroup\$ wish I could upvote this again. this really saves a lot of time!!! I am using Notepad++ \$\endgroup\$ – Malachi Jan 2 '14 at 19:48
  • \$\begingroup\$ 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) \$\endgroup\$ – firda Oct 9 '14 at 21:41
1
\$\begingroup\$

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

\$\endgroup\$

You must log in to answer this question.

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