19

Normally when you want to enter code in an answer you can simply paste your code, highlight it and hit the "format as code" (curly braces icon) on the toolbar above your answer ('Ctrl+K' does the same thing). That indents by four spaces to produce something like this:

alert("hello");

Thats all fine and dandy but if you want to include code in a list, that is where the "format as code" button fails, because it always simply adds (or removes) 4 spaces before the highlighted block. I am proposing that the "format as code" function be modified to detect how deep you are in a list and format the code (add spaces) accordingly. For example:

  • This is my first list item

    alert("Format as code should indent me by 8 spaces");
    
  • This is my second list item

    alert("Format as code should indent me by 8 spaces");
    
    • This list item is indented by four spaces

      alert("Format as code should indent me by 12 spaces");
      
      • This list item is indented by eight spaces

        alert("Format as code should indent me by 16 spaces");
        
6
  • I think this is a duplicate, but +1 nonetheless.
    – Naftali
    Commented Oct 23, 2012 at 18:01
  • In case the feature is not implemented, a quick work around to this (instead of manually adding spaces before every line) is to indent by 4 spaces, copy into notepad then find/replace the 4 spaces with however many you need. Hopefully however, I (we) wont always need to resort to that.
    – n00b
    Commented Oct 23, 2012 at 18:06
  • 2
    @amana found it! Commented Oct 23, 2012 at 18:18
  • @ShaWizDowArd I knew it had to exist!
    – Naftali
    Commented Oct 23, 2012 at 18:32
  • 1
    I dont agree with #1, #2 and #4 of that feature request. #1 & #2 is already covered in the "dropdown help menu" and the "advanced help" page. The question is too broad a feature request, I am only requesting #3 of that question.
    – n00b
    Commented Oct 23, 2012 at 18:32
  • 1
    +1 This fix is super important. So annoying to have to copy to an external editor to get the correct indentation.
    – Andrew Mao
    Commented Sep 18, 2013 at 20:14

1 Answer 1

1

I agree this is super annoying and needs to be fixed, but for now there is a simple way to trick the Javascript to do what you want in a few keystrokes, without having to use an external editor. I was always doing that before, or counting spaces by hand, both of which were really cumbersome. So I'd like to share.

Suppose you have something like the following:

code_that_needs_indenting = function() {
    // ... some long boring code
};

Press Ctrl-K to indent it 4 spaces, then insert a character on the line above without indentation:

a
    code_that_needs_indenting = function() {
        // ... some long boring code
    };

Select the entire block (including the character) and press Ctrl-K again. You have now indented the block by 8 spaces.

    a
        code_that_needs_indenting = function() {
            // ... some long boring code
        };

Move the character back to the beginning, and rinse and repeat if you have even deeper nesting in your list:

a        
        code_that_needs_indenting = function() {
            // ... some long boring code
        };

Press Ctrl-K to get your code indented by 12 spaces:

    a            
            code_that_needs_indenting = function() {
                // ... some long boring code
            };

...and so on.

P.S. the process by which I had to generate the blocks above was a real m.f.

1

You must log in to answer this question.

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