Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging/intercepting every keystroke in Vim

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?

like image 847
Wojciech Baranowski Avatar asked May 30 '13 08:05

Wojciech Baranowski


People also ask

Can keylogger record keystrokes?

Short for “keystroke logging,” a keylogger is a type of malicious software that records every keystroke you make on your computer. Keyloggers are a type of spyware — malware designed to spy on victims. Because they can capture everything you type, keyloggers are one of the most invasive forms of malware.

How many keystrokes can a hardware keylogger record?

It has a capacity of up to 2,000,000 keystrokes stored with STRONG 128-bit encryption.

What is keystroke in Linux?

Key Logging is the process of storing keystrokes with/without the knowledge of user. Keylogging can be hardware based as well as software based. As clear from the name, a hardware based keylogger does not depends upon any software and keystroke logging is done at hardware level itself.


2 Answers

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

like image 95
Wojciech Baranowski Avatar answered Sep 28 '22 05:09

Wojciech Baranowski


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.

like image 23
Xavier T. Avatar answered Sep 28 '22 06:09

Xavier T.