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

custom key mappings #17

Open
nikroy16 opened this issue Jan 25, 2018 · 14 comments
Open

custom key mappings #17

nikroy16 opened this issue Jan 25, 2018 · 14 comments

Comments

@nikroy16
Copy link

Thanks for the great work! A vim binding was the only thing holding me back from using jupyterlab..is there a way to have custom key mappings such as jj instead of Esc? Thanks!

@jwkvam
Copy link
Owner

jwkvam commented Jan 26, 2018

@nikroy16 Thanks for the kind words! Unfortunately there isn't a way to create custom key mappings easily. I do want to add this in, but I can't offer an estimate when I'll get around to it. I'll happily accept a PR if anyone wants to give it a shot.

If you don't mind getting your hands dirty, you can add those shortcuts to the code yourself and do a local install.

For example, try adding this to https://github.com/jwkvam/jupyterlab_vim/blob/371d5625e8b034ecce0a0a14633c0229ddcead67/src/index.ts#L343

        commands.addKeyBinding({
            selector: '.jp-Notebook.jp-mod-editMode',
            keys: ['J J'],
            command: 'leave-insert-mode'
        });

Note: I haven't tried this so I don't know if it will work.

@jimypbr
Copy link

jimypbr commented Feb 21, 2018

Thanks for creating this!

I added your code snipped to index.ts:

        commands.addKeyBinding({
            selector: '.jp-Notebook.jp-mod-editMode',
            keys: ['J J'],
            command: 'leave-insert-mode'
        });

I also tried (my preferred) keybinding of ['F D'], however they both didn't work as expected.

  1. ['J J'] overrides the normal cursor movement of 'J' so down movement doesn't work anymore.
  2. ['F D'] escapes, but leaves a 'f' in the text. In the Jupyter notebook vim plugin the 'f' is inserted, but immediately deleted after the escape.

@jwkvam
Copy link
Owner

jwkvam commented Feb 21, 2018

@jimypbr Thanks for giving it a shot. Rereading my answer, I'm afraid it was not very good :( I somehow confused myself with the different modes.

I took a look at this issue lambdalisue/jupyter-vim-binding#8

Please add the last line from this snippet:

            let lcm = CodeMirror as any;
            let lvim = lcm.Vim as any;
            lvim.defineEx('quit', 'q', function(cm: any) {
                commands.execute('notebook:enter-command-mode');
            });
            lvim.map('jj', '<Esc>', 'insert')

I've tested this and it works. This extension will need some way of allowing users to customize this in the future.

@jimypbr
Copy link

jimypbr commented Feb 21, 2018

@jwkvam I implemented your fix it works just like I expect. Thanks!

@FlyingWombat
Copy link

Any update on defining custom mappings?

@aldanor
Copy link

aldanor commented Apr 13, 2019

Would be nice to be able to map even the limited number of simplest things, like ESC. What would be a reasonable way to go about it? Config file, handled by the jupyter's config system?..

@Suchunsv
Copy link

Suchunsv commented Jun 6, 2019

It doesn't matter that can't use the custom key mapping except the only ESC key that should have the options,we all know to press esc can be annoying and not every computer provide the authority to
modify the regedit to mapping key. (Could you just kill Caps Lock and map to ESC or map jj to ESC?)

@aldanor
Copy link

aldanor commented Nov 14, 2019

@jwkvam @jimypbr Would you guys advise on how to remap this in the current state of things?..

@wsh3776
Copy link

wsh3776 commented Feb 22, 2020

I am using jupyter-vim-binding. It works and is very easy to custom key mappings. Maybe you can try it.

https://github.com/lambdalisue/jupyter-vim-binding

@gilch
Copy link

gilch commented Feb 23, 2020

@meteorsh does it work in the new Jupyter Lab environment or only the old Jupyter Notebooks?

I got it working with jupyterlab-vim and the ex command :imap fd <C-[>, so the capability exists already, however, it does not persist between sessions. @jwkvam since CodeMirror seems to already support :imap does that make the custom key mappings any easier to implement? Maybe like sourcing a vimscript on startup?

@gordonrust
Copy link

gordonrust commented Apr 19, 2020

Unable to get back into normal mode.

@jimypbr @jwkvam I am also able to add a custom 'fd' keybinding to get into normal mode using a local install. However, when I restart the system, ( or , even when opening the same notebook in another browser(opera this time; earlier it was firefox) ) I am unable to go into the normal mode! Note that the extension is working since the remappings such as Ctr-O to create a new cell works as wxpected.

Moreover, the same changes do persist in a docker image! To be clear, I created a docker image, did a local instal wrt to a running container, and then commited that contained with these changes. When I run this new container, everything works as expected.

Update: I think the problem starts after reloading the notebook page. So this may happen when I open a notebook in two browsers. I am able to get back to normal mode in one of the browsers, but can't do so in another browser.

@gordonrust
Copy link

Unable to get back into normal mode.

@jimypbr @jwkvam I am also able to add a custom 'fd' keybinding to get into normal mode using a local install. However, when I restart the system, ( or , even when opening the same notebook in another browser(opera this time; earlier it was firefox) ) I am unable to go into the normal mode! Note that the extension is working since the remappings such as Ctr-O to create a new cell works as wxpected.

Moreover, the same changes do persist in a docker image! To be clear, I created a docker image, did a local instal wrt to a running container, and then commited that contained with these changes. When I run this new container, everything works as expected.

Update: I think the problem starts after reloading the notebook page. So this may happen when I open a notebook in two browsers. I am able to get back to normal mode in one of the browsers, but can't do so in another browser.

Figured a wayaround... close all the open notebooks and refresh the webpage. Things work normally after this :)

@gordonrust
Copy link

@jwkvam ... I was trying to map 'Shift Enter' by changing 'run-select-next-edit' to 'run-select-next-insert'. So that I land in insert mode in the new cell.(Jupyter Lab is mostly for experimentation and it makes sense to have insert as the primary mode, unlike vim editor which has normal as primary, since the philosophy there is that u mostly edit the code rather than writting it.)

I add these two line

    let editor = content.activeCell.editor as CodeMirrorEditor;
     (CodeMirror as any).Vim.handleKey(editor.editor, 'i');

The function adding the Commmand is here:

commands.addCommand('run-select-next-insert', {
        label: 'Run Cell and Edit Next Cell',
        execute: args => {
            const current = getCurrent(args);

            if (current) {
                const { context, content } = current;
                NotebookActions.runAndAdvance(content, context.session);
                    let editor = content.activeCell.editor as CodeMirrorEditor;
                    current.content.mode = 'edit';
                    (CodeMirror as any).Vim.handleKey(editor.editor, 'i');
            }
        },
        isEnabled
    });

Alas, it changes nothing. Any suggestions.

@ianhi
Copy link

ianhi commented May 22, 2020

For remappings inside the editor: i.e. inoremap ii <Esc> using the codemirror vim api map function is the correct thing to do. It's unclear if this repo is still maintained, so in the meantime I made a jupyterlab-vimrc extension (https://github.com/ianhi/jupyterlab-vimrc) that lets you use the various map commands. Since these are stored in settings they will persist across sessions.

I'm happy to try to translate my extension into a PR for this extension if this becomes active again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants