MATLAB: Delete line hotkey?
Asked Answered
N

5

6

My trackpad is not comfortable to use so I aim to do coding with my keyboard only. My "kill line" hotkey do not work no matter what key I set it to. I did unassign all conflicts.

Simple question: Is there a way to make a hotkey, that will delete the line of code, the cursor (caret) is currently located on (not only all code after the cursor, like kill-line is supposed to do)?

Noe answered 20/3, 2015 at 14:23 Comment(0)
U
6

The Kill line (Ctrl+K by default in Windows) is working only in the Matlab console window by default. If you go to File->Preferences->Keyboard->Shortcuts, and select the Kill Line action line, in the next table (labeled Shortcuts for Kill Line) you will see two columns Shortcut and Tolls with shortcut. So you should make the Tools with shortcut to be All tools instead of Command Window by clicking on it and selecting everything.

Ungodly answered 20/3, 2015 at 15:24 Comment(1)
This doesn't really work as requested though, as Kill line only deletes the part of text after the cursor. There's no command in Matlab (R2016b) to delete a line, AFAIK. (Please let me know if someone finds a workaround, using a plugin or otherwise).Roundabout
G
2

A better way is to use AHK directly:

#IfWinActive,ahk_exe MATLAB.exe
    ^d::send,{End}{Home 2}+{Down}{Del}
#IfWinActive

You can change ^d (ctrl+d) to any other key you like.

Gunslinger answered 18/10, 2019 at 13:52 Comment(0)
V
1

There is. You need to create new shortcut, pin it to quick access toolbar and run it with Alt + number (or custom shortcut with AHK)

Home -> New -> Command Shortcut

Paste there this piece of code and fill Label field with i.e "Delete row" string and check Add to quick access toolbar.

currentEditor = matlab.desktop.editor.getActive;
originalSelection =currentEditor.Selection;
row = originalSelection(1);% get row of cursor
C = strsplit(currentEditor.Text,'\n');% Split text of editor by lines
C(row) = [];%remove current row
currentEditor.Text = strjoin(C,'\n');%join it together
currentEditor.Selection =  [originalSelection(1) 1  originalSelection(1) 1 ];
  % make sure cursor is on the same line and on first position

Shortcut editor

Now your shortcut probably appear next to Search documentation text box. Pressing Alt + 1 will trigger the code and delete the row in editor.

Now you can change it to your custom shortcut keys with help of AutoHotkey.

#IfWinActive MATLAB    ;in MATLAB window
F11::                  ;pressing F11...
Send !1                ;triggers Alt + 1 

Edit: After hour of testing I have to say, that this is working, but sadly not well, especially with larger scripts. There is delay and editor "blinks"...

Voleta answered 7/2, 2017 at 18:35 Comment(0)
A
1

The problem with Alamakanambra's solution is that it will unify multiple '\n' into a single '\n'

For example if this is your code:

y=eye(5)
x=eye(5)


% set m
m = length(x)

% display value of m
fprintf('value of m is: %d', m)

And you would like to remove this line: y=eye(5) Then running the previous solution will output the following code:

x=eye(5)
% set m
m = length(x)
% display value of m
fprintf('value of m is: %d', m)

The desired output is:

x=eye(5)


% set m
m = length(x)

% display value of m
fprintf('value of m is: %d', m)

The solution is to use the CollapseDelimiters property, and set it to false in the strsplit method

You can find the solution here

This is the full solution:

currentEditor = matlab.desktop.editor.getActive;
originalSelection =currentEditor.Selection;
row = originalSelection(1)-1;% get row of cursor
C = strsplit(currentEditor.Text,'\n','CollapseDelimiters',false) % Split text of editor by lines
C(row) = [];%remove current row
currentEditor.Text = strjoin(C,'\n');%join it together
currentEditor.Selection =  [originalSelection(1)-1 1  originalSelection(1) 0 ];
  % make sure cursor is on the same line and on first position
Anhydrous answered 11/4, 2019 at 23:38 Comment(0)
C
1

In MATLAB version R2024b Desktop, the default shortcut is option+delete for MacOS which deletes the entire line where your cursor is on. Note this only works in the editor and not command window. I just use command+A (to quickly select all in the current line) and then delete when in the command window.

If you search up the key bindings in the settings it actually shows the delete key bindings as control+delete and command + delete, however both does not work in practice and only option+delete works for me. This might be a glitch because option+delete is unlisted.

I think you can adjust this in the hot-key settings but doesn't seem to be working for me at the moment.

If anyone knows how to make the option+delete shortcut work in the command window, please let me know.

Chapman answered 4/10, 2024 at 11:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.