Keyboard shortcut for duplicating a line of code

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

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

The suggested keystrokes do not work here. I'm running MATLAB '.9.0.1467703 (R2020b)' under macOS Big Sur. In this MATLAB editor, "home" and "end" send the insertion point to the beginning or end of the entire file. I find that terribly disruptive, since I routinely edit MATLAB files of a thousand lines or more. With all my other editors (Word, Outlook, PowerPoint, even EMACS), "home" and "end" stay on the same line. Is there some setting to change this?
@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.
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

That would be nice. Not as convenient as simply hitting + on the numeric keyboard but at least a step in the right direction. Can I just click anywhere arbitrarily in the line, and click and drag to another arbitrary column on another line, then type control-shift,C to capture all of all lines that are all or partially highlighted?
My Brief command set also had a nice feature where you could do alt-L (to start "marking" lines) then you could hit down or up arrow as many times as you want to select adjacent lines, then hit + to copy them all into the clipboard. Very nice since often, or even usually, you want to select a range of lines to copy or cut.
Yes, if there are multiple lines selected, then using the shortcut will duplicate every line that's selected or partially selected. It should also (hopefully once it's added) be possible to configure a custom shortcut for it other than the default Ctrl-Shift-C (in Preferences under MATLAB->Keyboard->Shortcuts).
I don't think we have the ability to "mark" lines like you described, but I can create an enhancement request to consider adding support for that in a future release.
Thank you, But I Can't.
I press ctrl+shift+c in a line. I do not see any reaction.
@nima nabavi, it probably won't show a "reaction" though it is supposed to be in the clipboard. Are you sure you're using Live editor, not the regular m-file editor? And you are sure you tried to paste it in somewhere to see if it was actually in the clipboard?
I use software. Not under the website.
Another point, for example, if you press the "Cntrl+D " keys, in notpadPlus plus(npp) the line will be repeated(very easy).!
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.
@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

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).
@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

Products

Release

R2020b

Asked:

on 17 Apr 2021

Edited:

Amy
on 26 Oct 2022

Community Treasure Hunt

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

Start Hunting!