8

Intro

There are a few posts related to this (namely Tabbing multiple lines of code on Stack Overflow, Tabbing multiple lines of code on Stack Overflow, and notably Markdown editor indent and outdent functionality), but they're all quite old and I don't think they really address the core issue (besides Markdown editor indent and outdent functionality).

Problem

People write a lot of code in Stack Overflow, in a lot of different ways. They write code by hand when asking questions and in answers, and they copy code from various sources (with various levels of indent and formatting) in questions and answers. They manually edit other peoples' questions and answers, to fix (often terrible) indentation/formatting.

There are a lot of problems with the current flow. It's painfully slow to manually add and delete spaces, especially when you're wanting to change the indentation of entire blocks (because of removing some nesting, or fixing some code you copied from a source with deep indentation).

Solution

The ability to tab (resulting in e.g., 4 spaces, not an actual tab character) single lines, tab multiple lines at once, and shift + tab to remove indentation of multiple lines would make my general flow of answering questions much easier.

Browsers have come a long way since those older posts, and there are plenty of instances of full IDEs in the browser that do this, and I'm not even asking for that. I just want this basic support for managing indentation (with some potential ways to improve it further, such as auto-preseving indentation on newlines). This answer to a similar post says that tabbing going to the next focus element is standard web semantics, and, while I agree (and supporting accessibility is important), this is a specialized domain with a disproportionately large amount of code that requires formatting. This "IDE-like mode" could be toggled, if some user would prefer to have the classic (imo terribly difficult to work with) tabbing behavior.

Example Demo

There is a project that allows for creation of text areas with rich tab indentation support. Here's a demo of it. Just play around with it a bit, and imagine how much easier it would be to answer various questions or fix other people's formatting with an interface like this!

(I'm in no way affiliated with that project, and just found it a few minutes ago when looking for an example)

tl;dr;

A richer text area that supports (or can be toggled to support) indentation control with tabbing and shift-tabbing multiple lines is, in my opinion, one of the largest missing features on Stack Overflow. This, while not trivial, can be implemented without a huge amount of work, and would be a huge quality of life improvement.

26
  • 1
    Hmm, honestly this seems viable! Let's see what the rest have to offer :3
    – DialFrost
    Oct 15, 2022 at 2:31
  • 2
    Related/duplicate request with actual tabs not 4 spaces Markdown editor indent and outdent functionality
    – Henry Ecker Mod
    Oct 15, 2022 at 2:38
  • @HenryEcker ah, I hadn't seen that one. I'd still like to leave this up, as that one's quite old and nothing ever came of it, despite the top answer being in agreement. What is the process for feature requests like this actually going into the dev pipeline?
    – Nathan
    Oct 15, 2022 at 2:40
  • Also, if we can get enough current support for this, we could potentially band together and create a browser extension to serve in the meantime!
    – Nathan
    Oct 15, 2022 at 2:43
  • @Nathan This seems like a good idea to me, but I'm not sure how much work might need to put in into adding this (idk lol) But we'll see!
    – DialFrost
    Oct 15, 2022 at 2:47
  • Actually, looking more into that other post, the author created a JS snippet to do just this for text areas (here). Building a basic browser extension for this, that hooks into all text areas on stack exchange sites, really wouldn't be that hard. Anyone interested in working with me on that? Not sure if I have the time/motivation for it currently, but I'll definitely add it to my backlog.
    – Nathan
    Oct 15, 2022 at 2:49
  • 3
    This already exists in userscripts. Personally, I get it from Magic™ Editor (GitHub) (install), which gets it by including tab-editing.user.js, which is a fork of "Better handling of indentation and the TAB key when editing posts".
    – Makyen Mod
    Oct 15, 2022 at 3:14
  • @Makyen ooph I just spent the last 30 mins building an extension scaffolding, but this is good to know. I just installed Tampermonkey and tried both Magic(TM Editor) and tab-editing.user.js directly, and neither seemed to work? Tampermonkey shows the script is active on the page, but I don't see any different tab behavior.
    – Nathan
    Oct 15, 2022 at 3:26
  • @Nathan Magic Editor works for me in Firefox with a test profile with Tampermonkey installed with no other userscripts, just clicking on the above "install" link and then editing an SO post. I haven't actually tried "tab-editing.user.js" on its own, as the functionality is fully included in Magic Editor, which I've had installed for a long time. Did you install both userscripts at the same time? Only one should be installed/enabled at a time.
    – Makyen Mod
    Oct 15, 2022 at 3:44
  • @Makyen I just had Magic Editor installed by itself, and it didn't work. All I saw was a magic wand in the edit window, and when I clicked it the whole text flashed and went white. No matter, I'm going to make an extension anyways, as that's more accessible for most people, and it seems fun. I've gotten it working with Parcel and TypeScript, and hope to make it cross-browser compatible.
    – Nathan
    Oct 15, 2022 at 3:52
  • There should be no need to click on the wand icon to use the Tab key.
    – Makyen Mod
    Oct 15, 2022 at 3:54
  • Yeah, I inferred they were separate, just explaining my experience (where the wand aspect at least seems to be broken), and where tab behavior doesn't seem changed.
    – Nathan
    Oct 15, 2022 at 4:01
  • 1
    Ahhh... That's a dark-theme/light-theme issue. It looks like some CSS needs to change (and that there's a PR for it which needs review wrt. if the requested changes were made).
    – Makyen Mod
    Oct 15, 2022 at 4:22
  • 4
    Just FYI, Stack Editor, the future of Stack Exchange's text input form is open-sourced.
    – Andrew T.
    Oct 15, 2022 at 4:26
  • 1
    It only applies for answers on MSO and MSE currently. @Nathan
    – Henry Ecker Mod
    Oct 15, 2022 at 4:46

