Keyboard shortcut for duplicating a line of code

136 ビュー (過去 30 日間)
Radim Zedka
Radim Zedka 2021 年 4 月 17 日
編集済み: Amy 2022 年 10 月 26 日
Is there any keyboard shortcut for duplicating a line of code in MATLAB editor ? (e.g. the way Ctrl+D does it in notepad++)

採用された回答

Image Analyst
Image Analyst 2021 年 4 月 17 日
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 件のコメント
Image Analyst
Image Analyst 2022 年 2 月 17 日
編集済み: Image Analyst 2022 年 2 月 17 日
@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 2022 年 2 月 17 日
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.

サインインしてコメントする。

その他の回答 (2 件)

Amy
Amy 2021 年 5 月 18 日
編集済み: Amy 2022 年 10 月 26 日
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 件のコメント
Image Analyst
Image Analyst 2021 年 8 月 9 日
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 2022 年 2 月 17 日
@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
Sajjad Monfared 2022 年 4 月 9 日
編集済み: Sajjad Monfared 2022 年 4 月 9 日
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 件のコメント
Image Analyst
Image Analyst 2022 年 4 月 9 日
編集済み: Image Analyst 2022 年 4 月 9 日
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 2022 年 4 月 9 日
編集済み: Sajjad Monfared 2022 年 4 月 9 日
@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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by