Copy text without dismissing the selection #19892

Closed
opened 2026-01-31 06:56:33 +00:00 by claunia · 8 comments
Owner

Originally created by @zadjii-msft on GitHub (May 17, 2023).

In Notepad, if there is a selection, pressing Ctrl+C, the text will be copied to the clipboard, otherwise nothing happens. This is the same in other edit boxes and controls
...
I would like Terminal/PowerShell to behave exactly like Notepad

This seems like a reasonable property to add to the copy action.

class CopyActionArgs {
  singleLine: bool = false;
  copyFormatting = false;
  dismissSelection: bool = false;
}

Originally suggested by @gaberiel44 in https://github.com/microsoft/terminal/discussions/15349

Note

Walkthrough

  • Add a Boolean DismissSelection to CopyTextArgs in src\cascadia\TerminalSettingsModel\ActionArgs.idl
  • Add DismissSelection to COPY_TEXT_ARGS in ActionArgs.h
  • The actual copy args are retrieved in TerminalPage::_HandleCopyText (in AppActionHandlers.cpp), and passed to TerminalPage::_CopyText, which calls down to TermControl::CopySelectionToClipboard.
    • In CopySelectionToClipboard, just don't ClearSelection.
  • This arg will also need localization, in ActionArgs.cpp, CopyTextArgs::GenerateName, and src\cascadia\TerminalSettingsModel\Resources\en-US\Resources.resw
Originally created by @zadjii-msft on GitHub (May 17, 2023). > In Notepad, if there is a selection, pressing Ctrl+C, the text will be copied to the clipboard, otherwise nothing happens. This is the same in other edit boxes and controls > ... > I would like Terminal/PowerShell to behave exactly like Notepad This seems like a reasonable property to add to the `copy` action. ```ts class CopyActionArgs { singleLine: bool = false; copyFormatting = false; dismissSelection: bool = false; } ``` _Originally suggested by @gaberiel44 in https://github.com/microsoft/terminal/discussions/15349_ > **Note** > ## Walkthrough * Add a `Boolean DismissSelection` to `CopyTextArgs` in `src\cascadia\TerminalSettingsModel\ActionArgs.idl` * Add `DismissSelection` to `COPY_TEXT_ARGS` in `ActionArgs.h` * The actual copy args are retrieved in `TerminalPage::_HandleCopyText` (in `AppActionHandlers.cpp`), and passed to `TerminalPage::_CopyText`, which calls down to `TermControl::CopySelectionToClipboard`. * In `CopySelectionToClipboard`, just don't `ClearSelection`. * This arg will also need localization, in `ActionArgs.cpp`, `CopyTextArgs::GenerateName`, and `src\cascadia\TerminalSettingsModel\Resources\en-US\Resources.resw`
Author
Owner

@horrificCoder commented on GitHub (May 18, 2023):

@zadjii-msft Can I work on this?

@horrificCoder commented on GitHub (May 18, 2023): @zadjii-msft Can I work on this?
Author
Owner

@zadjii-msft commented on GitHub (May 18, 2023):

Absolutely!

@zadjii-msft commented on GitHub (May 18, 2023): Absolutely!
Author
Owner

@Sayan505 commented on GitHub (May 18, 2023):

I've got a fix working. I've removed _core.ClearSelection(); from TermControl.cpp which leads to the intended behaviour. Is this correct?
Looking for feedback on my first ever PR on GitHub. https://github.com/microsoft/terminal/pull/15383

@Sayan505 commented on GitHub (May 18, 2023): I've got a fix working. I've removed `_core.ClearSelection();` from `TermControl.cpp` which leads to the intended behaviour. Is this correct? Looking for feedback on my first ever PR on GitHub. https://github.com/microsoft/terminal/pull/15383
Author
Owner

@Sayan505 commented on GitHub (May 20, 2023):

@zadjii-msft Hi, could you please explain me the point of bool dismissSelection?

@Sayan505 commented on GitHub (May 20, 2023): @zadjii-msft Hi, could you please explain me the point of `bool dismissSelection`?
Author
Owner

@zadjii-msft commented on GitHub (May 23, 2023):

could you please explain me the point of bool dismissSelection?

Sure - by making this a setting, then users can choose which behavior they want. They could even have both! Like, a user could have the following settings:

"actions":
[
    { "keys": "ctrl+c", "command": { "action": "copy", "dismissSelection": "true" }, },
    { "keys": "ctrl+d", "command": { "action": "copy", "dismissSelection": "false" }, },
]

And then

  • ctrl+c would copy and dismiss the selection (as it currently does)
  • ctrl+d would copy and leave the selection unchanged (as in Notepad et al.)

Or a user could just rebind ctrl+c to "action": "copy", "dismissSelection": "false", or whatever!

Does that make sense?

@zadjii-msft commented on GitHub (May 23, 2023): > could you please explain me the point of `bool dismissSelection`? Sure - by making this a setting, then users can choose which behavior they want. They could even have both! Like, a user could have the following settings: ```json "actions": [ { "keys": "ctrl+c", "command": { "action": "copy", "dismissSelection": "true" }, }, { "keys": "ctrl+d", "command": { "action": "copy", "dismissSelection": "false" }, }, ] ``` And then * <kbd>ctrl+c</kbd> would copy and dismiss the selection (as it currently does) * <kbd>ctrl+d</kbd> would copy and leave the selection unchanged (as in Notepad et al.) Or a user could just rebind <kbd>ctrl+c</kbd> to `"action": "copy", "dismissSelection": "false"`, or whatever! Does that make sense?
Author
Owner

@code-krishna commented on GitHub (May 28, 2023):

@zadjii-msft can I kindly work on this issue?

@code-krishna commented on GitHub (May 28, 2023): @zadjii-msft can I kindly work on this issue?
Author
Owner

@Delano5000 commented on GitHub (Jun 16, 2023):

@/class CopyActionArgs {
singleLine: bool = false;
copyFormatting = false;
dismissSelection: bool = false;
}

@Delano5000 commented on GitHub (Jun 16, 2023): @/class CopyActionArgs { singleLine: bool = false; copyFormatting = false; dismissSelection: bool = false; }
Author
Owner

@rifat01mahmud commented on GitHub (Jun 19, 2023):

function copyText() {
var selectedText = window.getSelection().toString();
var tempInput = document.createElement("textarea");
tempInput.value = selectedText;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand("copy");
document.body.removeChild(tempInput);
}

// Call the function when the copy button or any trigger event is activated
copyText();

Rifat Mahmud (Ovronormal)

@rifat01mahmud commented on GitHub (Jun 19, 2023): function copyText() { var selectedText = window.getSelection().toString(); var tempInput = document.createElement("textarea"); tempInput.value = selectedText; document.body.appendChild(tempInput); tempInput.select(); document.execCommand("copy"); document.body.removeChild(tempInput); } // Call the function when the copy button or any trigger event is activated copyText(); Rifat Mahmud (Ovronormal)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#19892