How to automatically remove deleted spaces when saving in Matlab?

I did not find a significant function in Matlab 2012b:

Remove trailing whitespaces on save. 

on this topic:

How to automatically remove trailing spaces in Eclipse?

Aptana 3 - How to remove trailing spaces when saving

+6
source share
2 answers

I had the same need and I wrote a little script to do something. Put the following in the desktop shortcut of MATLAB . Whenever you click on the quick access button, it separates whitespace from the active file in the editor. Not quite as good as automatically doing this when saving - you need to remember to click a button before saving - but almost. Tested on 11b, 12a and 13b, but should also be good on 12b.

Hope this helps!

 % Temp variable for shortcut. Give it an unusual name so it 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__ 
+6
source

I don't have enough reputation for comments, but I created a Gist on github to update Sam Roberts answer:

He will remember your last position / cursor selection and select back after removing the final space.

I also deleted all trailing blank lines at the end of the editor.

I find it quite useful for longer files

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 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__ 
+6
source

Source: https://habr.com/ru/post/957349/


All Articles