1 Answer 1

2

With the advent of the triple backticks/code fences to format code, the concern around formatting your code to be perfect with the sometimes finicky Markdown script to do this originally is greatly reduced.

To further the point and explain why I'm against a proposal like this, remember the scope of the Markdown editor in the first place. The point is to communicate and show your code. Yes, it'd be nice if someone formatted it nicely to begin with, and if you're in Python, formatting is key, but we've always had the ability to just...edit it...if we felt like it could be improved. Any number of small text editors let us do just that.

I'm not sure there's any need in adding more power around editing code when the point isn't to do a whole lot with the code in the first place; just bring enough for us to be able to get into context and understand what they're asking in the first place.

7
  • 3
    I still like the idea of having a more functional editor, but I am also very pro-code-fences. Oct 15, 2022 at 4:48
  • 1
    I use code fences all the time; they are indeed easier. However, code (especially Python, which is my main language on the site) depends on indentation for readability (and functionality in Python). This editor proposal would be very useful.
    – pigrammer
    Oct 15, 2022 at 13:48
  • 1
    @pigrammer: In what context, exactly? If you're just formatting the code, you could always copy it to your machine and do that there. Do you do the editing on mobile, something that even though I love using mobile, something that I wouldn't really consider doing on a lot of stuff?
    – Makoto
    Oct 16, 2022 at 4:13
  • 1
    @pigrammer I am actually concerned that things would be worse for Python. Since reproducing indentation accurately, not correctly, is required for many issues too much cleverness in handling indentation would be harmful. Likewise, for other languages many desirable IDE features rely on valid code and may thus conflict with the kind of code we need here. Oct 16, 2022 at 7:00
  • @Makoto Sometimes, I just need to add one line to a piece of code. In that case, I'd prefer to be able to edit it directly in SO.
    – pigrammer
    Oct 16, 2022 at 11:05
  • 1
    @MisterMiyagi I don't think we need sophisticated indentation. Just a simple "when I press Enter, keep the previous line's indentation.
    – pigrammer
    Oct 16, 2022 at 11:06
  • And anyways, this is a moot point, as the new Stacks Editor has this feature.
    – pigrammer
    Oct 16, 2022 at 11:08

You must log in to answer this question.

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