How to create a "new mode" for vim (i.e., loop and get user input, and then perform related actions)

I am trying to create something like "new mode" in vim. The details of the mode are unimportant, but there is one thing I need to do.

I need to do something like the following pseudocode:

get user input (movement keys like "j" or complex keys like "dd") while user_input != <esc> execute the user input endwhile 

In other words, I need a loop that will read what the user is doing and then perform a related action.

I already have the following code:

 let char = nr2char(getchar()) while char =~ '^\w$' execute "normal ". char let char = nr2char(getchar()) endwhile 

This is great for custom motions ( j , k , etc.), but not for more complex multi-character commands like dd .

Also, this is a little annoying, but the cursor disappears during getchar (), which means you cannot see the cursor (this is less important due to what I'm trying to do, but hopefully has a solution as well).

Does anyone know how I can make multi-character actions work?

+4
source share
2 answers

I think you might be interested in subode.vim if you don't use it to at least see how they implemented this function.

+4
source

I usually override locally ( :h map-<buffer> , for example) the things that this new mode should change. And I also redefine <esc> to unregister these things from mode.

This is a simpler IMO approach.

+1
source

Source: https://habr.com/ru/post/1340935/


All Articles