2013-11-04 76 views
6

我不覺得在Matlab 2012B的基本特徵:如何自動刪除保存在Matlab中的尾隨空格?

Remove trailing whitespaces on save. 

相關:

How to auto-remove trailing whitespace in Eclipse?

Aptana 3 - How remove trailing whitespaces on save

+3

AFAIK,這個功能不會在MATLAB編輯器存在。使用自動縮進(在一個選擇中使用「Ctrl + i」)*會*去掉所有尾隨的空格,但在保存之前每次都會很痛苦。您可以創建一個autohotkey腳本,每次在MATLAB編輯器窗口中按Ctrl + s鍵時都會發送Ctrl + A,Ctrl + i,Ctrl + s,但這只是一個解決方法(具有不需要的副作用)恕我直言,應該是編輯器核心的一部分。 –

+0

感謝您的解決方法! – Wok

回答

6

我有同樣的需求,並寫了一個小腳本做有點接近。將以下內容放入MATLAB desktop shortcut。無論何時單擊快捷按鈕,它都會從編輯器中的活動文件中去除尾隨的空白。不如在保存時自動完成 - 您需要記住在保存之前按下按鈕 - 但幾乎是這樣。測試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__ 
6

我沒有足夠的信譽發表評論,但我創建了一個要點在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__ 
+0

太好了。我幾乎每天都在使用它。 – amw