Keyboard shortcut for duplicating a line of code

98 views (last 30 days)
Is there any keyboard shortcut for duplicating a line of code in MATLAB editor ? (e.g. the way Ctrl+D does it in notepad++)

Accepted Answer

Image Analyst
Image Analyst on 17 Apr 2021
I don't think there is one. Unfortunately MATLAB is not a programmer's editor. It's a slightly modified version of the lame CUA editor like is used in word processing programs and elsewhere. In my old editor (Codewright, I could simply type + on the numeric keypad top copy the current line into the clipboard, then type the Insert key to paste a duplicate in the same place). In MATLAB you'd have to do
Home shift-DownArrow Ctrl-C Ctrl-V Ctrl-V
So that's touching 6 keys to do the 5 operations instead of hitting just 2 keys. But it will duplicate the line of code. I think you could make a script to do that and put it up on the quick access toolbar.
  3 Comments
Steven Lord
Steven Lord on 17 Feb 2022
FYI this functionality was added to Live Editor in release R2019b according to the Release Notes. That entry suggests using Ctrl+Shift+C on Windows or Linux or Cmd+Shift+C on Mac.

Sign in to comment.

More Answers (2)

Amy
Amy on 18 May 2021
Edited: Amy on 26 Oct 2022
In the Live Editor, or in the Editor in R2021b and later, you can use the shortcut Ctrl-Shift-C to duplicate the current line of code (or right click and select "Duplicate Line" from the context menu).
  9 Comments
Image Analyst
Image Analyst on 17 Feb 2022
@nima nabavi for me, typing that 3-key combination instantly duplicates the line, so now I have two instances of the line in the editor.

Sign in to comment.


Sajjad Monfared
Sajjad Monfared on 9 Apr 2022
Edited: Sajjad Monfared on 9 Apr 2022
I found a way to do this in Linux. It's a bit tricky but you'll get used to it.
  1. Select the line you want to duplicate (I suggest tripple clicking on the line to do the selection; because it also selects the newline character ('\n') at the end of the line)
  2. Press mouse middle-button (or Alt + Mouse Left-Click)
  2 Comments
Sajjad Monfared
Sajjad Monfared on 9 Apr 2022
Edited: Sajjad Monfared on 9 Apr 2022
@Image Analyst You are right. Although I haven't tried it myself, I'm pretty sure it's not going to work on Windows.
Using mouse middle click is a common mechanism in Linux for quick copy/pasting in terminal window. I wasn't sure it would perform the same function in MATLAB, so I just gave it a try on a MATLAB editor and it worked!
shift-control-c combination doesn't work on the installed version on my machine.

Sign in to comment.

Categories

Find more on Desktop in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!