Keyboard shortcut for duplicating a line of code
Show older comments
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
More Answers (2)
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
on 18 May 2021
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.
Amy
on 18 May 2021
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.
nima nabavi
on 8 Aug 2021
Thank you, But I Can't.
Image Analyst
on 8 Aug 2021
nima nabavi
on 8 Aug 2021
I press ctrl+shift+c in a line. I do not see any reaction.
Image Analyst
on 8 Aug 2021
@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?
nima nabavi
on 9 Aug 2021
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).!
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
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.
Sajjad Monfared
on 9 Apr 2022
Edited: Sajjad Monfared
on 9 Apr 2022
0 votes
I found a way to do this in Linux. It's a bit tricky but you'll get used to it.
- 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)
- Press mouse middle-button (or Alt + Mouse Left-Click)
2 Comments
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
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.
Categories
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!