5

So, many people mess up code fences in their questions. One of the most common errors is that one tab is missing at the beginning of every line. Example:

public class ExampleClass{
private void exampleMethod(){
    if(abc == ABC){
        System.out.println("123");
    }
}
}

Sometimes, fixing this can take quiet some time. Is there a way to add a tab to every single line except the first and last?

I've tried some methods which work in some editors (like holding down the Option-key), but they didn't work. Any ideas?

6
  • 6
    I usually take the code out into my own editor.. add the extra spaces (not tabs) and paste it back in.
    – Scratte
    Commented May 17, 2020 at 22:00
  • I'll sometimes add a line with a character in the left column, select that line and all the code I want to indent, hit Ctrl-K to code format it, then remove the line I added (and the blank lines possibly added by the code formatter). Commented May 17, 2020 at 22:09
  • @Scratte Hmm.. I'm ashamed of myself for not thinking of this myself. Thanks! Can you write this as an answer so I can accept it? Commented May 17, 2020 at 22:12
  • 2
    @NikitaDemodov I don't think Questions on meta needs answers like they do on the main site. If I were to write an answer, I'd have to give it some more thought. Like: Should this small piece of code be edited and causing it to go through the suggested edit queue? It's not perfect, bur still easily readable.
    – Scratte
    Commented May 17, 2020 at 22:35
  • @Scratte In my example, it is easily readable, but some people will make even worse mistakes, like not having any tabs, and, oftentimes, they will also post 200+ lines of code. THAT is unreadable. But I get where you're coming from. Maybe StackOverflow should have some type of code-format-checker which would allow anyone to add tabs/spaces without having to go to the edit queue? But I guess this should have its own feature-request. Thanks for answering! Commented May 17, 2020 at 22:45
  • We should get rid of tabs. Most modern tools cover the common use cases for them. We no longer live in the era of CRT terminals and typewriters. Commented May 18, 2020 at 14:21

1 Answer 1

9

This can easily be done using the automatic code formatting provided in the Stack Exchange editor, which indents the selected text by four spaces (and adds a blank line prior to the selection, if one doesn't exist). The automatic code formatting is available by either clicking on the icon for code formatting {} icon, or using the ctrl-k keyboard shortcut.

The trick is to use the automatic addition of four spaces for everything, while only modifying the first and last line manually to remove the existing spaces. So, for the code selection example you gave in the question, which is all already indented 4 spaces, you remove the four spaces from the first and last line, then select all of the code, and perform the auto-code formatting.

When you do it, it looks like:

Animation showing using the automatic code formatting to indent

1
  • Great, now my answer is useless, lol. Bonus points for the GIF.
    – user10957435
    Commented May 18, 2020 at 20:07

You must log in to answer this question.

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