2013-11-04 10 views
6

Non trovo una caratteristica essenziale in Matlab 2012b:Come rimuovere automaticamente gli spazi bianchi finali al salvataggio in Matlab?

Remove trailing whitespaces on save. 

correlati:

How to auto-remove trailing whitespace in Eclipse?

Aptana 3 - How remove trailing whitespaces on save

+3

per quanto ne so, questa funzione non esiste nell'editor MATLAB. Usare l'indentazione automatica ('Ctrl + i' su una selezione) * fa * spoglia tutti gli spazi bianchi finali, ma è un dolore da fare ogni volta prima di un salvataggio. Puoi creare uno script autohotkey che manda 'Ctrl + A, Ctrl + i, Ctrl + s 'a destra ogni volta che premi' Ctrl + s' nella finestra dell'editor MATLAB, ma questa è solo una soluzione (con effetti collaterali indesiderati) per una funzione che IMHO dovrebbe far parte del nucleo dell'editor. –

+0

Grazie per la soluzione! – Wok

risposta

6

Ho avuto la stessa necessità, e ha scritto un piccolo script da fare qualcosa vicino Inserire quanto segue in un MATLAB desktop shortcut. Ogni volta che fai clic sul pulsante di scelta rapida, lo spazio vuoto finale viene rimosso dal file attivo nell'editor. Non abbastanza buono come farlo automaticamente su Salva - è necessario ricordare di premere il pulsante prima di salvare - ma quasi. Testato su 11b, 12a e 13b, ma dovrebbe anche andare bene su 12b.

Spero che questo aiuti!

% 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__ 
6

io non ho abbastanza fama di commentare, ma ho creato un Gist su GitHub, per aggiornare Sam Roberts' risposta:

Si ricorderà l'ultima posizione del cursore/selezione e scegliere di nuovo dopo la rimozione lo spazio bianco finale.

Ho anche rimosso tutte le righe vuote finali alla fine dell'editor.

Trovo molto utile per i file più lunghi

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__ 
+0

Grande. Lo uso quasi tutti i giorni. – amw

Problemi correlati