Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

editor

I'm trying to create something of a "new mode" in vim. The details of the mode are unimportant, but there is one thing I need to be able to do.

I need to do something like the following pseudo-code:

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, then perform the associated action.

I've already got the following code:

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

This works fine for user movements (j, k, etc.), but fails for more complex multi-character commands like dd.

Also, this is a small annoyance, but the cursor disappears during getchar(), meaning you effectively can't see the cursor (this is of less importance because of what I'm trying to do, but hopefully has a solution as well).

Does anyone have any idea how I can get multi-character actions to work?

like image 302
Edan Maor Avatar asked Feb 23 '11 09:02

Edan Maor


1 Answers

I think you might be interested in submode.vim, if not to use it, to at least see how they've implemented this feature.

like image 111
Randy Morris Avatar answered Sep 20 '22 16:09

Randy Morris