# Stop Mathematica’s automatic indentation on existing notebooks

This question relates to Mathematica 12.3 (and possibly many other versions).

Mathematica's automatic indentation does not conform to my workflow. From the very old days of Mathematica 1.2, i.e. before the introduction of the notebook frontends, I am still used to writing in indentation notation with one brace or bracket below the matching one. At the end of a construct, I insert additional line breaks so that I can align the closing parenthesis to match the beginning of the construct.

However, the frontend prevents me from doing this when I use Input cells. I could circumvent this by using Code cells, but they have other drawbacks.

I found out that you can disable Mathematica's indentation in Input cells by changing the Input cell in your own Default.nb like this (it is an example of my favorite indentation style):

Cell[StyleData["Input"]
, AutoIndent -> False    (* 2020-12-08 *)
, AutoSpacing -> False   (* 2020-12-08 *)
, FontSize -> 13
, CellMargins->{{66, 10}, {5, 8}}
, StyleKeyMapping->{"=" -> "WolframAlphaShort"
, "*" -> "Item"
, ">" -> "ExternalLanguageDefault"
}
]


But this only works on new notebooks. For an existing one, you do this:

1. call Format>OptionInspector: Show Option Values>Selected Notebook and set Lookup: AutoIndent to False, then click Apply,
2. set Lookup: AutoSpacing to False, then click on Apply.

(If I do this with Show Option Values>Global, it still doesn't work on old notebooks after they have been loaded).

• How can I combine steps 1. and 2. programmatically?
• Can I make it happen automatically after I open an existing notebook?

My favorite indentation scheme:

Because function names, which precede a square bracket, practically belong to the bracket following them, I treat the function name regarding the indentation like an opening bracket. Then the closing square bracket is in the same indentation depth as the function name before the opening square bracket (and of course the closing bracket is not in-column with its particular opening one).

Mathematica syntax separates parameters and list items by commas. If you want to see where the next parameter or list element begins in an indented text, you have to keep looking back and forth between the beginning of the indented text and the end of the line. This is exhausting and tiring, and it is easy to miss a comma.

You can also consider the commas as parameter separators or between list elements as belonging to the same indentation order as the corresponding opening and closing brackets, braces or parentheses. In fact, the coloring of syntax elements during editing in the frontend, it highlights the associated brackets together with the keyword before them, e.g. if you click before a comma. My personal view on the syntax elements seems to be present among at least some developers of Mathematica).

My preferred indentation takes up more space in the vertical direction than absolutely necessary, but it makes it easier to see the relationships.

• That is why I use code cells to write code in and not input cells. This way I have full control of formatting myself without the frontend formatting getting in the way. I Jun 14 at 8:24
• The disadvantage of code cells is that they do not automatically wrap lines. If the window is too narrow, you can miss something at the end of the line, which disappears behind the boundary of the window. Fortunately, the light green markings on brackets also work in code cells in my version. Jun 14 at 11:51
• @AdalbertHanßen I recommend that you remove or condense the discussion of your preferred indentation scheme at the end of the question. You already provided a clear example in code, and your question is not about the indentation scheme itself anyway, but about how to enforce any such scheme in existing notebooks. Jun 14 at 14:42
• You can enter a pair of quotes in Mathematica's notebook, then paste the code in, finally delete the quotes outside. Jun 15 at 5:43

Note: saving the style sheet in the directory returned by FileNameJoin[{\$UserBaseDirectory, "SystemFiles", "FrontEnd", "StyleSheets"}] will result in the stylesheet name appearing in the Format>Stylesheet submenu.