Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Implementing VI-like key-bindings

by osfameron (Hermit)
on Jun 27, 2002 at 09:52 UTC ( #177666=perlquestion: print w/replies, xml ) Need Help??

osfameron has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to be able to implement VI-like keybindings in Perl (and also in VBA... but there's another story) and I've not found a module to do this.

For those who don't know Vi, in summary, it would need to

  1. Accept a keypattern for a command, optionally preceded by a repeat-count.
    • dd
    • 3w
  2. Disambiguate between partial-matches.
    • dd (e.g. delete a line)
    • ddd (custom action)
    e.g. if dd is typed, this action will fire after a timeout unless the last d is pressed.
  3. Allow custom mappings to map to a category of action. (Not sure if I can clearly explain this, here goes). Certain commands take a motion as a parameter: for example the delete command d.
    • dw Delete a word
    • d3w Delete 3 words
    • d} Delete next paragraph
    Other mappings can be made that map to movement commands.
    • :map foo w
    • d3foo delete 3 words
    e.g. when a motion is expected, foo is now one of the combinations that will be allowed, because it in turn is mapped to an atomic motion command.
I suppose that I want some sort of tree-based HoH structure with callbacks at combinations that exist. For example $keys->{d}{d}{callback} = \&delete_line.

Has anyone implemented this before in Perl, or is there a good guide to how this is done in general (Yes, I could read sources, but I don't really know C, and I could do with a gentler introduction before I do that). And has this been turned into a module? (I searched "key", "binding", "vi::", and "Term::" at CPAN but no joy: any other pointers?).


Replies are listed 'Best First'.
Re: Implementing VI-like key-bindings
by bwana147 (Pilgrim) on Jun 27, 2002 at 15:17 UTC

    I dunno whether it might help you out, but some (crazy) people have started out an implementation of vi in Perl.

    Still, I'm wandering how you will tell apart "custom actions" from regular commands, and how the users will know that what they thought would be a regular command has in fact been overridden and now does something completely different from what they expect (depends on how you tune the time out, it can become pretty confusing, not to mention dangerous).

    e.g., d3foo normally deletes up to the 3rd occurence of the letter 'o' on the current line, then starts a new line below the current one. Odds are that some foolishly devised map: commands will make the whole thing unusable.

    Vim has a nice approach (IMHO) of prefixing non-standard commands with g, a letter that's otherwise unused in standard vi.



      Thanks: I downloaded the Vip files, but they are zipped in .bz2 format (probably trivial in Unix, but WinZip doesn't open them. Where did I put those ported GNU utilities again?)

      Also, it requires Curses, which I'm struggling to install. (I compiled PDCurses using VC++, but I can't make for some reason - it's looking for a nonexistent pdcurses.h header file? But I digress).


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://177666]
Approved by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2021-09-19 03:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found