Я не нахожу важной функции в Matlab 2012b:
Remove trailing whitespaces on save.
Связанный:
Я не нахожу важной функции в Matlab 2012b:
Remove trailing whitespaces on save.
Связанный:
У меня была такая же потребность, и я написал небольшой сценарий, чтобы сделать что-то близкое. Поместите следующее на рабочий стол 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__
У меня недостаточно репутации для комментариев, но я создал Gist на github, чтобы обновить ответ Сэма Робертса:
Он запомнит вашу последнюю позицию / выбор курсора и вернется после удаления конечного пробела.
Я также удалил все завершающие пустые строки в конце редактора.
Я считаю, что это очень полезно для более длинных файлов
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__
Ctrl+i
при выделении) действительно удаляет все конечные пробелы, но это сложно делать каждый раз перед сохранением. Вы можете создать сценарий autohotkey, который отправляетCtrl+A, Ctrl+i, Ctrl+s right
каждый раз, когда вы нажимаетеCtrl+s
в окне редактора MATLAB, но это всего лишь обходной путь (с нежелательными побочными эффектами) для функции, которая IMHO должна быть частью ядра редактора. - person Rody Oldenhuis   schedule 04.11.2013