如何在 Matlab 中保存时自动删除尾随空格?
Posted
技术标签:
【中文标题】如何在 Matlab 中保存时自动删除尾随空格?【英文标题】:How to auto-remove trailing whitespaces on save in Matlab? 【发布时间】:2013-11-15 05:26:18 【问题描述】:我没有在 Matlab 2012b 中找到基本功能:
Remove trailing whitespaces on save.
相关:
How to auto-remove trailing whitespace in Eclipse?
Aptana 3 - How remove trailing whitespaces on save
【问题讨论】:
AFAIK,MATLAB 编辑器中不存在此功能。使用自动缩进(Ctrl+i
选择)确实去除所有尾随空格,但每次保存前都这样做很痛苦。您可以制作一个自动热键脚本,每次在 MATLAB 编辑器窗口中按 Ctrl+s
时发送 Ctrl+A, Ctrl+i, Ctrl+s right
,但这只是恕我直言应该成为编辑器核心一部分的功能的一种解决方法(具有不需要的副作用)。
感谢您的解决方法!
【参考方案1】:
我没有足够的声誉来发表评论,但我在 github 上创建了一个 Gist,以更新 Sam Roberts 的答案:
它会记住您最后的光标位置/选择,并在删除尾随空格后选择返回。
我还让它删除了编辑器末尾的所有尾随空行。
我发现它对于较长的文件非常有用
https://gist.github.com/hmaarrfk/8462415
% To remove a Matlab trailing whitespace in the editor
% Original Author: Sam Roberts
% http://***.com/questions/19770347/how-to-auto-remove-trailing-whitespaces-on-save-in-matlab
% Modified by Mark Harfouche to remember cursor location
%
%
% Temp variable for shortcut. Give it an unusual name so it's unlikely to
% conflict with anything in the workspace.
shtcutwh__ = struct;
% Check that the editor is available.
if ~matlab.desktop.editor.isEditorAvailable
return
end
% Check that a document exists.
shtcutwh__.activeDoc = matlab.desktop.editor.getActive;
if isempty(shtcutwh__.activeDoc)
return
end
% save the old cursor location
shtcutwh__.Selection = shtcutwh__.activeDoc.Selection;
% Get the current text.
shtcutwh__.txt = shtcutwh__.activeDoc.Text;
% Remove trailing whitespace from each line.
shtcutwh__.lines = deblank(regexp(shtcutwh__.txt,'[^\n]*(\n)|[^\n]*$', 'match'));
% remove the trailing blank lines
for n = length(shtcutwh__.lines):-1:1
if length(shtcutwh__.linesn) == 0
shtcutwh__.lines(n) = [];
else
break
end
end
% Reconcatenate lines.
shtcutwh__.addNewline = @(x)sprintf('%s\n',x);
shtcutwh__.lines = cellfun(shtcutwh__.addNewline, shtcutwh__.lines, 'UniformOutput', false);
% If you always want to add a newline at the end of the file, comment this line out
% Remove the last newline character
shtcutwh__.linesend(end) = '';
shtcutwh__.newtxt = horzcat(shtcutwh__.lines:);
% Set the current text.
shtcutwh__.activeDoc.Text = shtcutwh__.newtxt;
% Place the cursor back
shtcutwh__.activeDoc.Selection = shtcutwh__.Selection;
% Delete temp variable.
clear shtcutwh__
【讨论】:
太棒了。我几乎每天都使用它。【参考方案2】:我也有同样的需求,写了一个小脚本来做一些接近的事情。将以下内容放入MATLAB desktop shortcut。每当您单击快捷按钮时,它都会从编辑器中的活动文件中删除尾随空格。不如在保存时自动执行 - 您需要记住在保存之前按下按钮 - 但差不多。在 11b、12a 和 13b 上测试过,但在 12b 上也应该没问题。
希望有帮助!
% Temp variable for shortcut. Give it an unusual name so it's unlikely to
% conflict with anything in the workspace.
shtcutwh__ = struct;
% Check that the editor is available.
if ~matlab.desktop.editor.isEditorAvailable
return
end
% Check that a document exists.
shtcutwh__.activeDoc = matlab.desktop.editor.getActive;
if isempty(shtcutwh__.activeDoc)
return
end
% Get the current text.
shtcutwh__.txt = shtcutwh__.activeDoc.Text;
% Remove trailing whitespace from each line.
shtcutwh__.lines = deblank(regexp(shtcutwh__.txt,'[^\n]*(\n)|[^\n]*$', 'match'));
% Reconcatenate lines.
shtcutwh__.addNewline = @(x)sprintf('%s\n',x);
shtcutwh__.lines = cellfun(shtcutwh__.addNewline, shtcutwh__.lines, 'UniformOutput', false);
shtcutwh__.newtxt = horzcat(shtcutwh__.lines:);
% Set the current text.
shtcutwh__.activeDoc.Text = shtcutwh__.newtxt;
% Delete temp variable.
clear shtcutwh__
【讨论】:
以上是关于如何在 Matlab 中保存时自动删除尾随空格?的主要内容,如果未能解决你的问题,请参考以下文章