jupyterlab-vim
jupyterlab-vim copied to clipboard
Can't remap Normal mode `j` to `gj` or `k` to `gk` in notebooks
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
-
Keybinding
:j
-
The key sequence to execute
:gj
-
Mode
:normal
(the strange thing is in visual mode the map work) -
Map function
:normap
(ormap
, both can't help this map work)
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
- Jupyter Lab version: 4.0.5
-
jupyterlab-vim
version: 4.0.3
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.