13

Would it be too difficult to have an indentation feature in the text area for questions and answers?

Perhaps next to the undo/redo buttons you could have these buttons :

enter image description here

I am getting increasingly frustrated when pasting code into my questions and answers. Sometimes the code indentation is just a tiny tiny little bit out-of-whack (eg. one method is missing one level of indentation). And I just need to indent those lines. I know SO is not supposed to be a text editor, but the lack of this feature bugs me the most.

7
  • 4
    Related: Let's have a “Tidy Up” button! Commented Jul 21, 2014 at 8:50
  • This will also help when trying to place code within a list. Unfortunately, variants of this feature have been suggested multiple times, and SE have always been very reluctant to change it.
    – Matt
    Commented Jul 21, 2014 at 8:51
  • 1
    I feel it would give me 0 benefit when writing posts, but I agree it might help me edit posts that the original user has already screwed up the indentation of - but I tend to walk away from those unless it's just a couple of lines. ±0 from me
    – OGHaza
    Commented Jul 21, 2014 at 8:54
  • 2
    This could also potentially break many python questions (and every other language where indentation is relevant, e.g. haskell). It's already quite hard sometimes to figure out the correct indentation with people messing it up all by themselves, I can only imagine it getting harder if such a tool would be introduced...
    – l4mpi
    Commented Jul 21, 2014 at 9:09
  • 1
    This is a multidupe, but probably all of the originals are over on Meta Stack Exchange. You can kinda simulate it by highlighting all code and hitting ctrl-k. This works to un-over-indent code from morons who paste and don't clean up their own sheet.
    – user1228
    Commented Jul 21, 2014 at 15:41
  • I don't think this would be a useful feature. In general code shouldn't be pages long in the first place, and fixing it by hand is usually not a huge task. Selecting code and clicking the code button already indents the code with 4 spaces.
    – Sumurai8
    Commented Aug 24, 2014 at 7:01
  • Increase/Decrease Indentation Buttons for the Editor
    – Cody Gray Mod
    Commented Aug 24, 2014 at 7:59

2 Answers 2

3

So I feel your pain Oliver and I decided to see if I could whip up a bookmarklet (a JavaScript bookmark) that would fulfill the role while the StackOverflow/StackExchange folks debate the worthyness of such a feature ;-)

While I agree that formatting Python code might break things and that StackOverflow might not want to incorporate this to stop seemingly helpful SO users from fixing badly formatted code (when those OP's should learn to write cleaner code)... there are SO users like myself that do post lots of code answers and just want to indent our own code blocks the 4 spaces required by markdown to have the code recognized and thus formatted correctly.

I've posted the raw code up on GitHub: https://github.com/scunliffe/StackOverflowCodeIndent and I'm going to attempt to inline the bookmarklet below:

SORRY, JS based links are blocked on StackExchange sites I've hosted it here for easy access: http://scunliffe.github.io/SOCodeIndent.html

When you are ready to indent your code... highlight your block of code and click the bookmarklet - presto!

Notes:

  1. It indents every line of the selection with 4 spaces
  2. It converts any/all leading tabs to 2 spaces
  3. It does not alter the code in any other way (e.g. adjust bracket whitespace or convert braces to K&R style)
  4. It isn't currently smart enough to not re-indent if you re-select a block of code that is already 4-space indented
-1

The reason I think this feature would be important is that often I paste in code snippets.

While the I generally agree that code should be compiled, cleaned up, and formatted in IDE before sending to SO, extra work needs to be done to a snippet that is deeply indented. I need to copy the indented snippet into a new javascript file, and then re-format so the indendation disappears. That is an annoying amount of work IMO for just a tiny snippet.

You must log in to answer this question.

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