Skip to content

Instantly share code, notes, and snippets.

@hmaarrfk
Last active April 1, 2021 19:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hmaarrfk/8462415 to your computer and use it in GitHub Desktop.
Save hmaarrfk/8462415 to your computer and use it in GitHub Desktop.
To remove a Matlab trailing whitespace in the editorOriginal Author: Sam Roberts http://stackoverflow.com/questions/19770347/how-to-auto-remove-trailing-whitespaces-on-save-in-matlabModified by Mark Harfouche to remember cursor location
% To remove a Matlab trailing whitespace in the editor
% Original Author: Sam Roberts
% Improved by: Simone Gaiarin <simgunz@gmail.com>
% 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 whitespaces from each line.
shtcutwh__.allLines = regexp(shtcutwh__.txt, '[^\n]*(\n)|[^\n]*$', 'match');
shtcutwh__.lines = deblank(shtcutwh__.allLines);
% Uncomment this block to remove trailing whitespaces only from each non-empty line.
% This method preserve the indentation whitespaces inserted by matlab in the empty lines.
%emptyLinesIdx = cellfun(@isempty, shtcutwh__.lines);
%onlySpacesLinesIdx = cellfun(@(x) length(x)>1, shtcutwh__.allLines);
%idx = emptyLinesIdx & onlySpacesLinesIdx;
%if any(onlySpacesLinesIdx)
% shtcutwh__.lines(idx) = regexprep(shtcutwh__.allLines(idx), '\n', '');
%end
% remove the trailing blank lines
for n = length(shtcutwh__.lines):-1:1
if isempty(regexp(shtcutwh__.lines{n}, '.*\S.*', 'once'))
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{:});
% Check for changes
if (~strcmp(shtcutwh__.activeDoc.Text, shtcutwh__.newtxt))
% Set the current text.
shtcutwh__.activeDoc.Text = shtcutwh__.newtxt;
% Place the cursor back
shtcutwh__.activeDoc.Selection = shtcutwh__.Selection;
end
% Place the cursor back
shtcutwh__.activeDoc.Selection = shtcutwh__.Selection;
% Delete temp variable.
clear shtcutwh__
%{
Changelog:
simgunz commented on Dec 4, 2014
I've modified the script in order to add the possibility to keep the spaces in the lines where there are only spaces. This because matlab insert those spaces automatically to indent the code. If your collaborators aren't using this script you will end with a lot of deletion in you git diff because you'll keep deleting the indent-spaces inserted automatically by the others.
You can find the update version in my fork of this repository if you want to update it. This feature is disabled by default, you should uncomment a code block.
szapp commented on Sep 22
Thanks for this helpful script. I forked it to have it replace the text only if it was changed, to prevent the "unsaved" asterisk.
%}
@simgunz
Copy link

simgunz commented Dec 4, 2014

I've modified the script in order to add the possibility to keep the spaces in the lines where there are only spaces. This because matlab insert those spaces automatically to indent the code. If your collaborators aren't using this script you will end with a lot of deletion in you git diff because you'll keep deleting the indent-spaces inserted automatically by the others.
You can find the update version in my fork of this repository if you want to update it. This feature is disabled by default, you should uncomment a code block.

@szapp
Copy link

szapp commented Sep 22, 2016

Thanks for this helpful script. I forked it to have it replace the text only if it was changed, to prevent the "unsaved" asterisk.

@hmaarrfk
Copy link
Author

hmaarrfk commented Dec 7, 2016

Thanks guys, I updated the text with both your changes. simgunz, I also used your new regexp

@hadarl
Copy link

hadarl commented May 9, 2017

Awsome

@pestophagous
Copy link

tried it. loved it. starred it. thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment