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?
2 Answers
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).
-
1\$\begingroup\$ Step 3 from Notepad++ worked with Sublime Text:
View / Indentation / Convert Indentation to Spaces
\$\endgroup\$– LiviuCommented Jan 2, 2014 at 18:15 -
\$\begingroup\$ wish I could upvote this again. this really saves a lot of time!!! I am using Notepad++ \$\endgroup\$– MalachiCommented Jan 2, 2014 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\$– user52292Commented 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:
<pre>
and<code>
tags that I'm unaware of? \$\endgroup\$<pre><code>
for a while and never noticed anything wrong, but I always replace tabs with spaces anyway. \$\endgroup\$