Logging/intercepting every keystroke in Vim
Asked Answered
R

4

10

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?

Redemptioner answered 30/5, 2013 at 8:0 Comment(0)
R
2

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/.

Redemptioner answered 30/5, 2013 at 20:47 Comment(0)
F
1

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.

Fancied answered 30/5, 2013 at 13:12 Comment(0)
T
1

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.

Tasha answered 2/7, 2015 at 22:8 Comment(0)
M
0

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.

Mazurka answered 30/5, 2013 at 10:31 Comment(2)
I've done some more searching but couldn't find anything that would suit me. 'screen' is capable of logging the output, but not the input. 'script' logs both input and output, but you cannot make it to log only the input. All the keyloggers I've found either log all the keyboard input (regardless of the tty/process that receives it) or require custom drivers on the kernel level. I'd rather use something that doesn't even require root privileges and IMO it should be doable.Redemptioner
Sorry to necropost, but I can see a use case for implementing this in Vim. If you have several layers to troubleshoot (tmux + zsh + ssh + ... + Vim) then troubleshooting Vim's key mappings also means troubleshooting any number of parent layers. https://mcmap.net/q/1064222/-vim-commands-log -- there seems to be a built-in option for logging, though I haven't tested it yet vim.wikia.com/wiki/Map_Ctrl-S_to_save_current_or_new_files -- an example of a key command intended for Vim but commonly intercepted by terminalsVelda

© 2022 - 2024 — McMap. All rights reserved.