10

我在 Matlab 2012b 中找不到基本功能:

Remove trailing whitespaces on save.

有关的:

如何在 Eclipse 中自动删除尾随空格?

Aptana 3 - 如何在保存时删除尾随空格

4

2 回答 2

7

我有同样的需求,并写了一个小脚本来做一些接近的事情。将以下内容放在MATLAB 桌面快捷方式中。每当您单击快捷按钮时,它都会从编辑器中的活动文件中删除尾随空格。不如在保存时自动执行 - 您需要记住在保存之前按下按钮 - 但差不多。在 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__
于 2013-11-06T17:01:36.667 回答
7

我没有足够的声誉发表评论,但我在 github 上创建了一个 Gist 来更新 Sam Roberts 的答案:

它会记住您最后的光标位置/选择,并在删除尾随空格后重新选择。

我还让它删除了编辑器末尾的所有尾随空行。

我发现它对于较长的文件非常有用

https://gist.github.com/hmaarrfk/8462415

% To remove a Matlab trailing whitespace in the editor
% Original Author: Sam Roberts 
% http://stackoverflow.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__.lines{n}) == 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__.lines{end}(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__
于 2014-01-16T20:07:29.213 回答