Repository: xonsh/xonsh

Issue:
`$XONSH_COPY_ON_DELETE = False` not respected when `$VI_MODE = True`
When `$VI_MODE = True`, deleting any chunk of text (eg `<esc>dd` to delete the current line) will copy it to the clipboard. Even when explicitly adding `$XONSH_COPY_ON_DELETE = False` (which should be the default behaviour) this still occurs.

## minimum reproducible `xonfig`

```python
$VI_MODE = True
$XONSH_COPY_ON_DELETE = False
```
Version info:
```bash
~ $ xonsh --version
xonsh/0.13.4
```
```
iterm2: build 3.4.19
macos: 13.4 (22F66)
```



Discussion:
Ah I was just looking into this myself!

@daniel-shimon Is there a particular reason for your implementation (8da682b)?

My proposed implementation is to get rid of the current `disable_copy_on_deletion` function, and instead [during shell initialization](https://github.com/xonsh/xonsh/blob/66f0ba39f2a190c06f2a59ad1da40cf388ac9f25/xonsh/ptk_shell/shell.py#L197) instantiate [ptk.application.Application](https://python-prompt-toolkit.readthedocs.io/en/master/pages/reference.html#module-prompt_toolkit.application) with a [`PyperclipClipboard`](https://python-prompt-toolkit.readthedocs.io/en/master/pages/reference.html#prompt_toolkit.clipboard.pyperclip.PyperclipClipboard) by default (current behavior) but an [`InMemoryClipboard`](https://python-prompt-toolkit.readthedocs.io/en/master/pages/reference.html#prompt_toolkit.clipboard.InMemoryClipboard) when `$XONSH_COPY_ON_DELETE` is set.

This would presumably disable the system clipboard altogether, not only when removing words (ctrl+backspace), but in VI mode as well.

For consistency I suppose we'd rename `$XONSH_COPY_ON_DELETE` to `$XONSH_USE_SYSTEM_CLIPBOARD`.

---

I will implement this locally as a PoC, and if no-one has any objections I'll clean it up and make a PR.
PR is welcome!

Resolving patch:
diff --git a/xonsh/environ.py b/xonsh/environ.py
--- a/xonsh/environ.py
+++ b/xonsh/environ.py
@@ -1689,7 +1689,14 @@ class PTKSetting(PromptSetting):  # sub-classing -> sub-group
     )
     XONSH_COPY_ON_DELETE = Var.with_default(
         False,
-        "Whether to copy words/lines to clipboard on deletion (must be set in .xonshrc file)."
+        "Whether to copy words/lines to clipboard on deletion (must be set in the run control file)."
+        "Does not have any effect in ``vi_mode``."
+        "Only available under the prompt-toolkit shell.",
+    )
+    XONSH_USE_SYSTEM_CLIPBOARD = Var.with_default(
+        True,
+        "Whether to let the shell use the system clipboard (must be set in the run control file)."
+        "The main use-case is to fully disable clipboard integration in ``vi_mode``."
         "Only available under the prompt-toolkit shell.",
     )
     XONSH_CTRL_BKSP_DELETION = Var.with_default(
diff --git a/xonsh/ptk_shell/shell.py b/xonsh/ptk_shell/shell.py
--- a/xonsh/ptk_shell/shell.py
+++ b/xonsh/ptk_shell/shell.py
@@ -7,6 +7,7 @@
 
 from prompt_toolkit import ANSI
 from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
+from prompt_toolkit.clipboard import InMemoryClipboard
 from prompt_toolkit.enums import EditingMode
 from prompt_toolkit.formatted_text import PygmentsTokens, to_formatted_text
 from prompt_toolkit.history import ThreadedHistory
@@ -193,8 +194,11 @@ def __init__(self, **kwargs):
         ptk_args.setdefault("history", self.history)
         if not XSH.env.get("XONSH_COPY_ON_DELETE", False):
             disable_copy_on_deletion()
-        if HAVE_SYS_CLIPBOARD:
-            ptk_args.setdefault("clipboard", PyperclipClipboard())
+        if HAVE_SYS_CLIPBOARD and (XSH.env.get("XONSH_USE_SYSTEM_CLIPBOARD", True)):
+            default_clipboard = PyperclipClipboard()
+        else:
+            default_clipboard = InMemoryClipboard()
+        ptk_args.setdefault("clipboard", default_clipboard)
         self.prompter: PromptSession = PromptSession(**ptk_args)
 
         self.prompt_formatter = PTKPromptFormatter(self)


Test patch: