Unwanted editor behavior: whitespace only lines trimmed
17 views (last 30 days)
Show older comments
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
Answers (2)
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;
0 Comments
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
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.
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!