22

After some discussion on this question I decided to make a feature-request (or at least a discussion about it).

It would be very nice to be able to use Tab and Shift+Tab to move code blocks (like in any major IDE).

The most important reason for this request/discussion is that Ctrl+K does not give the desired result/behavior.

Looking forward to your opinions on this.

26
  • 3
    Because you are not really supposed to editing code on the site.. Only posting it. Write it and format it in your favorite IDE and just paste it into the post...
    – Lix
    Commented Apr 3, 2013 at 13:35
  • 5
    So, it's OK to break normal tabbing in the browser (links, form elements etc)?
    – Oded
    Commented Apr 3, 2013 at 13:35
  • 2
    I'm in favor of this. As long as it's only in the question/answer text field. Commented Apr 3, 2013 at 13:35
  • 2
    @Lix What a due respect, but i do spend quite a lot of time on SO editing anwers because people fail to post it correctly..
    – Rob
    Commented Apr 3, 2013 at 13:36
  • 1
    I support this! This is so useful Commented Apr 3, 2013 at 13:38
  • 4
    I'm not dedicated enough to open up an IDE just to fix a random person's code indentation.
    – JJJ
    Commented Apr 3, 2013 at 13:39
  • 1
    How about the best of both worlds - have it off by default, but include a checkbox "Allow Tabs to format code". Commented Apr 3, 2013 at 13:43
  • 11
    No, please don't highjack the tab key. It already has a well-established use in the browser. Fix Ctrl+K and add Ctrl+Shift+K (or other shortcut keys) instead. Commented Apr 3, 2013 at 13:44
  • 1
    @rob - what about using the right (as opposed to left) Ctrl button? That makes it very one-handish :P
    – Lix
    Commented Apr 3, 2013 at 13:49
  • 1
    I have a related Feature-Request on MSO: Can we get a button in the post editor to toggle the Tab key being used as 4 Spaces?
    – Rachel
    Commented Apr 3, 2013 at 13:50
  • 2
    @rob - tisk tisk... Real programmers never leave the home row ;)
    – Lix
    Commented Apr 3, 2013 at 13:51
  • 3
    @hims056 I think it could be better. Press Ctrl + K again to indent more, Ctrl + Shift + K to undo one level of indent. Commented Apr 3, 2013 at 14:07
  • 1
    @BilltheLizard I never use Ctrl+K because I typically have my right hand on my mouse, and my left hand on the left side of the keyboard, while the K key is on the right side of my keyboard. It would be much easier to use if whatever shortcut key is used is on the right side of the keyboard.
    – Rachel
    Commented Apr 3, 2013 at 14:14
  • 3
    @Rachel I think they picked Ctrl + K because most of the left-hand keys already have a default shortcut setting in most browsers. If they can find some other shortcut that's not used I'd be fine with it. (Even just adding "indent more" and "indent less" buttons in the formatting row above the editor window would be fine with me. I can use the mouse.) Commented Apr 3, 2013 at 14:22
  • 1
    I'd be down for having a checkbox or a user setting to force it to use the tab key. I'm sure many people like me never use the tab for its default purpose. Commented Apr 3, 2013 at 14:40

3 Answers 3

8

Though this is not a native StackExchange feature, there's a user script on StackApps that does exactly what you are looking for:

Better handling of indentation and the TAB key when editing posts

This user script changes the behavior of a few keys (most notably the Tab key) within the post editor to behave more like it does in IDEs or text editors:

  • When multiple lines are selected, Tab and Shift-Tab indent and dedent these lines
  • When nothing is selected, Tab and Shift-Tab insert or remove whitespace to align the cursor on a tab boundary
  • When the cursor is within the left margin of a line, Backspace removes whitespace to align the cursor on a tab boundary (in other words, it may delete more than just one space character)
  • On indented lines, the Home key toggles the cursor between the actual beginning of the line and the beginning of the real content (in other words, it jumps back and forth to before and after the leading whitespace). This only happens on lines that are indented by at least four spaces or a tab, since it can be confusing for the following reason: When you press Home in the text editor, you expect the cursor to jump to the beginning of the line as it is displayed, which (due to wrapping) may be different from the actual previous newline character.
  • So you don't have to reach for the mouse to tab out of the editor you can press and release the Ctrl key, and the next key press will not be intercepted; thus Tab takes you out of the editor. Pressing and releasing Ctrl will grey out the text editor until the next keystroke to clarify this. If you think this is too awkward, I'm open to other suggestions, but there should be some way to tab out of the editor .

Note that this will never insert TAB characters, only spaces. It does however handle already-present TABs, and it handles them the same way the Markdown converter does.

0

A "format code" tool would imply

  1. knowing how to format many different languages
  2. picking a indentation style

The first is a hassle, the second an invitation to holy wars.

You shouldn't be typing significant bits of code directly into SO anyway, as you will generate spurious errors: cut and paste the code that you ran to show how it doesn't do what you hoped.

Format in your editor or IDE, already.

0

As has been said before, if you make Tab indent instead of moving between fields, you are breaking a fundamental UI feature. Please don't do this. Make Ctrl+K indent instead. Yes, it's less discoverable, but it beats breaking moving between fields. The problem with Ctrl+K is that it only supports a single level (which breaks in lists, in particular) — so fix that. Do not change the functionality of the Tab key.

1
  • Note that GitHub is handling (un)indentation with Tab and Shift+Tab for a while now and without any accessibility issue (because only when text is selected). This is now kind of a keyboard shortcut "standard".
    – Yves M.
    Commented Jul 2, 2022 at 10:45

You must log in to answer this question.

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