When we select text for indenting, e.g.

enter image description here

and Ctrl+k, we get this:

enter image description here

I don't think our target audience is accustomed to this behavior, although I could be wrong. The following programs indent entire lines, as I'd expect:

  • Eclipse (tested on Kepler SR1)
  • Ideone (tested on 2013-10-29)
  • NetBeans (tested on version 7.4)
  • Notepad++ (tested on version 6.5)
  • Sublime Text (tested on version 2.0.2)
  • UltraEdit (tested on version 19.10.0.1012)
  • vi(m)—well, in this case, the Visual block already extends to the beginning of the line.

I know this is really minor, but every time it happens to me (a few times a week, though my brain might be filtering more frequent occurences) I tell myself I'll post on meta and I never do, so... here it is.


P.S. When only one line is highlighted, Eclipse, Notepad++, Sublime Text, and UltraEdit all simply replace that portion with a tab, which makes sense in their contexts, but doesn't apply here. Stack Exchange backticks the selection in that case, which I think makes sense and should stay.

  • 5
    I expect Ctrl-K to delete the entire line starting from point, so... yeah. – Ernest Friedman-Hill Oct 27 '13 at 11:34
  • 2
    Netbeans also functions the way you describe – Richard Tingle Oct 27 '13 at 13:05
  • Please, no. I find this useful enough (and have used it before) to not replace it by functionality one can already get by simply moving the mouse a little more, or pressing a key or two more. What's the problem anyway? - You do this once, see it doesn't work the way you thought, and don't do it again. – Dukeling Oct 27 '13 at 22:15
  • None of your example tools, as far as I'm aware, are intended to mix code and non-code in the same file. It would be better to compare it to web tools, where such an intention exists (don't ask me which tool, though). – Dukeling Oct 27 '13 at 22:16
  • @Dukeling - That's a good point, that IDEs and code editors aren't intended for use with mixed content. Regarding your comment that users should "simply move the mouse a little more" though, hm... I don't mean to undermine your preference, but couldn't I also argue that you could simply hit Enter before Ctrl+K'ing, if the functionality were the other way around? My point is---not an attack on your preference---but just that, either way, it's very little work. I was hoping (perhaps wrongly!) that what IDEs and code editors already do, would be what's natural to the majority of users. – Andrew Cheong Oct 29 '13 at 12:07
  • @acheong87 I don't feel particularly strongly about the way it's working now, it's just that changing things is always more work than keeping them the same, so I'd rather have SE's development time focussed on more important things. – Dukeling Oct 29 '13 at 12:16
  • @Dukeling - No disagreement there. I hope though the important things get voted up high, and little things like this, stay low to the ground, maybe for future interns at SE to pick as low-hanging fruit :-) – Andrew Cheong Oct 29 '13 at 12:18
  • 2
    Visual and SQL Server Management Studios also function in this manner (at least 2010+ and 2008R2+, respectively). – Esoteric Screen Name Oct 29 '13 at 13:28
  • 1
    And TextPad (version 5.2.0) (which does allow you to mix code and text) – JDB Oct 29 '13 at 13:53
  • 1
    Heh... even the VBA editor in the Microsoft Office products does this. (Tested with Microsoft Visual Basic for Applications 7.0) – JDB Oct 29 '13 at 13:58
  • @Dukeling - I think the question should be: which is easier to undo? If you indent the whole line, you can undo in a single keystroke. If you indent only the selected text, you have to go in and remove all of the added whitespace which takes significantly longer. – JDB Oct 29 '13 at 14:00
  • 1
    @JDB Pretty much everything listed allows you to mix code and text, but that's not the intended use case. Is certain parts of the same file in TextPad syntax highlighted and not other parts? If not, the intention was most likely not to mix the two. Undo is one key press - Ctrl-Z - either way. – Dukeling Oct 29 '13 at 14:21
  • @Dukeling - Good point about Ctrl+Z - I've added it to my post. As to editors which intend for you to mix text and code - I don't think there are very many. Stack Overflow is a bit unique, but given the audience I think that it should lean more heavily toward code editor standards, especially when the explicit goal is editing code. – JDB Oct 29 '13 at 14:33
  • Everyone needs to keep in mind that we're not really talking about indenting code (as all listed tools do, I believe), we're talking about marking text as code (which happens to be done by adding 4 spaces before it). The only merit I see here is that if you do this to text that's already code, the current behaviour is fairly illogical. – Dukeling Oct 29 '13 at 14:41
  • 1
    @Dukeling - I think the point is usability. I am looking at code in my post (I know it's code). The FAQ says I need to indent that code by four spaces in order to apply the proper formatting. Ctrl+K is the shortcut to indent that code. At this point, any programmer would expect the indentation to work the same way as all code editors. But think about it this way too: How often does code, which should be located on it's own line, start in the middle of a line? – JDB Oct 29 '13 at 15:16

I support this proposal. Based on the current behavior, the assumption would appear to be that we write posts like this:

I am having an issue with foo when I pass in an uninitialized bar: var bar;
//This is where the problem is
foo( bar ); The defined behavior should be a return value of baz... am I misunderstanding something?

Certainly some questions come in this way, but usually from low rep users who don't use the formatting tools in the first place. In practice, I see very, very few questions this poorly formatted. Most low rep users know to separate their code from the rest of their content. Professional programmers who spend time with the site (and, as I said, even most hobbyists who are new to the site) are much more likely to format like so:

I am having an issue with foo when I pass in an uninitialized bar:

var bar;
//This is where the problem is
foo( bar );

The defined behavior should be a return value of baz... am I misunderstanding something?

Now, when a knowledgable programmer who is used to using code editors selects starting from somewhere in the middle of the first line to somewhere in the middle of the last line and presses Ctrl + K, the following happens:

I am having an issue with foo when I pass in an uninitialized bar:

var b

ar;  
//This is where the problem is  
foo( 

bar );

The defined behavior should be a return value of baz... am I misunderstanding something?

This then becomes a bit of a mess to clean up after1. Attempting to reverse the indent (as you might when using a code editor) leaves you with this:

I am having an issue with foo when I pass in an uninitialized bar:

var b

ar;
//This is where the problem is
foo(

bar );

The defined behavior should be a return value of baz... am I misunderstanding something?

Realizing your mistake, you can now press Ctrl + Z (once!) to get back to where you were, but at this point I've usually lost faith in the editor and have switched to using TextPad.

If we changed the behavior to match that of nearly all professional code and text editors, then someone attempting to use the formatting shortcut on the first example would end up with this:

I am having an issue with foo when I pass in an uninitialized bar: var bar;  
\\This is where the problem is  
foo( bar ); The defined behavior should be a return value of baz... am I misunderstanding something?

Which can be easily undone using the same stroke which caused the formatting faux pas, and then formatted correctly by adding the necessary whitespace. (Again, I can't think of any examples where someone has actually submitted a post like this.)


In conclusion (TL;DR), I think this request is justified for at least the following reasons:

  • It better aligns with existing conventions within the programmer community (the goal is, after all, to edit code, even if the post contains other non-code content)
  • It is easier more intuitive to undo1
  • It would be appropriate for (in my unresearched opinion) the majority of posts
  • The current behavior is actually destructive to the original post for those caught unawares

1 As Dukeling pointed out, Ctrl + Z will undo the formatting in a single stroke. I don't know why this hadn't occurred to me before. I still think that pressing Ctrl + K twice should leave you with the same content as you started with, but this "easier to undo" point is a bit weaker now.

I really like the idea, but the only issue with me is that oftentimes I want to specifically comment variable-names within the sentence:

Say -

the user then clicks on the JButton and it triggers myNewFlashyFunction again.

So what I'd like is that, yes - allow Ctrl+K to indent irrespective of highlighting-completion, but then assign a new keyboard shortcut to specifically indent exactly what I've selected, and nothing more.

  • 2
    Even when a very long line wraps in the editor, it will still be a single line. (No line breaks.) Selecting part of such line, even if it includes the wrapping, would then still be considered a partial selection within a single line. And the feature request already excludes this case. – Arjan Feb 16 '14 at 18:24
  • Thanks for bringing this up. I'm not at a computer but I believe currently highlighting within a single line and Ctrl+K'ing does what you describe, and I'd actually like to keep that functionality as is. I was speaking only about the case where a linebreak character's included in the selection. Would that be compatible with what you are describing? – Andrew Cheong Feb 17 '14 at 7:04

You must log in to answer this question.

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