Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-remove trailing whitespaces on save in Matlab?

I don't find an essential feature in Matlab 2012b:

Remove trailing whitespaces on save.

Related:

How to auto-remove trailing whitespace in Eclipse?

Aptana 3 - How remove trailing whitespaces on save

like image 412
Wok Avatar asked Nov 04 '13 14:11

Wok


People also ask

How do you remove trailing spaces in Matlab?

Description. newStr = strtrim( str ) removes leading and trailing whitespace characters from str and returns the result as newStr .

Which function is used to remove leading and trailing Whitespaces?

The Trim function The most obvious (and generally efficient) method for removing both leading and trailing space is to use the TRIM() function.

How do I remove trailing spaces from a file?

Method One: sed A simple command line approach to remove unwanted whitespaces is via sed . The following command deletes all spaces and tabs at the end of each line in input. java . If there are multiple files that need trailing whitespaces removed, you can use a combination of find and sed commands.

How do you fix a trailing whitespace error?

Use your editor to find the end of the line and backspace. Many modern text editors can also automatically remove trailing whitespace from the end of the line, for example every time you save a file. In emacs: C-M-% <space> + $ then press return twice.


2 Answers

I had the same need, and wrote a little script to do something close. Put the following in a MATLAB desktop shortcut. Whenever you click the shortcut button, it will strip trailing whitespace from the active file in the editor. Not quite as good as automatically doing it on save - you need to remember to press the button before saving - but nearly. Tested on 11b, 12a, and 13b, but should also be fine on 12b.

Hope that helps!

% 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__
like image 67
Sam Roberts Avatar answered Oct 09 '22 21:10

Sam Roberts


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

It will remember your last cursor position/selection and select back after removing the trailing whitespace.

I also made it remove 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'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__
like image 37
hmaarrfk Avatar answered Oct 09 '22 20:10

hmaarrfk