12

icon

About

Here's yet another Greasemonkey / Tampermonkey userscript for Stack Exchange. It adds the following enhancements:

  1. Within preformatted code blocks, double-click to select all.

  2. In the post editor, this script mimics the behavior of Notepad++ with Tab, Home, End, and Enter.

    • The Tab key inserts \t. If multiple lines are highlighted, indent all included lines within the selection.
    • Shift+Tab outdents. If selection doesn't span multiple lines, move cursor backward one tab stop toward the beginning of text, or outdent if already at the beginning of text.
    • Home moves the cursor to the previous line wrap, with a double tap moving to the beginning of the line.
    • Likewise, End moves the cursor to the next wrap, then to the next \n.
    • Enter continues the previous line's indentation.
    • Ctrl+Enter submits the edit.

Why?

My inspirations for writing this script were the Select Code Block Buttons script and Better handling of indentation and the TAB key.... Both scripts come close, but neither behaves exactly how I expect. The "hover over code block to reveal a select all button" script is a nice idea, but I like a bigger target, and the version I tried from that page interfered with adding comments. Some purists might also appreciate that this double-click method adds no buttons or other visual controls to besmirch the clean UI. The "better handling of indentation" script was also a nice idea, but I felt I could (and believe I did) do it better, making the behavior more closely resemble Notepad++ and adding indent inheritance for carriage returns. And both of those scripts rely on jQuery, whereas mine requires no external libraries. This script is fast enough that I never notice it calculating, even when editing this fairly sizeable question.

Where?

Install Greasemonkey for Firefox or Tampermonkey for Chrome if you haven't already.

Download this script from GitHub. (direct link)

Bug reports / Feature requests

If you find a bug or have a feature request, feel free to post the request as an answer. I can't guarantee I'll implement your request, as this is a script I use to add behavior that I like, which may or may not be what the masses like. I'll consider adding your feature if it's something from which I would personally benefit. Or if the request gets enough upvotes that it would be embarrassing for me to continue ignoring it, I'll add it if I can. Otherwise, it might be quicker for you to learn JavaScript and write it yourself.

Then why is this page here?

Because I worked for several hours on this script, and I thought someone else might get some benefit from it. Because sharing is caring.

License

This script is free and unencumbered software released to the public domain. See license details.

  • 1
    Is it possible to indent with 4 spaces? I have seen many more posts which used spaces indentations (not tab) on SO. – A.L Dec 10 '14 at 23:10
  • @A.L Ah, thanks for pointing out that bug. [Enter] was mis-handling the 4-space indentation. I fixed it. You can now indent with 4 spaces and the script will continue auto indenting with tabs. Try it out and let me know if it's still a problem. Merci beaucoup! – rojo Dec 11 '14 at 1:59
  • I tested your plugin and appreciated it, but I don't like that it add a tab in a block of code which uses 4 spaces for indentations. But in the same time it's not something easy to detect. – A.L Dec 12 '14 at 21:32
  • @A.L I appreciate your feedback. If my script's replacement of spaces with tabs is irritating to you, then you might prefer balpha's script. His uses spaces for indenting. Modifying my script to indent with spaces would be prohibitively complicated, as so much of the logic depends on spaces being converted to tabs. But as you say, it's invisible, and I prefer it this way. It's faster for me to arrow key through 2 tabs than through 8 spaces. – rojo Dec 12 '14 at 21:43
  • In fact, when I said it's not something easy to detect I was thinking about detecting the indentation of the line or the previous line in order to indent with a tab or 4 spaces. I don't know if edit reviewers care about mixing indentation with spaces or tabs. – A.L Dec 12 '14 at 21:48
  • I forgot it until now, but the text editor of Stack Exchange uses 4 spaces to indent block codes. That's why I don't like using tabs for indenting text on the SE network. – A.L Dec 13 '14 at 23:45
  • @A.L I'm sorry, but I'm not sure that understand what you mean. Are you saying that only spaces can be used to define a code block on StackExchange pages? If so, I think you're mistaken. A tab will work just as well as 4 spaces to define a code block in StackExchange network's markdown syntax. – rojo Dec 14 '14 at 13:12
  • 2
    I'm not saying that only spaces are used for indentation,tabs work too. But since SE uses 4 spaces for indentation I prefer to use spaces too. – A.L Dec 14 '14 at 15:45
  • Using tabs is a nightmare which is why you end up having to build scripts like this in the first place, just use spaces. – Lankymart Jul 14 '15 at 12:04

You must log in to answer this question.

Browse other questions tagged .