Question

I just started working on a tool to help me increase my productivity with Vim. I want it to log every keystroke to a file and then identify inefficient usage patterns. I would like it to store a timestamp for every keystroke.

I've tried using the -w and -W vim options to dump every keystroke to a pipe. However, Vim doesn't report keystrokes on-line and so I could not obtain reliable timestamps.

I've also tried intercepting input from a tty, writing it to a pipe and redirecting it as stdin for Vim. But then Vim just quits with:

Vim: Warning: Input is not from a terminal

I've also found this trick to capture every key: http://vim.wikia.com/wiki/Capture_all_keys. I don't know anything about vimscript, but I get a feeling that's not what I'm looking for.

So my thinking now is: I need to intercept input from a tty, process it and then write it to some fake tty that Vim will use as an input. Would you agree that's the best approach to take? If so, any hints on how I could do that?

Was it helpful?

Solution

After digging a bit more and looking into 'script' sourcecode, I found this:

man 7 pty
man 4 pts
man 3 openpty

Creating a new pseudo-terminal seems like the way to go here and I'm going to give it a shot.

Edit: It seems to be working. In case someone runs into similar issue, the project can be found at https://github.com/Baranowski/habit-vim/.

OTHER TIPS

A possible approach could be to patch the Vim code : in the loop, where keyboard inputs are captured, save them, and write them to your file. (or you could somewhow add them to the .viminfo)

https://code.google.com/p/vim/source/browse/src/gui.c

It seems that the following function is waiting to get input from the user.

  int gui_wait_for_chars(wtime)

So you might be able to find the entry point for keyboard input from there.

If you're working on mac os x, you can use this code to log your key inputs to vim.

This code borrows heavily from the old unix script command, which is open source.

Using it would look something like this (after downloading the source):

$ make keylog          # No Makefile needed for an easy case like this.
$ ./keylog vim myfile  # ... and use vim a bit, then exit.
$ hexdump -C outfile   # `outfile` is the hard-coded keylog file name.

You could also cat outfile, but it's likely to contain many control characters that make the output difficult to read.

Also, please don't use that code for nefarious purposes. It's kind of dangerous.

Since this is not directly related to Vim (and could be equally applied to improve the use of other keyboard-driven applications), I would not attempt to implement this in Vim (which, as you've already found out, is close to impossible), but rather intercept the keys via operating system means, e.g. through a custom keyboard driver or keylogger. There probably exist several implementations to get you started.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top