Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No ability to copy/paste in code editor #7388

Open
mlucool opened this issue Oct 17, 2019 · 7 comments
Open

No ability to copy/paste in code editor #7388

mlucool opened this issue Oct 17, 2019 · 7 comments
Labels
bug pkg:codemirror tag:Keyboard Compatibility issues with keyboards, including locale-specific and layout-specific issues
Milestone

Comments

@mlucool
Copy link
Contributor

mlucool commented Oct 17, 2019

Description

When editing a file foo.py, I cannot copy or paste (either via a context menu or ctrl-c/ctrl-v).

Reproduce

  1. Create a new file foo.py
  2. Write some text and try to copy it

Expected behavior

Copy/paste works similar to how it does in notebook

Context

  • Operating System and version: Linux
  • Browser and version: Chrome
  • JupyterLab version: 1.1.3
@jasongrout
Copy link
Contributor

This works for me. Can you go through the diagnosis guidelines to narrow down the issue? https://jupyterlab.readthedocs.io/en/stable/getting_started/issue.html

@jasongrout jasongrout added this to the Reference milestone Oct 17, 2019
@mlucool
Copy link
Contributor Author

mlucool commented Oct 18, 2019

This seems to be related to the key map editor. Ctrl-c/ctrl-y does not wok in vim mode, which I don't disagree with. Still, there is an issue. Clearer directions to reproduce:

  1. Setting->Text Editor Keymap->vim
  2. Open foo.py. Highlight some text, push y to copy. Then P to paste. This works within foo.py
  3. Open bar.ipynb and use ctrl-v to paste. The data is not copied over.
  4. ctrl-c to copy text in bar.ipynb. Use p to paste into foo.py. This does not work.

Aside from this bug, I think it would be good to have copy/paste as explicit menu options, similar to browsers and microsoft office. I think it would be nice to have this in the context and edit menus. What do you think?

@jasongrout
Copy link
Contributor

From your reproduction steps, it sounds like VIM mode is not using the system clipboard? In other words, it may have less to do with notebooks and JupyterLab, and more to do with the CodeMirror VIM mode. Does copying and pasting from the system clipboard work in either the notebook or VIM?

As for menu options - completely agree if it is possible. There are browser restrictions about copy/paste being invoked programmatically. It would be great if someone could look into it and answer definitively, as you're not the first person to suggest a copy/paste item in the menu.

Note that you can shift-right click to get the browser context menu, which usually does have copy/paste.

@jasongrout
Copy link
Contributor

(and don't forget you can use the browser's actual menu to copy/paste as well)

@mlucool
Copy link
Contributor Author

mlucool commented Oct 18, 2019

Does copying and pasting from the system clipboard work in either the notebook or VIM?

Yes for notebook, no from vim. I think this discrepancy is because vim mode doesn't work the same in notebook cells without something like https://github.com/jwkvam/jupyterlab-vim. So, this sounds like a code mirror VIM issue.

There are browser restrictions about copy/paste being invoked programmaticaly.

A menu should be possible. Copy/paste will work so long as there is called from a short running, user-generated event handler (ref).

@jasongrout
Copy link
Contributor

A menu should be possible. Copy/paste will work so long as there is called from a short running, user-generated event handler (ref).

Yes, I imagine so. Devil's in the details, as they say, especially with browsers, so it would be great if someone could investigate with the actual code.

@ianhi
Copy link
Contributor

ianhi commented Apr 6, 2020

Crtl-C

@jasongrout are you using a mac when finding that you can copy-paste using the keyboard?

It turns out that Crtl-C is a vim command that is almost (but not quite) synonymous to <esc> so CodeMirror captures Crtl-C events in an attempt to faithfully replicate vim and text doesn't get copied to the clipboard. On a mac this is avoided because you use Cmd + C to copy.

The reason that Crtl-C works for notebook cells when using that extension is because the extension disables the vim crtl-c behavior (ref):

if (!IS_MAC) {
    extraKeys['Ctrl-C'] = false;
}

Maybe a setting could be added that allows toggling how crtl-c behaves?

yanking to * and + registers

You also can't yank to the * or + registers in vanilla codemirror vim mode because traditionally browser support for accessing the clipboard seems to have been really inconsistent. But based on the compatibility chart for the navigator.clipboard.writeText api this is possible for all the major browsers except safari (though based on this bugzilla and the safari release notes) safari 13.1 may also support this)

With the writeText function you can populate the the system clipboard. So we can reimplement the codemirror yank function to also fill the system clipboard. I did this in an extension here: https://github.com/ianhi/jupyterlab_vim-system-clipboard-support (jupyter labextension install jupyterlab_vim-system-clipboard-support) if you want to try that out. If all the major browsers end up supporting the clipboard api maybe something like that could be added to the codeeditor? The relevant snippet for that extension is here:

 if (['+', '*'].indexOf(args.registerName) !== -1) {
  navigator.clipboard.writeText(text).catch(err => {
    // This can happen if the user denies clipboard permissions:
    // or if using safari
    console.error('Could not copy text: ', err);
  });
  cm.focus()
}

small gif of this working
clipboard

ctrl-c to copy text in bar.ipynb. Use p to paste into foo.py. This does not work.

Also of note is that you should be able to yank to any register in any jupyterlab codemirror instance, and then use p to paste it into any other codemirror instance in jupyter. There is a VimGlobalState that gets defined inside the 'codemirror/keymap/vim.js' file that enables this behavior. What happened here is the crtl-c was only disabled by jupyterlab_vim, but wasn't changed to fill the * or + or 0 register. If a setting gets implemented in jupyterlab that enables crtl-c to copy then it would be nice if it also filled the appropriate vim registers.

Context menu

A context menu element for copy could probably be implemented using this api. Though annoyingly Firefox only allows browser extensions to use the sibling readText method so there are more limitations with paste. Even without the ability to paste this would solve the probably common annoyance of highlighting an error message then right clicking expecting to copy the error. This has always been a small pain point for me as I wasn't aware of the shift right click until very recently when I saw it mentioned in an issue.

@ianhi ianhi mentioned this issue Oct 4, 2020
3 tasks
@JasonWeill JasonWeill added the tag:Keyboard Compatibility issues with keyboards, including locale-specific and layout-specific issues label Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug pkg:codemirror tag:Keyboard Compatibility issues with keyboards, including locale-specific and layout-specific issues
Projects
None yet
Development

No branches or pull requests

5 participants