Unwanted editor behavior: whitespace only lines trimmed

14 views (last 30 days)
I'm using MATLAB 2023a. Sometimes, when I have a line in an m-file with only whitespace then whitespace characters are removed from the line (not by me). I want this editor behavior to stop. In other languages/editors, I can set something like "remove trailing whitespace" to false to get the desired behavior. How do I do this in MATLAB?
What I get (no whitespace):
if
code;
code;
end
What I want (whitespace):
if
code;
code;
end
Edit:
From @DGM's comment, I understand the smart indenting behavior changed in R2021 (as pointed out in Smart Indenting now Removes Whitespace - MATLAB Answers - MATLAB Central (mathworks.com)). I would like to leave smart indenting on and for it to leave whitespace on otherwise blank lines. How can I acheive this via setting preferences? (This assumes MathWorks would not change a behavior so prominently linked to line-by-line workflows without providing an option to revert the change!!!)
  3 Comments
Jonathan
Jonathan on 20 Oct 2023
@DGM, this is the problem. That is, the problem is the behavior of smart indenting changed. I want to know how to return smart indenting to the previous behavior.
Jonathan
Jonathan on 20 Oct 2023
Here's another post related to this. I agree the new behavior is infuriating. I believe blank lines should (in some sense) begin at the level of indentation of the context the line is in. The old smart indent behavior does this. The new one does not. I want an option to go back.
In my opinion, this is just as bad as Windows 11 not initially having the "Never Combine Windows" option for the task bar. This breaks my workflow so much, I will recommend new projects use Python. At least there, I can turn the option off for my preferred editor.

Sign in to comment.

Answers (2)

goc3
goc3 on 22 Mar 2024
Edited: goc3 on 1 Apr 2024
Release 2024a includes a setting for this!! The following code will stop smart indenting from removing white space:
s = settings;
s.matlab.editor.indent.PadEmptyLines.PersonalValue = 1;
You may also need to set the following:
s.matlab.editor.indent.RemoveAutomaticWhitespace.PersonalValue = 0;
See the documentation page for more details on this and other editor settings.

Pratyush
Pratyush on 20 Oct 2023
Hi Jonathan,
I understand you want to maintain the whitespaces manually placed in a M file and do not want the MATLAB editor to remove those whitespaces atomatically.
Change the following setting in MATLAB to achieve this:
Go to Home > Preferences > Editor/ Debugger > Indenting.
Uncheck the check box "Apply smart indenting while typing".
Hope this helps.
  2 Comments
DGM
DGM on 20 Oct 2023
Edited: DGM on 20 Oct 2023
How is that a solution (or workaround) to the problem?
Despite the misleading cursor position after creating a new auto-indented line, tabs are no longer actually retained on a line unless some text is inserted before hitting enter again (or unless you play some ridiculous games with editing and cursor motion sequences or held keys). In affected versions, if the smart indenter is manually invoked, it will actively remove any manually inserted indentation on blank lines.
This behavior is a counterproductive annoyance regardless of whether it's applied manually or while typing. Disabling the automated use only results in the user needing to place all indentation manually. It doesn't insert tabs in empty lines where the user would rightfully expect tabs to be automatically inserted. It doesn't force the smart indenter to preserve tabs on lines that have had tabs manually inserted.
There is one mostly-useless thing this answer does for us. If a superfluous tab were manually inserted on a new auto-indented line, the smart indenter would normally discard it, just the same as it discards the automatically inserted tabs. Disabling the automatic smart indenter prevents it from deleting tabs created manually via an identical sequence of keystrokes (at least automatically). Of course, if the smart indenter worked, we wouldn't be trying to manually add those tabs in the first place.
Disclosure: My only familiarity with these recent versions is via MATLAB online, so I'm never really sure how much is MATLAB or some difference inherent to the web-interface. Either way, that's what I've observed.
Jonathan
Jonathan on 20 Oct 2023
@Pratyush, I understand your answer. This prompts me to clarify my question. I would like to know how to return smart indenting to its pre-R2021a behavior (cf. Smart Indenting now Removes Whitespace - MATLAB Answers - MATLAB Central (mathworks.com) shared by @DGM) . That is, I would like to have smart indenting on and for smart indenting to leave whitespace on otherwise blank lines.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!