90

indenting

About

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.

Feedback very welcome, except for discussions about a) tabs vs. spaces, and b) tab width :)

Download

Click this link to install it [Archive link 2017-03-18]

16
  • Question: (awesome script by the way!) how does the auto-update feature work around the same-domain restriction for localStorage? Commented Mar 23, 2012 at 22:20
  • It doesn't. It checks for updates on every SE site you visit.
    – balpha Staff
    Commented Mar 23, 2012 at 22:21
  • Great script, when creating questions. I tried to use its features when editing a question accessed through the review-beta system, with no success. Is this the right place to file this bug? Commented Sep 26, 2012 at 15:17
  • @balpha Great script! I'm bookmarking it. How about auto indentation? It will be great if, when I press Enter, the new line can be indented automatically to match the previous line's indentation. As a bonus, it could also extra-indent the line after opening brackets by one step and de-indent closing brackets by one step.
    – ADTC
    Commented Jan 4, 2014 at 20:56
  • I'd like to suggest dropping the <kbd>ctrl</kbd> key triggering if the tab is sent to the browser or not, and instead using the <kbd>esc</kbd> key. People won't unintentionally press the esc key in everyday editing, so that would seem like a much better key to trigger this on imo.
    – joeytje50
    Commented Jan 10, 2014 at 17:56
  • @joeytje50 Esc already has a meaning during editing, so this is out of the question.
    – balpha Staff
    Commented Jan 10, 2014 at 19:10
  • @balpha What does it do then? I tried googling this, but that came up with questions on SO about binding the esc key, and searching on meta.SO didn't help either.
    – joeytje50
    Commented Jan 10, 2014 at 23:17
  • @joeytje50 What the Esc key is there for -- it cancels the editing. Same as clicking the "cancel" link. Note that I'm talking about editing, not suggesting edits; the latter has no cancel link because it doesn't need one (since it doesn't happen inline, but on a new page).
    – balpha Staff
    Commented Jan 11, 2014 at 7:09
  • 4
    Note that as of Chrome 35 (late May 2014) you can't install user scripts that aren't in the web store, even if you download them locally. Get TamperMonkey as a workaround, but the author may wish to add the script to the Chrome Web Store for easier installation for Chrome users. Commented May 23, 2014 at 18:48
  • Or install the script manually stackapps.com/tags/script/info
    – Dan
    Commented Jun 19, 2014 at 17:23
  • @Dan You can't, as of Chrome 35. You need to use TamperMonkey. @balpha You may want to add that Shift-Tab accessibility shortcut can be achieved (when using your add-on) as follows: 1. Press and hold down Shift key. 2. Press and release Ctrl while still holding down Shift (text area becomes grey). 3. Press the Tab key. The Shift-Tab action will be performed. You can now release the Shift key.
    – ADTC
    Commented Jun 27, 2014 at 9:26
  • 1
    @KonradRudolph I have given up on installing user scripts directly as Chrome extensions. I use TamperMonkey instead now.
    – balpha Staff
    Commented Mar 17, 2016 at 13:23
  • 2
    This userscript has stopped working since SO switched to https. I've created a pullrequest here that fixes this problem.
    – Tot Zam
    Commented Jul 28, 2017 at 5:17
  • 1
    @TotZam All the links in this post are dead. Do you currently have a working version installed that you can put up somewhere? The license looks to be MIT, so it should be OK Commented Sep 6, 2020 at 16:25
  • 1
    @CertainPerformance I don't and it looks like I just have a stale pull request in my BitBucket account. While it appears that the OP no longer works at SO, it looks like he is still an active user, so I would say it's up to him if he wants to make that code available to the community again.
    – Tot Zam
    Commented Sep 6, 2020 at 17:38

6 Answers 6

15

Inheriting indention when pressing enter

What I'd like to suggest is that if you press enter, the indentation is copied from the line you're coming from, and then automatically put before the next line. This would make it a bit easier to type out code on SO. Of course this would create the problem that people who would want to end their code would be required to press shift+tab or backspace, so to fix that, you could make it that it stops inheriting previous indentation when pressing enter with only whitespace on that line. For example, with the things between [] being inserted by the script; ⇆ Tab, ↵ Enter and ← Backspace:

[⇆ ]lorem Ipsum(dolor) {
[↵ ]    sit amet;
[↵←]}
[↵ ]|

and after pressing enter again, it inserts a newline, but removes the previously added indentation, so pressing enter again would change that to the following:

[⇆ ]lorem Ipsum(dolor) {
[↵ ]    sit amet;
[↵←]}

|

So in summary, it would do this when pressing enter:

  • ...insert the exact same amount of indentation as the previous line had
  • ...and only whitespace exists on the line, remove that whitespace and insert an enter

Other keys triggering default behaviour

I'd like to suggest using either alt+shift or ctrl+alt the esc key instead of ctrl. It would happen a lot less that people would use esc without actually meaning to do this. The ctrl key is used a lot in other situations, such as copy/pasting, backspacing full words, etc. In those situations where you use ctrl it might happen (and does regularly happen) that you press the ctrl key without actually intending to trigger the white-out.

4

This uses the jQuery .on(...) feature which is only available in 1.7 and up. Careers is still using an older version and you get an exception on every page right now with this installed.

We will be updating soon, but it would be nice if you could add a config page to turn it on/off for each site.

1
  • 1
    Good point; I've added a check to make sure it's only run on a plain ol' Stack Exchange Q&A site. It wouldn't be a problem to convert this to work with jQuery 1.5, but that isn't really necessary.
    – balpha Staff
    Commented Mar 28, 2012 at 6:03
4

It would be nice to have a script option to adjust the indentation size. I, for one, would probably find an indent size of two spaces helpful.

(Many questions I'm editing to correct indentation are not aligned nicely on four-space indents.)

4
  • An option to switch between spaces and tab would also be nice! :) Commented Jul 26, 2013 at 1:35
  • 3
    @Kyle_the_hacker Would it? Stack Exchange sits only recognise spaces for indenting code. Commented Jul 26, 2013 at 6:44
  • @Duncan_Jones: No it don't only support spaces, but also tabs. Just try. ;) Commented Jul 26, 2013 at 9:17
  • @Kyle_the_hacker You are correct! Commented Jul 26, 2013 at 9:58
4

With the recent network-wide HTTPS switch, the include's should change to:

// @include        https://stackoverflow.com/*
// @include        https://*.stackoverflow.com/*

Also, in case of editing a question in the review queues, I noticed that the script wasn't working. The problem was in this line:

$("#mainbar").on("keydown", ".wmd-input", function (evt) { ...

As one goes through various posts in the queue, I suspect the mainbar element itself would be removed/replaced. A fix to that would be:

$("#content").on("keydown", ".wmd-input", function (evt) { ...

I suspect that the other event listener might need to be changed similarly for Ctrl + Tab combo.

1
  • Better off using @match so that http/https and domain+subdomains can all be covered by a single rule per domain. e.g. //@match *://*.stackoverflow.com/* covers Stack Overflow & Meta Stack Overflow, and //@match *://*.stackexchange.com/* covers all sites at subdomain.stackexchange.com, and their meta sites.
    – 3D1T0R
    Commented Jun 26, 2018 at 18:49
0

I changed the @includes in the script header for the following simplified @matches:

// @match           *://*.askubuntu.com/questions*
// @match           *://*.mathoverflow.net/questions*
// @match           *://*.serverfault.com/questions*
// @match           *://*.stackapps.com/questions*
// @match           *://*.stackexchange.com/questions*
// @match           *://*.stackoverflow.com/questions*
// @match           *://*.superuser.com/questions*
// @exclude         http://chat.*
// @exclude         http://blog.*
// @exclude         http://careers.stackoverflow.com.*

Those will limit the script only to pages under /questions and to restrict only to individual questions, changed the following check at the start:

// are we really really on a Stack Exchange site?
if (!(window.StackExchange && StackExchange.options.routeName.indexOf('Questions/Show') !== -1 ))
    return;
1
0

Tabbing out of the editor

While tapping ctrl and then tab works well for tabbing out of the editor, i'd like to suggest an improvement. Unless the cursor is at the beginning of the line, it should tab out. Intending should only occur at the beginning of the line.

2
  • 3
    Indenting yes, but there are other valid reasons for tabbing, e.g. tabular data, lining up comments, etc.
    – balpha Staff
    Commented Oct 28, 2014 at 14:15
  • So how about automatically tabbing out if it's at the very end of the textbox?
    – Scimonster
    Commented Oct 28, 2014 at 14:27

You must log in to answer this question.

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