jupyterlab-contrib / jupyterlab-vim

Vim notebook cell bindings for JupyterLab
https://jupyterlab-contrib.github.io/jupyterlab-vim.html
MIT License
660 stars 43 forks source link

Can't remap Normal mode `j` to `gj` or `k` to `gk` in notebooks #113

Open Deeperand opened 9 months ago

Deeperand commented 9 months ago

Description

I tried to remap j to gj in normal mode, but it doesn't work. However, if I map M to gj, it will work. However, such map is valid for VISUAL mode.

Reproduce

Setting -> Notebook Vim -> Add with the following parameters

Then try to jump between a long-line-text, one can see j still jump to the next actual line, not the next visual line

Expected behavior

j should be mapped to gj, and therefore it will jump to next visual line

Context

firai commented 9 months ago

I understand you're referring to remapping these keys in notebooks, which is somewhat of a known limitation. Remapping j to gj and k to gk should already work for file editors.

This limitation is because the jupyterlab-vim plugin redefines/hijacks Normal mode j and k in notebook CodeMirror instances to combine regular cursor movement with navigation between cells, which CodeMirror otherwise knows nothing about. If you type in the commands :nnoremap j gj and :nnoremap k gk in a notebook, you should see successful remapping of the keys within the active cell, but the ability to move between cells using j and k in vim Normal mode would be lost. This plugin re-hijacks these keys whenever you switch between cells or between widgets to restore this ability.

In order to remap these keys, we would need to define custom functions to combine moving the cursor by display lines (gj and gk) with navigation between cells. I have created a proof-of-concept for an enhancement to allow remapping these keys at https://github.com/firai/jupyterlab-vim/tree/remap-j-k, but significant work is still required to deal with cursor behavior around edges of cells. Note that the approach taken in that branch is not the only one possible. Contributions, either building off of what I started or independent, are welcome.