Take the 2-minute tour ×
Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. It's 100% free, no registration required.

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

Use one of these two links to install:

share|improve this question
    
Question: (awesome script by the way!) how does the auto-update feature work around the same-domain restriction for localStorage? –  Nathan Osman Mar 23 '12 at 22:20
    
It doesn't. It checks for updates on every SE site you visit. –  balpha Mar 23 '12 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? –  Spontifixus Sep 26 '12 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 Jan 4 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 Jan 10 at 17:56
    
@joeytje50 Esc already has a meaning during editing, so this is out of the question. –  balpha Jan 10 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 Jan 10 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 Jan 11 at 7:09
2  
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. –  Carl Bussema May 23 at 18:48
    
Or install the script manually stackapps.com/tags/script/info –  Dan Jun 19 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 Jun 27 at 9:26
add comment

3 Answers 3

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.

share|improve this answer
    
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 Mar 28 '12 at 6:03
add comment

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.

share|improve this answer
add comment

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.)

share|improve this answer
    
An option to switch between spaces and tab would also be nice! :) –  Kyle_the_hacker Jul 26 '13 at 1:35
    
@Kyle_the_hacker Would it? Stack Exchange sits only recognise spaces for indenting code. –  Duncan Jul 26 '13 at 6:44
    
@Duncan_Jones: No it don't only support spaces, but also tabs. Just try. ;) –  Kyle_the_hacker Jul 26 '13 at 9:17
    
@Kyle_the_hacker You are correct! –  Duncan Jul 26 '13 at 9:58
add comment

You must log in to answer this question.

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