Clear Filters
Clear Filters

Keyboard shortcut for duplicating a line of code

162 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.
Image Analyst
Image Analyst on 17 Feb 2022
Edited: Image Analyst on 17 Feb 2022
@James Van Zandt, at least in the version I'm using, r2021b, if I click in a line and right click I can see Duplicate Line which can be performed by the "shortcut" control-shift-c. See what it says on your machine. I tried it -- it works, even without highlighting the line (the cursor just has to be somewhere in it). Amy (below) said it works for Live editor, but I've verfied it now works with the regular editor also.
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 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).
Image Analyst
Image Analyst on 9 Aug 2021
I wasn't talking about web site (MATLAB online, or MATLAB Answers). Do you realize that there are two editors in the main MATLAB program? One for m-files and one for "Live Editor" mlx-files? So I finally actually tried in a new .mlx file using Live Editor. If you have the cursor anywhere in the line and type control-shift-C, it makes a duplicate of the line just below the line you are on. Not ideal. Often/usually that is not where you wanted to paste the lines. But it didn't copy the line into the clipboard but into some secret buffer. So if you go to some other place and try to paste it in there, it won't show up there -- you'll get whatever is in the actual clipboard. Again, not good. Actually I don't see any use for this except if you wanted a duplicate of the line right in your current location. It's no good for copying and pasting to arbitrary other locations.
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)
Image Analyst
Image Analyst on 9 Apr 2022
Edited: Image Analyst on 9 Apr 2022
I tried it in Windows 10, and it (Alt + Mouse Left-Click) does not duplicate the line. It merely deselects the line.
However, If your cursor is anywhere in the line, you can do shift-control-c and that will duplicate the line. It now works in the regular editor also, not just the Live Editor.
If you select multiple lines, then shift-control-c will duplicate the set of lines (a new set will appear below the last line of the original set).
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.


Find more on Desktop in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!