41

I know that Vim keeps history for ex commands, searches, jump locations, changes, and possibly others.

With showcmd set, Vim also shows you what keys you have entered so far for a normal mode command, until that command is executed.

Sometimes, when in normal mode, I mistype and actions occur. I can undo these actions, but I sometimes want to know how the edits during my blunder occurred. Sometimes the edit looks cool and I want to know how to repeat it in the future.

So, is there any way of seeing the history of key presses in normal mode, preferably delimited when an actual edit or action (like movement or mode change) occurred?

200_success
  • 9,549
  • 5
  • 51
  • 64
John O'M.
  • 8,552
  • 2
  • 42
  • 58

2 Answers2

23

Following the link from @WChargin a bit further, I found this:

You can start vim with the -w or -W option as follows:

vim -w keys.txt my_file

All the characters that you type are recorded in the file {scriptout}, until you exit Vim.

-w will append to the specified file if it exists; -W will overwrite it.

It doesn't quite get you what you want, but it's the right direction.

Update: It looks like vim actually buffers all the keystrokes until you quit vim, but there is a one-line patch here that will write the keystrokes immediately.

xthrd
  • 5,310
  • 1
  • 15
  • 15
  • Thanks! I'm using this as a starting point for poking into the source code. It looks like -w saves every single key press, not just normal-mode commands, but you are correct: it is the right direction! – John O'M. Feb 08 '15 at 03:02
  • @xthrd, how are these keys.txt file formatted/structured? When I use -w, appending is always happening in a long-long-long line; and, there are weird characters displayed: under UTF-8, whenever I open a file in a new buffer, I will get €齛. This is a good marker by itself, but it would be great if the logs could be parsed optionally. – llinfeng Nov 19 '17 at 15:02
  • @llinfeng It's not formatted at all. It's literally just a file containing the keystrokes in the order they were made. (Although note that if some of the keystrokes are <cr>, then the file will contain linebreaks.) This is because the main purpose is for creating files that can be played back with the -s option. But why not try it out for yourself! – Rich Nov 28 '17 at 12:24
  • @Rich Thank you for clarifying. I have used this key-tracking mechanism to debug the problem that started my search. [My lesson is that: never map W for the command line.] – llinfeng Nov 28 '17 at 16:50
  • @llinfeng Why not? – Aaron Thoma Mar 08 '22 at 23:19
  • @Aaron Frankly speaking, I don't remember why I said that.

    Though, with command! W some_other_thing in my .vimrc, the logging with -W still works as intended. That is, the logging method should still work with :W mapped to do something else.

    Per my "old lesson about W", well, I may have been referring to something that started my search back in 2017.

    – llinfeng Mar 09 '22 at 14:24
-4

There is q: which shows a command history.

I also find it useful that you can press up in a partially completed command to cycle through matches. E.g. /abc Then pressing up will cycle through your history of searches starting with 'abc'

Collin Peters
  • 2,246
  • 1
  • 15
  • 9
  • 5
    q: shows ex command history, and is useful, but is not a log of normal commands, like "ad2tn (delete into register a until the second n) – John O'M. Feb 07 '15 at 17:37
  • Don't forget that while in the middle of editing a command you can bring up current line in the history editor and use vi to edit the line by pressing Ctrl+f – Sukima Feb 19 '15 at 04:20