Unwanted editor behavior: whitespace only lines trimmed
조회 수: 17 (최근 30일)
이전 댓글 표시
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!!!)
답변 (2개)
goc3
2024년 3월 22일
편집: goc3
2024년 4월 1일
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
Pratyush
2023년 10월 20일
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
DGM
2023년 10월 20일
편집: DGM
2023년 10월 20일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!